【问题标题】:JWT token mismatch while OTP verificationOTP 验证时 JWT 令牌不匹配
【发布时间】: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


【解决方案1】:

我在django-rest-framework-jwt 中看到一个关于类似问题的未解决问题:https://github.com/jpadilla/django-rest-framework-jwt/issues/284 看起来这个库已经几年没有维护了,我可能会建议你切换到一些替代方案,他们建议使用这个:https://github.com/SimpleJWT/django-rest-framework-simplejwt

【讨论】:

    【解决方案2】:

    编辑带有user_obj = obj_qs.first()的行

    user_obj = {
        'username': obj_qs.first().username,
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 1970-01-01
      • 2017-07-27
      • 2019-10-20
      • 2016-01-17
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 2020-05-14
      相关资源
      最近更新 更多