[英]django-oauth-toolkit : Customize authenticate response
I am new to Django OAuth Toolkit.我是 Django OAuth 工具包的新手。 I want to customize the authenticate response.我想自定义身份验证响应。
My authenticate url configuration on django application is:我在 django 应用程序上验证 url 配置是:
url('authenticate/',
include('oauth2_provider.urls', namespace='oauth2_provider'))
https://django-oauth-toolkit.readthedocs.io/en/latest/install.html https://django-oauth-toolkit.readthedocs.io/en/latest/install.html
Now, when i launch this command:现在,当我启动这个命令时:
curl -X POST -d 'grant_type=password&username=$username&password=$password'
-u "$client_id:$client_secret" http://127.0.0.1:8000/authenticate/token/
I get this response:我得到这样的回应:
{
"access_token": "ATiM10L0LNaldJPk12drXCjbhoeDR8",
"expires_in": 36000,
"refresh_token": "II4UBhXhpVDEKWmsUQxDzkj3OMjW1p",
"scope": "read groups write",
"token_type": "Bearer"
}
And would like this response:并希望得到这样的回应:
{
"access_token": "ATiM10L0LNaldJPk12drXCjbhoeDR8",
"expires_in": 36000,
"refresh_token": "II4UBhXhpVDEKWmsUQxDzkj3OMjW1p",
"scope": "read groups write",
"token_type": "Bearer",
"member": {
"id": 1,
"username": "username",
"email": "email@gmail.com",
....
}
}
I just want to override this response for add information of authenticated user.我只想覆盖此响应以添加经过身份验证的用户的信息。 I have read the documentation of django-oauth-toolkit.我已经阅读了 django-oauth-toolkit 的文档。 And i didn't find a solution to my problem...而且我没有找到解决我的问题的方法......
I was able to make this change by overwriting the TokenView class in your views.py我能够通过覆盖views.py 中的 TokenView 类来进行此更改
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_authorize
class CustomTokenView(TokenView):
@method_decorator(sensitive_post_parameters("password"))
def post(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 is not None:
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.在urls.py 中,只需通过指向自定义视图覆盖令牌 url。 This import should come before the include of the django-oauth-toolkit此导入应在包含 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"
}
}
Not sure how many people use drf_social_oauth2 but you can also do the same with that.不确定有多少人使用drf_social_oauth2但你也可以用它做同样的事情。 Here is my solution overwriting the drf-social-oauth2 Token View这是我覆盖drf-social-oauth2 令牌视图的解决方案
url(r"authenticate/token/$", CustomTokenView.as_view(), name="token"),
views.py视图.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
class CustomTokenView(TokenView):
def post(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 is not None:
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
This too can work这也可以工作
import json
from oauth2_provider.models import get_access_token_model
from oauth2_provider.views import TokenView as OAuth2TokenView
class TokenView(OAuth2TokenView):
def post(self, request, *args, **kwargs):
response = super().post(request, *args, **kwargs)
body = json.loads(response.content)
access_token = body.get("access_token")
token = get_access_token_model().objects.get(token=access_token)
body["member"] = {
"id": token.user.id,
"email": token.user.email,
"username": token.user.username,
}
response.content = json.dumps(body)
return response
and in urls.py
add并在urls.py
添加
path("o/token/", TokenView.as_view(), name="token"),
path("o/", include("oauth2_provider.urls", namespace="oauth2_provider")),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.