@@ -18,27 +18,21 @@ def __call__(self, request):
18
18
from rest_framework_simplejwt .authentication import JWTAuthentication
19
19
from rest_framework_simplejwt .exceptions import AuthenticationFailed , InvalidToken , TokenError
20
20
21
- JWT_ACCESS_TOKEN_ENABLED = flag_set ('fflag__feature_develop__prompts__dia_1829_jwt_token_auth' )
22
- if JWT_ACCESS_TOKEN_ENABLED :
23
- try :
24
- user_and_token = JWTAuthentication ().authenticate (request )
25
- if not user_and_token :
26
- logger .debug ('JWT auth could not resolve user/token' )
27
- return self .get_response (request )
28
-
21
+ try :
22
+ user_and_token = JWTAuthentication ().authenticate (request )
23
+ if user_and_token :
29
24
user = User .objects .get (pk = user_and_token [0 ].pk )
30
- if user .active_organization .jwt .api_tokens_enabled :
31
- logger .debug ('JWT auth resolved user/token' )
25
+ JWT_ACCESS_TOKEN_ENABLED = flag_set (
26
+ 'fflag__feature_develop__prompts__dia_1829_jwt_token_auth' , user = user
27
+ )
28
+ if JWT_ACCESS_TOKEN_ENABLED and user .active_organization .jwt .api_tokens_enabled :
32
29
request .user = user
33
30
request .is_jwt = True
34
- else :
35
- logger .debug ('JWT auth resolved user/token, but org does not have jwt enabled' )
36
-
37
- except User .DoesNotExist :
38
- logger .info ('JWT authentication failed: User no longer exists' )
39
- return JsonResponse ({'detail' : 'User not found' }, status = status .HTTP_401_UNAUTHORIZED )
40
- except (AuthenticationFailed , InvalidToken , TokenError ) as e :
41
- logger .info ('JWT authentication failed: %s' , e )
42
- # don't raise 401 here, fallback to other auth methods (in case token is valid for them)
43
- # (have unit tests verifying that this still results in a 401 if other auth mechanisms fail)
31
+ except User .DoesNotExist :
32
+ logger .info ('JWT authentication failed: User no longer exists' )
33
+ return JsonResponse ({'detail' : 'User not found' }, status = status .HTTP_401_UNAUTHORIZED )
34
+ except (AuthenticationFailed , InvalidToken , TokenError ) as e :
35
+ logger .info ('JWT authentication failed: %s' , e )
36
+ # don't raise 401 here, fallback to other auth methods (in case token is valid for them)
37
+ # (have unit tests verifying that this still results in a 401 if other auth mechanisms fail)
44
38
return self .get_response (request )
0 commit comments