简体   繁体   English

JWT 使用 Django REST 框架进行身份验证,使用 otp 获取 Z8A5DA52ED1264471D35AZE70C08A

[英]JWT Authentication with Django REST Framework using otp for getting api tokens

I have a custom user login where, I use mobile OTP verification and not at all using any django user model through out my project.need to authenticate jwt django restframework by otp. I have a custom user login where, I use mobile OTP verification and not at all using any django user model through out my project.need to authenticate jwt django restframework by otp. please help me with this.请帮我解决一下这个。 thanks谢谢

First send otp and save this in db.首先发送otp并将其保存在数据库中。

class LoginView(APIView):
    def post(self, request, format=None):
        data = request.data
        response = Response()       
        username = data.get('username', None)
        password = data.get('password', None)
        user = authenticate(username=username,password=password)
        if user is not None:
            if user.is_active:
                if user.two_step_verification:

                    GENERATE OTP HERE AND SAVE THIS IN USER MODEL...

                    user.otp = 'YOUR OTP'
                    user.save(update_fields=['otp',]) 
                    
                    SEND OTP HERE...                       
             
                    return Response({"send":"Two step verification OTP successfully send!!!"},status = status.HTTP_200_OK) 
            else:
                return Response({"No active" : "This account is not active!!"},status=status.HTTP_404_NOT_FOUND)
        else:
            return Response({"Invalid" : "Invalid username or password!!"},status=status.HTTP_404_NOT_FOUND)

Then verify this.然后验证这一点。 Here I use rest_framework_simplejwt这里我使用rest_framework_simplejwt

from rest_framework_simplejwt.tokens import RefreshToken

@api_view(['POST'])
@permission_classes([AllowAny,])
def two_step_otp_Verify(request,otp):
    try:
        user = User.objects.get(otp = otp,is_active = True)
        verify = 'VERIFY YOUR OTP HERE'
        if verify:
            response = Response()
            user.otp = None
            user.last_login = timezone.now()
            user.save()
            refresh = RefreshToken.for_user(user)
            
            response.set_signed_cookie(
                       key = 'ACCESS_TOKEN', 
                       value = str(refresh.access_token),
                       .....
                       )
            #ORRRRRRRRRRRRRRRRRRRRRRR
            login(request, user)
            
            response.data = {"Success" : "Login successfully"}
            return response
        else:
            return Response({"Time out" : "Given otp is expired!!"}, status=status.HTTP_408_REQUEST_TIMEOUT)
    except:
        return Response({"No User" : "Invalid otp OR No any active user found for given otp"}, status=status.HTTP_400_BAD_REQUEST)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM