pythondjangodjango-rest-frameworkpython-social-authdjango-oauth

Django: Add data to JWT payload


I want to add data (such as the token expiration date or user info) to the payload of the JWT generated by this library.

The current decoded payload of a JWT generated by this library is the following:

{
  "token": "sXF6EE6jlZRmKhx1mgodOCdUMuSE1I"
}

And I want something like this

{
  "expiration_date": 1588329561
}

I don't have any serializer or view implemented, because the library manages the serializers and the views.

I only have to declare the following URL in the urls.py file:

urlpatterns = [
    ...,
    path('auth/', include('drf_social_oauth2.urls', namespace='drf')),
    ...,
]

and then I can make POST requests to generate or refresh the JWTs to auth/token/.

I've seen solutions (of people that use other libraries) that try to modify the library, and others that implement a serializer and a view, but since the library I'm using is in charge of this task, I don't know how to address this problem.

Note:


Solution

  • drf-social-oauth2 doesn't provide a mechanism to easily override this setting, it overrides oauth2_provider.settings.ACCESS_TOKEN_GENERATOR with their generate_token method (https://github.com/wagnerdelima/drf-social-oauth2/blob/master/drf_social_oauth2/settings.py#L11-L14), this method doesn't include extra values, only the token.

    You can do the same on your side overriding the value with a custom method that adds the needed keys.