Skip to content

Commit

Permalink
Fix the None value of the outstanding token of the blacklist (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaeHyuckSa authored May 30, 2024
1 parent 3b71b89 commit d66d246
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rest_framework_simplejwt/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,14 @@ 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)

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

def test_outstanding_token_and_blacklisted_token_created_at(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)

def test_outstanding_token_and_blacklisted_token_user(self):
token = RefreshToken.for_user(self.user)

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


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

0 comments on commit d66d246

Please sign in to comment.