【发布时间】:2021-01-12 09:48:24
【问题描述】:
当用户验证 OTP 时,我正在生成 token。但是当我验证标头中的令牌时,我得到了Invalid payload。
如果对我收到此错误的原因有任何帮助,我们将不胜感激。
serializers.py:
class OTPVerifyForResetPasswordAPIView(APIView):
permission_classes = (AllowAny,)
def post(self,request,*args,**kwargs):
data = request.data
user = request.user
print(user)
phone_number = request.data['phone_number']
country_code = request.data['country_code']
verification_code = request.data['verification_code']
if phone_number and country_code and verification_code:
obj_qs = User.objects.filter(phone_number__iexact = phone_number,country_code__iexact = country_code)
obj = ''
if obj_qs.exists() and obj_qs.count() ==1:
user_obj = obj_qs.first()
#Development
if verification_code == '1234':
payload = jwt_payload_handler(user_obj)
token = jwt_encode_handler(payload)
token = 'JWT '+str(token)
return Response({
'success' : 'True',
'message' : 'Your mobile number verified successfully',
'data' : {
'phone_number' : user_obj.phone_number,
'country_code' : user_obj.country_code,
'token' : token,
}
},status=HTTP_200_OK)
else:
##some logic....
else:
## some logic...
【问题讨论】:
-
你的用户模型有
username吗? -
是的,我有@GProst
-
如果您编写了自定义有效负载处理程序,最好显示代码
-
我不确定如何解决您的问题,但我注意到
if verification_code == '1234'行容易受到timing attack 的攻击。我建议使用 django 的实用函数django.utils.crypto.constant_time_compare来解决这个问题。
标签: python django django-rest-framework jwt