77from rest_framework import exceptions , serializers
88from rest_framework .exceptions import AuthenticationFailed , ValidationError
99
10- from .exceptions import TokenError , TokenFamilyNotConfigured , RefreshTokenBlacklistedError
10+ from .cache import blacklist_cache
11+ from .exceptions import (
12+ RefreshTokenBlacklistedError ,
13+ TokenError ,
14+ TokenFamilyNotConfigured ,
15+ )
1116from .models import TokenUser
1217from .settings import api_settings
13- from .tokens import RefreshToken , SlidingToken , Token , UntypedToken , FamilyMixin
14- from .cache import blacklist_cache
1518from .token_blacklist .models import BlacklistedToken
19+ from .tokens import FamilyMixin , RefreshToken , SlidingToken , Token , UntypedToken
1620
1721AuthUser = TypeVar ("AuthUser" , AbstractBaseUser , TokenUser )
1822
@@ -143,7 +147,7 @@ def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
143147 data ["refresh" ] = str (refresh )
144148
145149 return data
146-
150+
147151 def _get_refresh_token (self , token_str : str ) -> RefreshToken :
148152 """
149153 Handles refresh token instantiation and family blacklisting if enabled.
@@ -152,9 +156,9 @@ def _get_refresh_token(self, token_str: str) -> RefreshToken:
152156 return self .token_class (token_str )
153157 except RefreshTokenBlacklistedError as e :
154158 if (
155- api_settings .TOKEN_FAMILY_ENABLED and
156- api_settings .TOKEN_FAMILY_BLACKLIST_ON_REUSE and
157- "rest_framework_simplejwt.token_family" in settings .INSTALLED_APPS
159+ api_settings .TOKEN_FAMILY_ENABLED
160+ and api_settings .TOKEN_FAMILY_BLACKLIST_ON_REUSE
161+ and "rest_framework_simplejwt.token_family" in settings .INSTALLED_APPS
158162 ):
159163 refresh = self .token_class (token = token_str , verify = False )
160164 family_id = refresh .get_family_id ()
@@ -202,13 +206,13 @@ def validate(self, attrs: dict[str, None]) -> dict[Any, Any]:
202206
203207 if BlacklistedToken .objects .filter (token__jti = jti ).exists ():
204208 raise ValidationError (_ ("Token is blacklisted" ))
205-
209+
206210 if (
207- api_settings .TOKEN_FAMILY_ENABLED and
208- "rest_framework_simplejwt.token_family" in settings .INSTALLED_APPS
209- ):
210- FamilyMixin .check_family_expiration (token = token )
211- FamilyMixin .check_family_blacklist (token = token )
211+ api_settings .TOKEN_FAMILY_ENABLED
212+ and "rest_framework_simplejwt.token_family" in settings .INSTALLED_APPS
213+ ):
214+ FamilyMixin .check_family_expiration (token = token )
215+ FamilyMixin .check_family_blacklist (token = token )
212216
213217 return {}
214218
@@ -224,7 +228,7 @@ def validate(self, attrs: dict[str, Any]) -> dict[Any, Any]:
224228 except AttributeError :
225229 pass
226230 return {}
227-
231+
228232
229233class TokenFamilyBlacklistSerializer (serializers .Serializer ):
230234 refresh = serializers .CharField (write_only = True )
@@ -238,5 +242,5 @@ def validate(self, attrs: dict[str, Any]) -> dict[Any, Any]:
238242 raise TokenFamilyNotConfigured ()
239243 except TokenError as e :
240244 raise serializers .ValidationError ({"refresh" : str (e )})
241-
245+
242246 return {"message" : "Token Family blacklisted" }
0 commit comments