models.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from django.contrib.auth import models as auth_models
  2. from django.db.models.manager import EmptyManager
  3. from django.utils.functional import cached_property
  4. from .compat import CallableFalse, CallableTrue
  5. from .settings import api_settings
  6. class TokenUser:
  7. """
  8. A dummy user class modeled after django.contrib.auth.models.AnonymousUser.
  9. Used in conjunction with the `JWTTokenUserAuthentication` backend to
  10. implement single sign-on functionality across services which share the same
  11. secret key. `JWTTokenUserAuthentication` will return an instance of this
  12. class instead of a `User` model instance. Instances of this class act as
  13. stateless user objects which are backed by validated tokens.
  14. """
  15. # User is always active since Simple JWT will never issue a token for an
  16. # inactive user
  17. is_active = True
  18. _groups = EmptyManager(auth_models.Group)
  19. _user_permissions = EmptyManager(auth_models.Permission)
  20. def __init__(self, token):
  21. self.token = token
  22. def __str__(self):
  23. return 'TokenUser {}'.format(self.id)
  24. @cached_property
  25. def id(self):
  26. return self.token[api_settings.USER_ID_CLAIM]
  27. @cached_property
  28. def pk(self):
  29. return self.id
  30. @cached_property
  31. def username(self):
  32. return self.token.get('username', '')
  33. @cached_property
  34. def is_staff(self):
  35. return self.token.get('is_staff', False)
  36. @cached_property
  37. def is_superuser(self):
  38. return self.token.get('is_superuser', False)
  39. def __eq__(self, other):
  40. return self.id == other.id
  41. def __ne__(self, other):
  42. return not self.__eq__(other)
  43. def __hash__(self):
  44. return hash(self.id)
  45. def save(self):
  46. raise NotImplementedError('Token users have no DB representation')
  47. def delete(self):
  48. raise NotImplementedError('Token users have no DB representation')
  49. def set_password(self, raw_password):
  50. raise NotImplementedError('Token users have no DB representation')
  51. def check_password(self, raw_password):
  52. raise NotImplementedError('Token users have no DB representation')
  53. @property
  54. def groups(self):
  55. return self._groups
  56. @property
  57. def user_permissions(self):
  58. return self._user_permissions
  59. def get_group_permissions(self, obj=None):
  60. return set()
  61. def get_all_permissions(self, obj=None):
  62. return set()
  63. def has_perm(self, perm, obj=None):
  64. return False
  65. def has_perms(self, perm_list, obj=None):
  66. return False
  67. def has_module_perms(self, module):
  68. return False
  69. @property
  70. def is_anonymous(self):
  71. return CallableFalse
  72. @property
  73. def is_authenticated(self):
  74. return CallableTrue
  75. def get_username(self):
  76. return self.username