Skip to content

Commit

Permalink
Update get_user to use new setting and remove unnecessary class
Browse files Browse the repository at this point in the history
  • Loading branch information
zxkeyy committed Dec 8, 2024
1 parent 18f62dc commit 8ab5c92
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 60 deletions.
34 changes: 1 addition & 33 deletions rest_framework_simplejwt/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ def get_user(self, validated_token: Token) -> AuthUser:
except self.user_model.DoesNotExist:
raise AuthenticationFailed(_("User not found"), code="user_not_found")

# Ensure authentication rule passes
if not api_settings.USER_AUTHENTICATION_RULE(user):
if api_settings.CHECK_USER_IS_ACTIVE and not user.is_active:
raise AuthenticationFailed(_("User is inactive"), code="user_inactive")

if api_settings.CHECK_REVOKE_TOKEN:
Expand Down Expand Up @@ -165,37 +164,6 @@ def get_user(self, validated_token: Token) -> AuthUser:
return api_settings.TOKEN_USER_CLASS(validated_token)


class JWTInactiveUserAuthentication(JWTAuthentication):
"""
An authentication plugin that authenticates requests through a JSON web
token provided in a request header, allowing inactive users to authenticate.
"""

def get_user(self, validated_token: Token) -> AuthUser:
"""
Attempts to find and return a user using the given validated token.
"""
try:
user_id = validated_token[api_settings.USER_ID_CLAIM]
except KeyError:
raise InvalidToken(_("Token contained no recognizable user identification"))

try:
user = self.user_model.objects.get(**{api_settings.USER_ID_FIELD: user_id})
except self.user_model.DoesNotExist:
raise AuthenticationFailed(_("User not found"), code="user_not_found")

if api_settings.CHECK_REVOKE_TOKEN:
if validated_token.get(
api_settings.REVOKE_TOKEN_CLAIM
) != get_md5_hash_password(user.password):
raise AuthenticationFailed(
_("The user's password has been changed."), code="password_changed"
)

return user


JWTTokenUserAuthentication = JWTStatelessUserAuthentication


Expand Down
27 changes: 0 additions & 27 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,30 +266,3 @@ def username(self):

# Restore default TokenUser for future tests
api_settings.TOKEN_USER_CLASS = temp


class TestJWTInactiveUserAuthentication(TestCase):
def setUp(self):
self.backend = authentication.JWTInactiveUserAuthentication()

def test_get_user(self):
payload = {"some_other_id": "foo"}

# Should raise error if no recognizable user identification
with self.assertRaises(InvalidToken):
self.backend.get_user(payload)

payload[api_settings.USER_ID_CLAIM] = 42

# Should raise exception if user not found
with self.assertRaises(AuthenticationFailed):
self.backend.get_user(payload)

u = User.objects.create_user(username="markhamill")
u.is_active = False
u.save()

payload[api_settings.USER_ID_CLAIM] = getattr(u, api_settings.USER_ID_FIELD)

# Otherwise, should return correct user
self.assertEqual(self.backend.get_user(payload).id, u.id)

0 comments on commit 8ab5c92

Please sign in to comment.