made JWT_TOKEN_CLAIMS_SERIALIZER setting value a callable string rather than a function

This commit is contained in:
alichass 2020-06-14 19:15:22 -04:00 committed by Michael
parent 0722ec4aee
commit 1dce781942
2 changed files with 13 additions and 15 deletions

View File

@ -21,6 +21,16 @@ except ImportError:
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from jwt import decode as decode_jwt from jwt import decode as decode_jwt
class TESTTokenObtainPairSerializer(TokenObtainPairSerializer):
@classmethod
def get_token(cls, user):
token = super().get_token(user)
# Add custom claims
token['name'] = user.username
token['email'] = user.email
return token
@override_settings(ROOT_URLCONF="tests.urls") @override_settings(ROOT_URLCONF="tests.urls")
class APIBasicTests(TestsMixin, TestCase): class APIBasicTests(TestsMixin, TestCase):
@ -610,18 +620,6 @@ class APIBasicTests(TestsMixin, TestCase):
self.assertEqual(resp.status_code, 500) self.assertEqual(resp.status_code, 500)
class TESTTokenObtainPairSerializer(TokenObtainPairSerializer):
@classmethod
def get_token(cls, user):
token = super().get_token(user)
# Add custom claims
token['name'] = user.username
token['email'] = user.email
return token
@override_settings(REST_USE_JWT=True) @override_settings(REST_USE_JWT=True)
@override_settings(JWT_AUTH_COOKIE=None) @override_settings(JWT_AUTH_COOKIE=None)
@override_settings(REST_FRAMEWORK=dict( @override_settings(REST_FRAMEWORK=dict(
@ -630,7 +628,7 @@ class APIBasicTests(TestsMixin, TestCase):
] ]
)) ))
@override_settings(REST_SESSION_LOGIN=False) @override_settings(REST_SESSION_LOGIN=False)
@override_settings(JWT_TOKEN_CLAIMS_SERIALIZER = TESTTokenObtainPairSerializer) @override_settings(JWT_TOKEN_CLAIMS_SERIALIZER = 'tests.test_api.TESTTokenObtainPairSerializer')
def test_custom_jwt_claims(self): def test_custom_jwt_claims(self):
payload = { payload = {
"username": self.USERNAME, "username": self.USERNAME,
@ -655,7 +653,7 @@ class APIBasicTests(TestsMixin, TestCase):
] ]
)) ))
@override_settings(REST_SESSION_LOGIN=False) @override_settings(REST_SESSION_LOGIN=False)
@override_settings(JWT_TOKEN_CLAIMS_SERIALIZER = TESTTokenObtainPairSerializer) @override_settings(JWT_TOKEN_CLAIMS_SERIALIZER = 'tests.test_api.TESTTokenObtainPairSerializer')
def test_custom_jwt_claims_cookie_w_authentication(self): def test_custom_jwt_claims_cookie_w_authentication(self):
payload = { payload = {
"username": self.USERNAME, "username": self.USERNAME,

View File

@ -21,7 +21,7 @@ try:
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
def jwt_encode(user): def jwt_encode(user):
TOPS = getattr(settings, 'JWT_TOKEN_CLAIMS_SERIALIZER', TokenObtainPairSerializer) TOPS = import_callable(getattr(settings, 'JWT_TOKEN_CLAIMS_SERIALIZER', TokenObtainPairSerializer))
refresh = TOPS.get_token(user) refresh = TOPS.get_token(user)
return refresh.access_token, refresh return refresh.access_token, refresh