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

Commit a1584b2

Browse files
committed
Return decoded payload in authenticate
This allows for accessing it on `request.auth` easily then.
1 parent 5c81e65 commit a1584b2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

rest_framework_jwt/authentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def authenticate(self, request):
4242

4343
user = self.authenticate_credentials(payload)
4444

45-
return (user, jwt_value)
45+
return (user, payload)
4646

4747
def authenticate_credentials(self, payload):
4848
"""

tests/test_authentication.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from rest_framework.test import APIRequestFactory
3030

3131
from rest_framework_jwt import utils
32+
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
3233
from rest_framework_jwt.compat import get_user_model
3334
from rest_framework_jwt.settings import DEFAULTS
3435
from rest_framework_jwt.settings import api_settings
@@ -284,3 +285,17 @@ def test_post_form_failing_jwt_auth_different_auth_header_prefix(self):
284285

285286
# Restore original settings
286287
api_settings.JWT_AUTH_HEADER_PREFIX = DEFAULTS['JWT_AUTH_HEADER_PREFIX']
288+
289+
def test_authenticate_returns_decoded_payload(self):
290+
"""
291+
Ensure `authenticate` returns the decoded payload, and not the
292+
JWT value.
293+
"""
294+
payload = utils.jwt_payload_handler(self.user)
295+
token = utils.jwt_encode_handler(payload)
296+
auth = 'JWT {0}'.format(token)
297+
request = factory.request(HTTP_AUTHORIZATION=auth)
298+
(user, payload) = JSONWebTokenAuthentication().authenticate(request)
299+
self.assertIsInstance(payload, dict)
300+
self.assertEqual(set(payload.keys()), {
301+
'user_id', 'username', 'exp', 'email'})

0 commit comments

Comments
 (0)