Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TokenUser class to be configurable #172

Merged
merged 1 commit into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rest_framework_simplejwt/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@ def get_user(self, validated_token):
# identifier claim.
raise InvalidToken(_('Token contained no recognizable user identification'))

return TokenUser(validated_token)
return api_settings.TOKEN_USER_CLASS(validated_token)
2 changes: 2 additions & 0 deletions rest_framework_simplejwt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
'TOKEN_TYPE_CLAIM': 'token_type',

'JTI_CLAIM': 'jti',
'TOKEN_USER_CLASS': 'rest_framework_simplejwt.models.TokenUser',

'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
'SLIDING_TOKEN_LIFETIME': timedelta(minutes=5),
Expand All @@ -37,6 +38,7 @@

IMPORT_STRINGS = (
'AUTH_TOKEN_CLASSES',
'TOKEN_USER_CLASS',
)

REMOVED_SETTINGS = (
Expand Down
22 changes: 22 additions & 0 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,25 @@ def test_get_user(self):

self.assertIsInstance(user, TokenUser)
self.assertEqual(user.id, 42)

def test_custom_tokenuser(self):
from django.utils.functional import cached_property

class BobSaget(TokenUser):
@cached_property
def username(self):
return "bsaget"

temp = api_settings.TOKEN_USER_CLASS
api_settings.TOKEN_USER_CLASS = BobSaget

# Should return a token user object
payload = {api_settings.USER_ID_CLAIM: 42}
user = self.backend.get_user(payload)

self.assertIsInstance(user, api_settings.TOKEN_USER_CLASS)
self.assertEqual(user.id, 42)
self.assertEqual(user.username, "bsaget")

# Restore default TokenUser for future tests
api_settings.TOKEN_USER_CLASS = temp