Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user_id type mismatch when user claim is not pk #851

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion rest_framework_simplejwt/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from uuid import uuid4

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractBaseUser
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -266,12 +267,17 @@ def blacklist(self) -> BlacklistedToken:
jti = self.payload[api_settings.JTI_CLAIM]
exp = self.payload["exp"]
user_id = self.payload.get(api_settings.USER_ID_CLAIM)
User = get_user_model()
try:
user = User.objects.get(**{api_settings.USER_ID_FIELD: user_id})
except User.DoesNotExist:
user = None

# Ensure outstanding token exists with given jti
token, _ = OutstandingToken.objects.get_or_create(
jti=jti,
defaults={
"user_id": user_id,
"user": user,
"created_at": self.current_time,
"token": str(self),
"expires_at": datetime_from_epoch(exp),
Expand Down
20 changes: 20 additions & 0 deletions tests/test_token_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ def test_outstanding_token_and_blacklisted_token_user(self):
outstanding_token = OutstandingToken.objects.get(token=token)
self.assertEqual(outstanding_token.user, self.user)

@override_api_settings(USER_ID_FIELD="email", USER_ID_CLAIM="email")
def test_outstanding_token_and_blacklisted_token_created_at_with_modified_user_id_field(
self,
):
token = RefreshToken.for_user(self.user)

token.blacklist()
outstanding_token = OutstandingToken.objects.get(token=token)
self.assertEqual(outstanding_token.created_at, token.current_time)

@override_api_settings(USER_ID_FIELD="email", USER_ID_CLAIM="email")
def test_outstanding_token_and_blacklisted_token_user_with_modifed_user_id_field(
self,
):
token = RefreshToken.for_user(self.user)

token.blacklist()
outstanding_token = OutstandingToken.objects.get(token=token)
self.assertEqual(outstanding_token.user, self.user)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also add a test which will ensure that the User.DoesNotExist error is properly handled when the user does not exist anymore?

E.g:

  1. create the token,
  2. delete self.user object with self.user.delete()
  3. call token.blacklist()
  4. assert that there is a BlacklistedToken record


class TestTokenBlacklistFlushExpiredTokens(TestCase):
def setUp(self):
Expand Down
Loading