|
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,21 +145,19 @@ 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_USER_IS_ACTIVE=False, |
@@ -190,40 +188,29 @@ def test_get_inactive_user(self): |
190 | 188 | CHECK_REVOKE_TOKEN=True, REVOKE_TOKEN_CLAIM="revoke_token_claim" |
191 | 189 | ) |
192 | 190 | def test_get_user_with_check_revoke_token(self): |
193 | | - payload = {"some_other_id": "foo"} |
194 | | - |
195 | | - # Should raise error if no recognizable user identification |
196 | | - with self.assertRaises(InvalidToken): |
197 | | - self.backend.get_user(payload) |
198 | | - |
199 | | - payload[api_settings.USER_ID_CLAIM] = 42 |
200 | | - |
201 | | - # Should raise exception if user not found |
202 | | - with self.assertRaises(AuthenticationFailed): |
203 | | - self.backend.get_user(payload) |
204 | | - |
205 | | - u = User.objects.create_user(username="markhamill") |
206 | | - u.is_active = False |
207 | | - u.save() |
| 191 | + user = User.objects.create_user(username="markhamill") |
| 192 | + payload = { |
| 193 | + api_settings.USER_ID_CLAIM: getattr(user, api_settings.USER_ID_FIELD) |
| 194 | + } |
208 | 195 |
|
209 | | - payload[api_settings.USER_ID_CLAIM] = getattr(u, api_settings.USER_ID_FIELD) |
210 | | - |
211 | | - # Should raise exception if user is inactive |
| 196 | + # Should raise exception if claim is missing |
212 | 197 | with self.assertRaises(AuthenticationFailed): |
213 | 198 | self.backend.get_user(payload) |
214 | 199 |
|
215 | | - u.is_active = True |
216 | | - u.save() |
217 | | - |
218 | | - # Should raise exception if hash password is different |
| 200 | + payload[api_settings.REVOKE_TOKEN_CLAIM] = "differenthash" |
| 201 | + # Should raise exception if claim is different |
219 | 202 | with self.assertRaises(AuthenticationFailed): |
220 | 203 | self.backend.get_user(payload) |
221 | 204 |
|
222 | | - if api_settings.CHECK_REVOKE_TOKEN: |
223 | | - payload[api_settings.REVOKE_TOKEN_CLAIM] = get_md5_hash_password(u.password) |
| 205 | + payload[api_settings.REVOKE_TOKEN_CLAIM] = _get_token_auth_hash( |
| 206 | + user, "other old not very secure secret" |
| 207 | + ) |
| 208 | + # Should return correct user if claim was signed with an old key |
| 209 | + self.assertEqual(self.backend.get_user(payload).id, user.id) |
224 | 210 |
|
| 211 | + payload[api_settings.REVOKE_TOKEN_CLAIM] = get_token_auth_hash(user) |
225 | 212 | # Otherwise, should return correct user |
226 | | - self.assertEqual(self.backend.get_user(payload).id, u.id) |
| 213 | + self.assertEqual(self.backend.get_user(payload).id, user.id) |
227 | 214 |
|
228 | 215 |
|
229 | 216 | class TestJWTStatelessUserAuthentication(TestCase): |
|
0 commit comments