From a1584b267bb329e52424e42d845316796f4e13e9 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 15 Sep 2017 14:55:12 +0200 Subject: [PATCH 1/3] Return decoded payload in `authenticate` This allows for accessing it on `request.auth` easily then. --- rest_framework_jwt/authentication.py | 2 +- tests/test_authentication.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/rest_framework_jwt/authentication.py b/rest_framework_jwt/authentication.py index 86b50b90..776ba422 100644 --- a/rest_framework_jwt/authentication.py +++ b/rest_framework_jwt/authentication.py @@ -42,7 +42,7 @@ def authenticate(self, request): user = self.authenticate_credentials(payload) - return (user, jwt_value) + return (user, payload) def authenticate_credentials(self, payload): """ diff --git a/tests/test_authentication.py b/tests/test_authentication.py index acc4d2f4..4876ba0a 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -29,6 +29,7 @@ from rest_framework.test import APIRequestFactory from rest_framework_jwt import utils +from rest_framework_jwt.authentication import JSONWebTokenAuthentication from rest_framework_jwt.compat import get_user_model from rest_framework_jwt.settings import DEFAULTS from rest_framework_jwt.settings import api_settings @@ -284,3 +285,17 @@ def test_post_form_failing_jwt_auth_different_auth_header_prefix(self): # Restore original settings api_settings.JWT_AUTH_HEADER_PREFIX = DEFAULTS['JWT_AUTH_HEADER_PREFIX'] + + def test_authenticate_returns_decoded_payload(self): + """ + Ensure `authenticate` returns the decoded payload, and not the + JWT value. + """ + payload = utils.jwt_payload_handler(self.user) + token = utils.jwt_encode_handler(payload) + auth = 'JWT {0}'.format(token) + request = factory.request(HTTP_AUTHORIZATION=auth) + (user, payload) = JSONWebTokenAuthentication().authenticate(request) + self.assertIsInstance(payload, dict) + self.assertEqual(set(payload.keys()), { + 'user_id', 'username', 'exp', 'email'}) From bbcd1a104d244266fffdae6b800540abdc926f78 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 22 Sep 2017 13:10:28 +0200 Subject: [PATCH 2/3] Better test (to squash) --- tests/test_authentication.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 4876ba0a..25d6d4be 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -64,6 +64,13 @@ def test_post_form_passing_jwt_auth(self): self.assertEqual(response.status_code, status.HTTP_200_OK) + # Ensure `authenticate` returned the decoded payload. + self.assertEqual(response.wsgi_request.user, self.user) + payload = response.wsgi_request.auth + self.assertIsInstance(payload, dict) + self.assertEqual(set(payload.keys()), { + 'user_id', 'username', 'exp', 'email'}) + def test_post_json_passing_jwt_auth(self): """ Ensure POSTing JSON over JWT auth with correct credentials @@ -285,17 +292,3 @@ def test_post_form_failing_jwt_auth_different_auth_header_prefix(self): # Restore original settings api_settings.JWT_AUTH_HEADER_PREFIX = DEFAULTS['JWT_AUTH_HEADER_PREFIX'] - - def test_authenticate_returns_decoded_payload(self): - """ - Ensure `authenticate` returns the decoded payload, and not the - JWT value. - """ - payload = utils.jwt_payload_handler(self.user) - token = utils.jwt_encode_handler(payload) - auth = 'JWT {0}'.format(token) - request = factory.request(HTTP_AUTHORIZATION=auth) - (user, payload) = JSONWebTokenAuthentication().authenticate(request) - self.assertIsInstance(payload, dict) - self.assertEqual(set(payload.keys()), { - 'user_id', 'username', 'exp', 'email'}) From 2fd879002af73ee4e56f138498ddf2844bee1f6d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 22 Sep 2017 13:53:46 +0200 Subject: [PATCH 3/3] fixup! Better test (to squash) --- tests/test_authentication.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 25d6d4be..626e05c6 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -29,7 +29,6 @@ from rest_framework.test import APIRequestFactory from rest_framework_jwt import utils -from rest_framework_jwt.authentication import JSONWebTokenAuthentication from rest_framework_jwt.compat import get_user_model from rest_framework_jwt.settings import DEFAULTS from rest_framework_jwt.settings import api_settings