@@ -475,9 +475,19 @@ def save_bearer_token(self, token, request, *args, **kwargs):
475
475
if "scope" not in token :
476
476
raise FatalClientError ("Failed to renew access token: missing scope" )
477
477
478
- # "authenticate_client" sets the client (Application) on request
479
- application = request .client
480
- access_token_expire_seconds = application .access_token_expire_seconds
478
+ # "authenticate_client" sets the client (Application) on request.
479
+ app = request .client
480
+
481
+ # Users on older app versions should get long-lived tokens for
482
+ # backwards compatibility.
483
+ is_legacy_token = request .POST .get ('is_legacy_token' , False )
484
+
485
+ if is_legacy_token :
486
+ access_token_expire_seconds = (
487
+ settings .LEGACY_ACCESS_TOKEN_EXPIRE_SECONDS ,
488
+ )
489
+ else :
490
+ access_token_expire_seconds = app .access_token_expire_seconds
481
491
482
492
# expires_in is passed to Server on initialization
483
493
# custom server class can have logic to override this
@@ -654,24 +664,18 @@ def validate_refresh_token(self, refresh_token, client, request, *args, **kwargs
654
664
seconds = oauth2_settings .REFRESH_TOKEN_GRACE_PERIOD_SECONDS
655
665
)
656
666
)
657
- rt = RefreshToken .objects .filter (null_or_recent , token = refresh_token ).select_related (
658
- "access_token" ,
659
- "application" ,
660
- ).first ()
667
+ rt = (
668
+ RefreshToken .objects
669
+ .filter (null_or_recent , token = refresh_token )
670
+ .select_related ("user" , "access_token" , "application" )
671
+ .first ()
672
+ )
661
673
662
674
if not rt :
663
675
return False
664
676
665
- # Access and refresh token expiration is configurable by Application
666
- # Determine refresh token expiration datetime by adding the timedelta
667
- # of "refresh_token_expire_seconds" to the "created" datetime
668
- expire_seconds = rt .application .refresh_token_expire_seconds
669
- expires = rt .created + timedelta (seconds = expire_seconds )
670
-
671
- is_expired = timezone .now () >= expires
672
-
673
- # Revoke token if expired
674
- if is_expired :
677
+ # Revoke token if expired.
678
+ if rt .is_expired :
675
679
try :
676
680
rt .revoke ()
677
681
# Catch exception in case access or refresh token do not exist
@@ -686,7 +690,7 @@ def validate_refresh_token(self, refresh_token, client, request, *args, **kwargs
686
690
# Token is valid if it refers to the right client AND is not expired
687
691
is_valid = (
688
692
rt .application == client and
689
- not is_expired
693
+ not rt . is_expired
690
694
)
691
695
692
696
return is_valid
0 commit comments