@@ -170,7 +170,8 @@ def check_exp(self, claim: str = "exp", current_time: Optional[datetime] = None)
170170 raise TokenError (format_lazy (_ ("Token has no '{}' claim" ), claim ))
171171
172172 claim_time = datetime_from_epoch (claim_value )
173- if claim_time <= current_time :
173+ leeway = self .get_token_backend ().leeway
174+ if claim_time <= current_time - timedelta (seconds = leeway ):
174175 raise TokenError (format_lazy (_ ("Token '{}' claim has expired" ), claim ))
175176
176177 @classmethod
@@ -190,11 +191,16 @@ def for_user(cls, user: Type[AbstractBaseUser]) -> Union["Token", Type["Token"]]
190191
191192 _token_backend = None
192193
193- def get_token_backend (self ):
194+ @property
195+ def token_backend (self ):
194196 if self ._token_backend is None :
195197 self ._token_backend = import_string ("ninja_jwt.state.token_backend" )
196198 return self ._token_backend
197199
200+ def get_token_backend (self ):
201+ # Backward compatibility.
202+ return self .token_backend
203+
198204
199205class BlacklistMixin :
200206 """
@@ -277,6 +283,11 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
277283 )
278284
279285
286+ class AccessToken (Token ):
287+ token_type : str = "access"
288+ lifetime : timedelta = api_settings .ACCESS_TOKEN_LIFETIME
289+
290+
280291class RefreshToken (BlacklistMixin , Token ):
281292 token_type : str = "refresh"
282293 lifetime : timedelta = api_settings .REFRESH_TOKEN_LIFETIME
@@ -290,6 +301,7 @@ class RefreshToken(BlacklistMixin, Token):
290301 api_settings .JTI_CLAIM ,
291302 "jti" ,
292303 )
304+ access_token_class = AccessToken
293305
294306 @property
295307 def access_token (self ) -> "AccessToken" :
@@ -298,7 +310,7 @@ def access_token(self) -> "AccessToken":
298310 claims present in this refresh token to the new access token except
299311 those claims listed in the `no_copy_claims` attribute.
300312 """
301- access = AccessToken ()
313+ access = self . access_token_class ()
302314
303315 # Use instantiation time of refresh token as relative timestamp for
304316 # access token "exp" claim. This ensures that both a refresh and
@@ -315,11 +327,6 @@ def access_token(self) -> "AccessToken":
315327 return access
316328
317329
318- class AccessToken (Token ):
319- token_type : str = "access"
320- lifetime : timedelta = api_settings .ACCESS_TOKEN_LIFETIME
321-
322-
323330class UntypedToken (Token ):
324331 token_type : str = "untyped"
325332 lifetime : timedelta = timedelta (seconds = 0 )
0 commit comments