gpt4 book ai didi

django-rest-auth - Django token 授权 - 通过覆盖获取验证 token 类自定义获取_jwt_token

转载 作者:行者123 更新时间:2023-12-03 22:32:01 25 4
gpt4 key购买 nike

我的目标是覆盖 obtain_jwt_token为了更好地控制程序的返回值,在doc我只发现了一个关于如何做到这一点的奇怪而粗略的信息:

Note that the default obtain_auth_token view explicitly uses JSON requests and responses, rather than using default renderer and parser classes in your settings. If you need a customized version of the obtain_auth_token view, you can do so by overriding the ObtainAuthToken view class, and using that in your url conf instead



至于现在,我的尝试是这样的:
urlpatterns = [
url(r'^api-token-auth/', my_customized_view),
]

class Foo(ObtainAuthToken):
def post(self):
# here goes my customized code

my_customized_view = Foo.as_view()

很有可能我的代码看起来很傻,我只是在谷歌上迷失了方向。我在 Djagno 方面的经验很少,所以请帮我解决这个问题!

最佳答案

我刚刚经历了同样的理解之旅,因为我希望返回用户并允许电子邮件或用户名登录。文档并不完全清楚,但如 auth token 所述,您可以对 JWT 执行相同的操作。
obtain_auth_token 即为获取授权 token ,如获取_jwt_token 为获取 JSONWebToken。
这是我覆盖的登录方法:

from rest_framework_jwt.settings import api_settings
from rest_framework_jwt.views import ObtainJSONWebToken

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
jwt_decode_handler = api_settings.JWT_DECODE_HANDLER


class LoginView(ObtainJSONWebToken):
def post(self, request, *args, **kwargs):
# by default attempts username / passsword combination
response = super(LoginView, self).post(request, *args, **kwargs)
# token = response.data['token'] # don't use this to prevent errors
# below will return null, but not an error, if not found :)
res = response.data
token = res.get('token')

# token ok, get user
if token:
user = jwt_decode_handler(token) # aleady json - don't serialize
else: # if none, try auth by email
req = request.data # try and find email in request
email = req.get('email')
password = req.get('password')
username = req.get('username')

if email is None or password is None:
return Response({'success': False,
'message': 'Missing or incorrect credentials',
'data': req},
status=status.HTTP_400_BAD_REQUEST)

# email exists in request, try to find user
try:
user = User.objects.get(email=email)
except:
return Response({'success': False,
'message': 'User not found',
'data': req},
status=status.HTTP_404_NOT_FOUND)

if not user.check_password(password):
return Response({'success': False,
'message': 'Incorrect password',
'data': req},
status=status.HTTP_403_FORBIDDEN)

# make token from user found by email
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
user = UserSerializer(user).data

return Response({'success': True,
'message': 'Successfully logged in',
'token': token,
'user': user},
status=status.HTTP_200_OK)

您可以通过自定义 Django 的身份验证模型将默认设置更改为仅通过电子邮件检查,但我很高兴同时拥有这两个选项。

我开始创建一个 api 样板。有一个 requirements.txt 文件和一个 config.example.py 文件,供任何想要下拉查看其余内容的人使用。
https://github.com/garyburgmann/django-api-boilerplate

关于django-rest-auth - Django token 授权 - 通过覆盖获取验证 token 类自定义获取_jwt_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42270058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com