1
1
from datetime import datetime , timedelta
2
- from typing import TYPE_CHECKING , Any , Generic , Optional , TypeVar
2
+ from typing import TYPE_CHECKING , Any , Optional , TypeAlias , TypeVar
3
3
from uuid import uuid4
4
4
5
5
from django .conf import settings
6
6
from django .contrib .auth import get_user_model
7
- from django .contrib .auth .models import AbstractBaseUser
8
7
from django .utils .module_loading import import_string
9
8
from django .utils .translation import gettext_lazy as _
10
9
14
13
TokenBackendExpiredToken ,
15
14
TokenError ,
16
15
)
17
- from .models import TokenUser
16
+ from .models import TokenUserBase
18
17
from .settings import api_settings
19
18
from .token_blacklist .models import BlacklistedToken , OutstandingToken
20
19
from .utils import (
29
28
if TYPE_CHECKING :
30
29
from .backends import TokenBackend
31
30
32
- T = TypeVar ("T" , bound = "Token" )
31
+ TokenBase : TypeAlias = "Token"
32
+ else :
33
+ TokenBase = object
33
34
34
- AuthUser = TypeVar ("AuthUser" , AbstractBaseUser , TokenUser )
35
+
36
+ T = TypeVar ("T" , bound = TokenBase )
35
37
36
38
37
39
class Token :
@@ -164,13 +166,13 @@ def set_exp(
164
166
See here:
165
167
https://tools.ietf.org/html/rfc7519#section-4.1.4
166
168
"""
167
- if from_time is None :
168
- from_time = self .current_time
169
+ from_time_datetime = from_time or self . current_time
170
+ lifetime_timedelta = lifetime or self .lifetime
169
171
170
- if lifetime is None :
171
- lifetime = self . lifetime
172
+ if TYPE_CHECKING :
173
+ assert lifetime_timedelta
172
174
173
- self .payload [claim ] = datetime_to_epoch (from_time + lifetime )
175
+ self .payload [claim ] = datetime_to_epoch (from_time_datetime + lifetime_timedelta )
174
176
175
177
def set_iat (self , claim : str = "iat" , at_time : Optional [datetime ] = None ) -> None :
176
178
"""
@@ -213,15 +215,15 @@ def outstand(self) -> Optional[OutstandingToken]:
213
215
return None
214
216
215
217
@classmethod
216
- def for_user (cls : type [T ], user : AuthUser ) -> T :
218
+ def for_user (cls : type [T ], user : TokenUserBase ) -> T :
217
219
"""
218
220
Returns an authorization token for the given user that will be provided
219
221
after authenticating the user's credentials.
220
222
"""
221
223
222
224
if hasattr (user , "is_active" ) and not user .is_active :
223
225
logger .warning (
224
- f"Creating token for inactive user: { user .id } . If this is not intentional, consider checking the user's status before calling the `for_user` method."
226
+ f"Creating token for inactive user: { user .pk } . If this is not intentional, consider checking the user's status before calling the `for_user` method."
225
227
)
226
228
227
229
user_id = getattr (user , api_settings .USER_ID_FIELD )
@@ -253,7 +255,7 @@ def get_token_backend(self) -> "TokenBackend":
253
255
return self .token_backend
254
256
255
257
256
- class BlacklistMixin (Generic [ T ] ):
258
+ class BlacklistMixin (TokenBase ):
257
259
"""
258
260
If the `rest_framework_simplejwt.token_blacklist` app was configured to be
259
261
used, tokens created from `BlacklistMixin` subclasses will insert
@@ -333,7 +335,7 @@ def outstand(self) -> Optional[OutstandingToken]:
333
335
)
334
336
335
337
@classmethod
336
- def for_user (cls : type [T ], user : AuthUser ) -> T :
338
+ def for_user (cls : type [T ], user : TokenUserBase ) -> T :
337
339
"""
338
340
Adds this token to the outstanding token list.
339
341
"""
@@ -353,7 +355,7 @@ def for_user(cls: type[T], user: AuthUser) -> T:
353
355
return token
354
356
355
357
356
- class SlidingToken (BlacklistMixin [ "SlidingToken" ] , Token ):
358
+ class SlidingToken (BlacklistMixin , Token ):
357
359
token_type = "sliding"
358
360
lifetime = api_settings .SLIDING_TOKEN_LIFETIME
359
361
@@ -374,7 +376,7 @@ class AccessToken(Token):
374
376
lifetime = api_settings .ACCESS_TOKEN_LIFETIME
375
377
376
378
377
- class RefreshToken (BlacklistMixin [ "RefreshToken" ] , Token ):
379
+ class RefreshToken (BlacklistMixin , Token ):
378
380
token_type = "refresh"
379
381
lifetime = api_settings .REFRESH_TOKEN_LIFETIME
380
382
no_copy_claims = (
0 commit comments