Django-oauth-toolkit : Customize Authenticate Response
I am new to Django OAuth Toolkit. I want to customize the authenticate response. My authenticate url configuration on django application is : url('authenticate/', include('oau
Solution 1:
I was able to make this change by overwriting the TokenView class in your views.py
from django.http import HttpResponse
from oauth2_provider.views.base import TokenView
from django.utils.decorators import method_decorator
from django.views.decorators.debug import sensitive_post_parameters
from oauth2_provider.models import get_access_token_model, get_application_model
from oauth2_provider.signals import app_authorized
import json
classCustomTokenView(TokenView):
@method_decorator(sensitive_post_parameters("password"))defpost(self, request, *args, **kwargs):
url, headers, body, status = self.create_token_response(request)
if status == 200:
body = json.loads(body)
access_token = body.get("access_token")
if access_token isnotNone:
token = get_access_token_model().objects.get(
token=access_token)
app_authorized.send(
sender=self, request=request,
token=token)
body['member'] = {
'id': token.user.id,
'username': token.user.username,
'email': token.user.email
}
body = json.dumps(body)
response = HttpResponse(content=body, status=status)
for k, v in headers.items():
response[k] = v
return response
In urls.py, just overwrite the token url by pointing to the custom view. This import should come before the include of the django-oauth-toolkit
url(r"authenticate/token/$", CustomTokenView.as_view(), name="token"),
url('authenticate/',
include('oauth2_provider.urls', namespace='oauth2_provider'))
The return will now contain the member data
{
"access_token": "YtiH9FGwAf7Cb814EjTKbv3FCpLtag",
"expires_in": 36000,
"token_type": "Bearer",
"scope": "read write groups",
"refresh_token": "99TyWmCwELrJvymT8m6Z9EPxGr3PJi",
"member": {
"id": 1,
"username": "admin",
"email": "admin@admin.com"
}
}
Solution 2:
Not sure how many people use drf_social_oauth2 but you can also do the same with that. Here is my solution overwriting the drf-social-oauth2 Token View
url(r"authenticate/token/$", CustomTokenView.as_view(), name="token"),
views.py
import json
from rest_framework.response import Response
from drf_social_oauth2.views import TokenView
from oauth2_provider.models import get_access_token_model, get_application_model
from oauth2_provider.signals import app_authorized
classCustomTokenView(TokenView):
defpost(self, request, *args, **kwargs):
mutable_data = request.data.copy()
request._request.POST = request._request.POST.copy()
for key, value in mutable_data.items():
request._request.POST[key] = value
url, headers, body, status = self.create_token_response(
request._request)
if status == 200:
body = json.loads(body)
access_token = body.get("access_token")
if access_token isnotNone:
token = get_access_token_model().objects.get(
token=access_token)
app_authorized.send(
sender=self, request=request,
token=token)
body['member'] = {
'id': token.user.id,
'username': token.user.username,
'email': token.user.email
}
body = json.dumps(body)
response = Response(data=json.loads(body), status=status)
for k, v in headers.items():
response[k] = v
return response
Post a Comment for "Django-oauth-toolkit : Customize Authenticate Response"