-
Notifications
You must be signed in to change notification settings - Fork 700
Expand file tree
/
Copy pathserializers.py
More file actions
276 lines (209 loc) · 9.43 KB
/
serializers.py
File metadata and controls
276 lines (209 loc) · 9.43 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from typing import Any, Optional, TypeVar
from django.conf import settings
from django.contrib.auth import _clean_credentials, authenticate, get_user_model
from django.contrib.auth.models import AbstractBaseUser, update_last_login
from django.utils.translation import gettext_lazy as _
from rest_framework import exceptions, serializers
from rest_framework.exceptions import AuthenticationFailed, ValidationError
from rest_framework.request import Request
from .models import TokenUser
from .settings import api_settings
from .tokens import RefreshToken, SlidingToken, Token, UntypedToken
from .utils import get_md5_hash_password
AuthUser = TypeVar("AuthUser", AbstractBaseUser, TokenUser)
if api_settings.BLACKLIST_AFTER_ROTATION:
from .token_blacklist.models import BlacklistedToken
class PasswordField(serializers.CharField):
def __init__(self, *args, **kwargs) -> None:
kwargs.setdefault("style", {})
kwargs["style"]["input_type"] = "password"
kwargs["write_only"] = True
super().__init__(*args, **kwargs)
class TokenObtainSerializer(serializers.Serializer):
username_field = get_user_model().USERNAME_FIELD
token_class: type[Token] | None = None
default_error_messages = {
"no_active_account": _("No active account found with the given credentials")
}
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.fields[self.username_field] = serializers.CharField(write_only=True)
self.fields["password"] = PasswordField()
def validate(self, attrs: dict[str, Any]) -> dict[Any, Any]:
authenticate_kwargs = {
self.username_field: attrs[self.username_field],
"password": attrs["password"],
}
try:
authenticate_kwargs["request"] = self.context["request"]
except KeyError:
pass
self.user = authenticate(**authenticate_kwargs)
if not api_settings.USER_AUTHENTICATION_RULE(self.user):
api_settings.ON_LOGIN_FAILED(
_clean_credentials(attrs), self.context.get("request")
)
raise exceptions.AuthenticationFailed(
self.error_messages["no_active_account"],
"no_active_account",
)
return {}
@classmethod
def get_token(cls, user: AuthUser) -> Token:
return cls.token_class.for_user(user) # type: ignore
class TokenObtainPairSerializer(TokenObtainSerializer):
token_class = RefreshToken
# Dynamically add read-only fields for schema generation only
access = serializers.SerializerMethodField(read_only=True)
refresh = serializers.SerializerMethodField(read_only=True)
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
data = super().validate(attrs)
refresh = self.get_token(self.user)
data["refresh"] = str(refresh)
data["access"] = str(refresh.access_token)
if api_settings.UPDATE_LAST_LOGIN:
api_settings.ON_LOGIN_SUCCESS(self.user, self.context.get("request"))
return data
class TokenObtainSlidingSerializer(TokenObtainSerializer):
token_class = SlidingToken
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
data = super().validate(attrs)
token = self.get_token(self.user)
data["token"] = str(token)
if api_settings.UPDATE_LAST_LOGIN:
api_settings.ON_LOGIN_SUCCESS(self.user, self.context.get("request"))
return data
class TokenRefreshSerializer(serializers.Serializer):
refresh = serializers.CharField()
access = serializers.CharField(read_only=True)
token_class = RefreshToken
default_error_messages = {
"no_active_account": _("No active account found for the given token."),
"password_changed": _("The user's password has been changed."),
}
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
refresh = self.token_class(attrs["refresh"])
user_id = refresh.payload.get(api_settings.USER_ID_CLAIM, None)
if user_id:
try:
user = get_user_model().objects.get(
**{api_settings.USER_ID_FIELD: user_id}
)
except get_user_model().DoesNotExist:
# This handles the case where the user has been deleted.
raise AuthenticationFailed(
self.error_messages["no_active_account"], "no_active_account"
)
if not api_settings.USER_AUTHENTICATION_RULE(user):
raise AuthenticationFailed(
self.error_messages["no_active_account"], "no_active_account"
)
if api_settings.CHECK_REVOKE_TOKEN:
if refresh.payload.get(
api_settings.REVOKE_TOKEN_CLAIM
) != get_md5_hash_password(user.password):
# If the password has changed, we blacklist the token
# to prevent any further use.
if (
"rest_framework_simplejwt.token_blacklist"
in settings.INSTALLED_APPS
):
try:
refresh.blacklist()
except AttributeError:
pass
raise AuthenticationFailed(
self.error_messages["password_changed"],
code="password_changed",
)
data = {"access": str(refresh.access_token)}
if api_settings.ROTATE_REFRESH_TOKENS:
if api_settings.BLACKLIST_AFTER_ROTATION:
try:
# Attempt to blacklist the given refresh token
refresh.blacklist()
except AttributeError:
# If blacklist app not installed, `blacklist` method will
# not be present
pass
refresh.set_jti()
refresh.set_exp()
refresh.set_iat()
refresh.outstand()
data["refresh"] = str(refresh)
return data
class TokenRefreshSlidingSerializer(serializers.Serializer):
token = serializers.CharField()
token_class = SlidingToken
default_error_messages = {
"no_active_account": _("No active account found for the given token."),
"password_changed": _("The user's password has been changed."),
}
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
token = self.token_class(attrs["token"])
user_id = token.payload.get(api_settings.USER_ID_CLAIM, None)
if user_id:
try:
user = get_user_model().objects.get(
**{api_settings.USER_ID_FIELD: user_id}
)
except get_user_model().DoesNotExist:
# This handles the case where the user has been deleted.
raise AuthenticationFailed(
self.error_messages["no_active_account"], "no_active_account"
)
if not api_settings.USER_AUTHENTICATION_RULE(user):
raise AuthenticationFailed(
self.error_messages["no_active_account"], "no_active_account"
)
if api_settings.CHECK_REVOKE_TOKEN:
if token.payload.get(
api_settings.REVOKE_TOKEN_CLAIM
) != get_md5_hash_password(user.password):
# If the password has changed, we blacklist the token
# to prevent any further use.
if (
"rest_framework_simplejwt.token_blacklist"
in settings.INSTALLED_APPS
):
try:
token.blacklist()
except AttributeError:
pass
raise AuthenticationFailed(
self.error_messages["password_changed"],
code="password_changed",
)
# Check that the timestamp in the "refresh_exp" claim has not
# passed
token.check_exp(api_settings.SLIDING_TOKEN_REFRESH_EXP_CLAIM)
# Update the "exp" and "iat" claims
token.set_exp()
token.set_iat()
return {"token": str(token)}
class TokenVerifySerializer(serializers.Serializer):
token = serializers.CharField(write_only=True)
def validate(self, attrs: dict[str, None]) -> dict[Any, Any]:
token = UntypedToken(attrs["token"])
if (
api_settings.BLACKLIST_AFTER_ROTATION
and "rest_framework_simplejwt.token_blacklist" in settings.INSTALLED_APPS
):
jti = token.get(api_settings.JTI_CLAIM)
if BlacklistedToken.objects.filter(token__jti=jti).exists():
raise ValidationError(_("Token is blacklisted"))
return {}
class TokenBlacklistSerializer(serializers.Serializer):
refresh = serializers.CharField(write_only=True)
token_class = RefreshToken
def validate(self, attrs: dict[str, Any]) -> dict[Any, Any]:
refresh = self.token_class(attrs["refresh"])
try:
refresh.blacklist()
except AttributeError:
pass
return {}
def default_on_login_success(user: AuthUser, request: Request | None) -> None:
update_last_login(None, user)
def default_on_login_failed(credentials: dict, request: Request | None) -> None:
pass