Coverage for api\authentication.py: 91.7%
20 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-30 20:58 +0200
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-30 20:58 +0200
1from rest_framework_simplejwt.authentication import JWTAuthentication
2from rest_framework_simplejwt.exceptions import AuthenticationFailed
3from rest_framework_simplejwt.settings import api_settings
4from users.models import Users
5from users.services.auth_service import AuthService
7class UsersJWTAuthentication(JWTAuthentication):
8 """
9 Autenticacion de usuarios usando JWT
10 """
11 def get_user(self, validated_token):
12 try:
13 user_id = validated_token[api_settings.USER_ID_CLAIM]
14 except KeyError:
15 raise AuthenticationFailed('Token contained no recognizable user identification', code='token_no_user_id')
17 try:
18 user = Users.objects.get(**{api_settings.USER_ID_FIELD: user_id})
19 except Users.DoesNotExist:
20 raise AuthenticationFailed('User not found', code='user_not_found')
22 if not user: 22 ↛ 23line 22 didn't jump to line 23 because the condition on line 22 was never true
23 raise AuthenticationFailed('User not found', code='user_not_found')
25 if AuthService.token_is_revoked(user, validated_token):
26 raise AuthenticationFailed('Token revoked', code='token_revoked')
28 return user