This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
109 lines (79 loc) · 2.94 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from django.contrib.auth import models as auth_models
from django.db.models.manager import EmptyManager
from django.utils.functional import cached_property
from .compat import CallableFalse, CallableTrue
from .settings import api_settings
class TokenUser:
"""
A dummy user class modeled after django.contrib.auth.models.AnonymousUser.
Used in conjunction with the `JWTStatelessUserAuthentication` backend to
implement single sign-on functionality across services which share the same
secret key. `JWTStatelessUserAuthentication` will return an instance of this
class instead of a `User` model instance. Instances of this class act as
stateless user objects which are backed by validated tokens.
"""
# User is always active since Simple JWT will never issue a token for an
# inactive user
is_active = True
_groups = EmptyManager(auth_models.Group)
_user_permissions = EmptyManager(auth_models.Permission)
def __init__(self, token):
self.token = token
def __str__(self):
return f"TokenUser {self.id}"
@cached_property
def id(self):
return self.token[api_settings.USER_ID_CLAIM]
@cached_property
def pk(self):
return self.id
@cached_property
def username(self):
return self.token.get("username", "")
@cached_property
def is_staff(self):
return self.token.get("is_staff", False)
@cached_property
def is_superuser(self):
return self.token.get("is_superuser", False)
def __eq__(self, other):
return self.id == other.id
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.id)
def save(self):
raise NotImplementedError("Token users have no DB representation")
def delete(self):
raise NotImplementedError("Token users have no DB representation")
def set_password(self, raw_password):
raise NotImplementedError("Token users have no DB representation")
def check_password(self, raw_password):
raise NotImplementedError("Token users have no DB representation")
@property
def groups(self):
return self._groups
@property
def user_permissions(self):
return self._user_permissions
def get_group_permissions(self, obj=None):
return set()
def get_all_permissions(self, obj=None):
return set()
def has_perm(self, perm, obj=None):
return False
def has_perms(self, perm_list, obj=None):
return False
def has_module_perms(self, module):
return False
@property
def is_anonymous(self):
return CallableFalse
@property
def is_authenticated(self):
return CallableTrue
def get_username(self):
return self.username
def __getattr__(self, attr):
"""This acts as a backup attribute getter for custom claims defined in Token serializers."""
return self.token.get(attr, None)