【问题标题】:Token generated by django-restframework-simplejwt not validdjango-rest framework-simple jwt 生成的令牌无效
【发布时间】:2021-09-21 10:46:49
【问题描述】:

在我尝试向项目添加社交身份验证之前,这一切正常,现在当我发送带有令牌的验证电子邮件时,令牌验证器说令牌无效!

class ResendVerifyEmail(APIView):
    serializer_class = RegisterSerialzer
    def post(self, request):
        data = request.data
        # email = data.get('email')
        email = data['email']
        print(email)
        try:
            user = User.objects.get(email=email)
            # print('hello')
            if user.is_verified:
                return Response({'msg':'User is already verified','status':'status.HTTP_400_BAD_REQUEST'})
            print (user.username)
            token = RefreshToken.for_user(user).access_token
            current_site= get_current_site(request).domain
            relativeLink = reverse('email-verify')
            
            absurl = 'http://'+current_site+relativeLink+"?token="+str(token)
            email_body = 'Hi '+ user.username + ' this is the resent link to verify your email \n' + absurl

            data = {'email_body':email_body,'to_email':user.email,
                    'email_subject':'Verify your email'}
            Util.send_email(data)
            return Response({'msg':'The verification email has been sent','status':'status.HTTP_201_CREATED'}, status=status.HTTP_201_CREATED)
        except User.DoesNotExist:
            return Response({'msg':'No such user, register first','status':'status.HTTP_400_BAD_REQUEST'})


class VerifyEmail(APIView):
    serializer_class = EmailVerificationSerializer
    def get(self, request):
        token = request.GET.get('token')
        try:
            payload = jwt.decode(token, settings.SECRET_KEY)
            # print('decoded')
            user = User.objects.filter(id=payload['user_id']).first()
            # print(user)
            if user.is_verified:
                return Response({'msg':'User already verified!'}, status=status.HTTP_400_BAD_REQUEST)
            else:
                user.is_verified = True
                # user.is_authenticated = True
                user.is_active = True
                # if not user.is_verified:
                user.save()
                return Response({'email':'successfuly activated','status':'status.HTTP_200_OK'}, status=status.HTTP_200_OK)
        # except jwt.ExpiredSignatureError as identifier:
        except jwt.ExpiredSignatureError:
            return Response({'error':'Activation Expired','status':'status.HTTP_400_BAD_REQUEST'}, status=status.HTTP_400_BAD_REQUEST)
        # except jwt.exceptions.DecodeError as identifier:
        except jwt.exceptions.DecodeError:
            return Response({'error':'invalid token','status':'status.HTTP_400_BAD_REQUEST'}, status=status.HTTP_400_BAD_REQUEST)

另外,我已经在 settings.py 中添加了这些

AUTHENTICATION_BACKENDS = (
    'social_core.backends.facebook.FacebookOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

我得到'jwt.exceptions.DecodeError'

【问题讨论】:

  • 我强烈建议使用专用中间件进行 JWT 授权。它会为你处理解码令牌。

标签: django django-rest-framework django-rest-framework-simplejwt


【解决方案1】:

这完成了工作

payload = jwt.decode(token, settings.SECRET_KEY, algorithms='HS256')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-02
    • 2019-08-01
    • 2020-05-08
    • 2017-06-25
    • 2017-03-30
    • 2016-11-02
    • 2020-01-10
    • 2023-01-27
    相关资源
    最近更新 更多