diff --git a/rest_framework_simplejwt/authentication.py b/rest_framework_simplejwt/authentication.py index e4be0f95e..d9ebc164f 100644 --- a/rest_framework_simplejwt/authentication.py +++ b/rest_framework_simplejwt/authentication.py @@ -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: @@ -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 diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 5ff4b9a63..cb6c3dc3f 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -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)