Skip to content

Commit

Permalink
Make the token serializer configurable (jazzband#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
2ykwang authored Feb 2, 2022
1 parent 4feb37a commit ed0f7b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions rest_framework_simplejwt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
"SLIDING_TOKEN_REFRESH_EXP_CLAIM": "refresh_exp",
"SLIDING_TOKEN_LIFETIME": timedelta(minutes=5),
"SLIDING_TOKEN_REFRESH_LIFETIME": timedelta(days=1),
"TOKEN_OBTAIN_SERIALIZER": "rest_framework_simplejwt.serializers.TokenObtainPairSerializer",
"TOKEN_REFRESH_SERIALIZER": "rest_framework_simplejwt.serializers.TokenRefreshSerializer",
"TOKEN_VERIFY_SERIALIZER": "rest_framework_simplejwt.serializers.TokenVerifySerializer",
"TOKEN_BLACKLIST_SERIALIZER": "rest_framework_simplejwt.serializers.TokenBlacklistSerializer",
"SLIDING_TOKEN_OBTAIN_SERIALIZER": "rest_framework_simplejwt.serializers.TokenObtainSlidingSerializer",
"SLIDING_TOKEN_REFRESH_SERIALIZER": "rest_framework_simplejwt.serializers.TokenRefreshSlidingSerializer",
}

IMPORT_STRINGS = (
Expand Down
22 changes: 16 additions & 6 deletions rest_framework_simplejwt/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.utils.module_loading import import_string
from rest_framework import generics, status
from rest_framework.response import Response

from . import serializers
from .authentication import AUTH_HEADER_TYPES
from .exceptions import InvalidToken, TokenError
from .settings import api_settings


class TokenViewBase(generics.GenericAPIView):
Expand All @@ -14,6 +16,14 @@ class TokenViewBase(generics.GenericAPIView):

www_authenticate_realm = "api"

def get_serializer_class(self):
# Get the serializer from settings
try:
return import_string(self._serializer_class)
except ImportError:
msg = "Could not import serializer '%s'" % self._serializer_class
raise ImportError(msg)

def get_authenticate_header(self, request):
return '{} realm="{}"'.format(
AUTH_HEADER_TYPES[0],
Expand All @@ -37,7 +47,7 @@ class TokenObtainPairView(TokenViewBase):
token pair to prove the authentication of those credentials.
"""

serializer_class = serializers.TokenObtainPairSerializer
_serializer_class = api_settings.TOKEN_OBTAIN_SERIALIZER


token_obtain_pair = TokenObtainPairView.as_view()
Expand All @@ -49,7 +59,7 @@ class TokenRefreshView(TokenViewBase):
token if the refresh token is valid.
"""

serializer_class = serializers.TokenRefreshSerializer
_serializer_class = api_settings.TOKEN_REFRESH_SERIALIZER


token_refresh = TokenRefreshView.as_view()
Expand All @@ -61,7 +71,7 @@ class TokenObtainSlidingView(TokenViewBase):
prove the authentication of those credentials.
"""

serializer_class = serializers.TokenObtainSlidingSerializer
_serializer_class = api_settings.SLIDING_TOKEN_OBTAIN_SERIALIZER


token_obtain_sliding = TokenObtainSlidingView.as_view()
Expand All @@ -73,7 +83,7 @@ class TokenRefreshSlidingView(TokenViewBase):
token's refresh period has not expired.
"""

serializer_class = serializers.TokenRefreshSlidingSerializer
_serializer_class = api_settings.SLIDING_TOKEN_REFRESH_SERIALIZER


token_refresh_sliding = TokenRefreshSlidingView.as_view()
Expand All @@ -85,7 +95,7 @@ class TokenVerifyView(TokenViewBase):
information about a token's fitness for a particular use.
"""

serializer_class = serializers.TokenVerifySerializer
_serializer_class = api_settings.TOKEN_VERIFY_SERIALIZER


token_verify = TokenVerifyView.as_view()
Expand All @@ -97,7 +107,7 @@ class TokenBlacklistView(TokenViewBase):
`rest_framework_simplejwt.token_blacklist` app installed.
"""

serializer_class = serializers.TokenBlacklistSerializer
_serializer_class = api_settings.TOKEN_BLACKLIST_SERIALIZER


token_blacklist = TokenBlacklistView.as_view()

0 comments on commit ed0f7b6

Please sign in to comment.