Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit 63b1120

Browse files
author
Carlton Gibson
committed
Merge pull request #221 from PierreF/uuids_pk
Support for UUID for User PK
2 parents 00f76e4 + 630e0c9 commit 63b1120

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

rest_framework_jwt/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import jwt
2+
import uuid
23
import warnings
34
from calendar import timegm
45
from datetime import datetime
@@ -23,6 +24,8 @@ def jwt_payload_handler(user):
2324
'username': username,
2425
'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
2526
}
27+
if isinstance(user.pk, uuid.UUID):
28+
payload['user_id'] = str(user.pk)
2629

2730
payload[username_field] = username
2831

tests/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import uuid
12
from django.db import models
23
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
34

@@ -11,3 +12,15 @@ class CustomUser(AbstractBaseUser):
1112

1213
class Meta:
1314
app_label = 'tests'
15+
16+
17+
class CustomUserUUID(AbstractBaseUser):
18+
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
19+
email = models.EmailField(max_length=255, unique=True)
20+
21+
objects = BaseUserManager()
22+
23+
USERNAME_FIELD = 'email'
24+
25+
class Meta:
26+
app_label = 'tests'

tests/test_views.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,51 @@ def test_jwt_login_json_bad_creds(self):
205205
self.assertEqual(response.status_code, 400)
206206

207207

208+
@override_settings(AUTH_USER_MODEL='tests.CustomUserUUID')
209+
class CustomUserUUIDObtainJSONWebTokenTests(TestCase):
210+
"""JSON Web Token Authentication"""
211+
urls = 'tests.test_views'
212+
213+
def setUp(self):
214+
from .models import CustomUserUUID
215+
216+
self.email = '[email protected]'
217+
self.password = 'password'
218+
user = CustomUserUUID.objects.create(email=self.email)
219+
user.set_password(self.password)
220+
user.save()
221+
self.user = user
222+
223+
self.data = {
224+
'email': self.email,
225+
'password': self.password
226+
}
227+
228+
def test_jwt_login_json(self):
229+
"""
230+
Ensure JWT login view using JSON POST works.
231+
"""
232+
client = APIClient(enforce_csrf_checks=True)
233+
234+
response = client.post('/auth-token/', self.data, format='json')
235+
236+
self.assertEqual(response.status_code, status.HTTP_200_OK)
237+
decoded_payload = utils.jwt_decode_handler(response.data['token'])
238+
self.assertEqual(decoded_payload['user_id'], str(self.user.id))
239+
240+
def test_jwt_login_json_bad_creds(self):
241+
"""
242+
Ensure JWT login view using JSON POST fails
243+
if bad credentials are used.
244+
"""
245+
client = APIClient(enforce_csrf_checks=True)
246+
247+
self.data['password'] = 'wrong'
248+
response = client.post('/auth-token/', self.data, format='json')
249+
250+
self.assertEqual(response.status_code, 400)
251+
252+
208253
class TokenTestCase(BaseTestCase):
209254
"""
210255
Handlers for getting tokens from the API, or creating arbitrary ones.

0 commit comments

Comments
 (0)