Skip to content

Commit

Permalink
fix: DIA-1935: fix JWT feature flag (#7128)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakelley authored Feb 26, 2025
1 parent db6754a commit 441679c
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions label_studio/jwt_auth/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,21 @@ def __call__(self, request):
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework_simplejwt.exceptions import AuthenticationFailed, InvalidToken, TokenError

JWT_ACCESS_TOKEN_ENABLED = flag_set('fflag__feature_develop__prompts__dia_1829_jwt_token_auth')
if JWT_ACCESS_TOKEN_ENABLED:
try:
user_and_token = JWTAuthentication().authenticate(request)
if not user_and_token:
logger.debug('JWT auth could not resolve user/token')
return self.get_response(request)

try:
user_and_token = JWTAuthentication().authenticate(request)
if user_and_token:
user = User.objects.get(pk=user_and_token[0].pk)
if user.active_organization.jwt.api_tokens_enabled:
logger.debug('JWT auth resolved user/token')
JWT_ACCESS_TOKEN_ENABLED = flag_set(
'fflag__feature_develop__prompts__dia_1829_jwt_token_auth', user=user
)
if JWT_ACCESS_TOKEN_ENABLED and user.active_organization.jwt.api_tokens_enabled:
request.user = user
request.is_jwt = True
else:
logger.debug('JWT auth resolved user/token, but org does not have jwt enabled')

except User.DoesNotExist:
logger.info('JWT authentication failed: User no longer exists')
return JsonResponse({'detail': 'User not found'}, status=status.HTTP_401_UNAUTHORIZED)
except (AuthenticationFailed, InvalidToken, TokenError) as e:
logger.info('JWT authentication failed: %s', e)
# don't raise 401 here, fallback to other auth methods (in case token is valid for them)
# (have unit tests verifying that this still results in a 401 if other auth mechanisms fail)
except User.DoesNotExist:
logger.info('JWT authentication failed: User no longer exists')
return JsonResponse({'detail': 'User not found'}, status=status.HTTP_401_UNAUTHORIZED)
except (AuthenticationFailed, InvalidToken, TokenError) as e:
logger.info('JWT authentication failed: %s', e)
# don't raise 401 here, fallback to other auth methods (in case token is valid for them)
# (have unit tests verifying that this still results in a 401 if other auth mechanisms fail)
return self.get_response(request)

0 comments on commit 441679c

Please sign in to comment.