From 048bfaa4fa9208c92703b796cda96b062e7344ba Mon Sep 17 00:00:00 2001 From: SaJH Date: Tue, 7 May 2024 15:30:58 +0900 Subject: [PATCH] Fix the None value of the outstanding token of the blacklist --- rest_framework_simplejwt/tokens.py | 3 +++ tests/test_token_blacklist.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/rest_framework_simplejwt/tokens.py b/rest_framework_simplejwt/tokens.py index 1eeadcf25..9e9c3b9df 100644 --- a/rest_framework_simplejwt/tokens.py +++ b/rest_framework_simplejwt/tokens.py @@ -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), }, diff --git a/tests/test_token_blacklist.py b/tests/test_token_blacklist.py index b4d576b68..fc45adf2e 100644 --- a/tests/test_token_blacklist.py +++ b/tests/test_token_blacklist.py @@ -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):