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

Modify JWT Serializer Field Names #501

Merged
merged 5 commits into from
May 7, 2023
Merged

Conversation

Dresdn
Copy link
Contributor

@Dresdn Dresdn commented Apr 14, 2023

Closes #142

This change modifies the JWTSerializer fields to be access and response to align with the naming of rest_framework_jwt.serializers.TokenRefreshSerializer

With rest_framework_simplejwt being tightly coupled into the JWT usage, this made sense to adhere to the upstream dependency naming standards.

Ultimately, it's ugly when using JWT to check for access_token names when authenticating, and then using access for a refresh.

Copy link

@zackkh zackkh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greetings ! While you're at it, can you add refresh_expiration field to dj_rest_auth.jwt_auth.get_refresh_view's response data since we are already returning an access expiration timestamp ?

def get_refresh_view():
    """ Returns a Token Refresh CBV without a circular import """
    from rest_framework_simplejwt.settings import api_settings as jwt_settings
    from rest_framework_simplejwt.views import TokenRefreshView

    class RefreshViewWithCookieSupport(TokenRefreshView):
        serializer_class = CookieTokenRefreshSerializer

        def finalize_response(self, request, response, *args, **kwargs):
            if response.status_code == status.HTTP_200_OK and 'access' in response.data:
                set_jwt_access_cookie(response, response.data['access'])
                response.data['access_expiration'] = (timezone.now() + jwt_settings.ACCESS_TOKEN_LIFETIME)
            if response.status_code == status.HTTP_200_OK and 'refresh' in response.data:
                set_jwt_refresh_cookie(response, response.data['refresh'])
                response.data['refresh_expiration'] = (timezone.now() + jwt_settings.REFRESH_TOKEN_LIFETIME)
            return super().finalize_response(request, response, *args, **kwargs)
    return RefreshViewWithCookieSupport

@Dresdn
Copy link
Contributor Author

Dresdn commented Apr 15, 2023

Good call @zackkh. I saw that but didn't dawn on me to include it. Let me know if there's anything else to be done.

Copy link

@zackkh zackkh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are 5/5 and match up with djangorestframework-simplejwt.

@iMerica iMerica merged commit a174adb into iMerica:master May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Proposal] Change Login JWT payload "refresh_token" to "refresh"
3 participants