|
10 | 10 | from rest_framework_simplejwt.models import TokenUser
|
11 | 11 | from rest_framework_simplejwt.settings import api_settings
|
12 | 12 | from rest_framework_simplejwt.tokens import AccessToken, SlidingToken
|
13 |
| -from rest_framework_simplejwt.utils import get_md5_hash_password |
| 13 | +from rest_framework_simplejwt.utils import _get_token_auth_hash, get_token_auth_hash |
14 | 14 |
|
15 | 15 | from .utils import override_api_settings
|
16 | 16 |
|
@@ -145,60 +145,47 @@ def test_get_user(self):
|
145 | 145 | with self.assertRaises(AuthenticationFailed):
|
146 | 146 | self.backend.get_user(payload)
|
147 | 147 |
|
148 |
| - u = User.objects.create_user(username="markhamill") |
149 |
| - u.is_active = False |
150 |
| - u.save() |
| 148 | + user = User.objects.create_user(username="markhamill", is_active=False) |
151 | 149 |
|
152 |
| - payload[api_settings.USER_ID_CLAIM] = getattr(u, api_settings.USER_ID_FIELD) |
| 150 | + payload[api_settings.USER_ID_CLAIM] = getattr(user, api_settings.USER_ID_FIELD) |
153 | 151 |
|
154 | 152 | # Should raise exception if user is inactive
|
155 | 153 | with self.assertRaises(AuthenticationFailed):
|
156 | 154 | self.backend.get_user(payload)
|
157 | 155 |
|
158 |
| - u.is_active = True |
159 |
| - u.save() |
| 156 | + user.is_active = True |
| 157 | + user.save() |
160 | 158 |
|
161 | 159 | # Otherwise, should return correct user
|
162 |
| - self.assertEqual(self.backend.get_user(payload).id, u.id) |
| 160 | + self.assertEqual(self.backend.get_user(payload).id, user.id) |
163 | 161 |
|
164 | 162 | @override_api_settings(
|
165 | 163 | CHECK_REVOKE_TOKEN=True, REVOKE_TOKEN_CLAIM="revoke_token_claim"
|
166 | 164 | )
|
167 | 165 | def test_get_user_with_check_revoke_token(self):
|
168 |
| - payload = {"some_other_id": "foo"} |
169 |
| - |
170 |
| - # Should raise error if no recognizable user identification |
171 |
| - with self.assertRaises(InvalidToken): |
172 |
| - self.backend.get_user(payload) |
173 |
| - |
174 |
| - payload[api_settings.USER_ID_CLAIM] = 42 |
175 |
| - |
176 |
| - # Should raise exception if user not found |
177 |
| - with self.assertRaises(AuthenticationFailed): |
178 |
| - self.backend.get_user(payload) |
179 |
| - |
180 |
| - u = User.objects.create_user(username="markhamill") |
181 |
| - u.is_active = False |
182 |
| - u.save() |
| 166 | + user = User.objects.create_user(username="markhamill") |
| 167 | + payload = { |
| 168 | + api_settings.USER_ID_CLAIM: getattr(user, api_settings.USER_ID_FIELD) |
| 169 | + } |
183 | 170 |
|
184 |
| - payload[api_settings.USER_ID_CLAIM] = getattr(u, api_settings.USER_ID_FIELD) |
185 |
| - |
186 |
| - # Should raise exception if user is inactive |
| 171 | + # Should raise exception if claim is missing |
187 | 172 | with self.assertRaises(AuthenticationFailed):
|
188 | 173 | self.backend.get_user(payload)
|
189 | 174 |
|
190 |
| - u.is_active = True |
191 |
| - u.save() |
192 |
| - |
193 |
| - # Should raise exception if hash password is different |
| 175 | + payload[api_settings.REVOKE_TOKEN_CLAIM] = "differenthash" |
| 176 | + # Should raise exception if claim is different |
194 | 177 | with self.assertRaises(AuthenticationFailed):
|
195 | 178 | self.backend.get_user(payload)
|
196 | 179 |
|
197 |
| - if api_settings.CHECK_REVOKE_TOKEN: |
198 |
| - payload[api_settings.REVOKE_TOKEN_CLAIM] = get_md5_hash_password(u.password) |
| 180 | + payload[api_settings.REVOKE_TOKEN_CLAIM] = _get_token_auth_hash( |
| 181 | + user, "other old not very secure secret" |
| 182 | + ) |
| 183 | + # Should return correct user if claim was signed with an old key |
| 184 | + self.assertEqual(self.backend.get_user(payload).id, user.id) |
199 | 185 |
|
| 186 | + payload[api_settings.REVOKE_TOKEN_CLAIM] = get_token_auth_hash(user) |
200 | 187 | # Otherwise, should return correct user
|
201 |
| - self.assertEqual(self.backend.get_user(payload).id, u.id) |
| 188 | + self.assertEqual(self.backend.get_user(payload).id, user.id) |
202 | 189 |
|
203 | 190 |
|
204 | 191 | class TestJWTStatelessUserAuthentication(TestCase):
|
|
0 commit comments