Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use Django simple captcha but for rest of the framework? #228

Open
GianfrancoParionaMendoza opened this issue Feb 8, 2024 · 0 comments

Comments

@GianfrancoParionaMendoza
Copy link

GianfrancoParionaMendoza commented Feb 8, 2024

I managed to get the image to rest but not verify it, I don't know how to do it, any help.

class CaptchaAPIView(APIView):
    permission_classes = [AllowAny]

    def get(self, request, *args, **kwargs):
       
        captcha_key = CaptchaStore.generate_key()
        image_url = captcha_image_url(captcha_key)
        
        
        img_response = captcha_image(key=captcha_key, request=image_url)

      
        img_base64 = base64.b64encode(img_response.getvalue()).decode('utf-8')
        captcha_key = CaptchaStore.generate_key()
        
        request.session['captcha_key'] = captcha_key
        
        return Response({'image_url': image_url, 'captcha_key': captcha_key, 'img_base64': img_base64})
    def post(self, request, *args, **kwargs):
      
        user_response = request.data.get('captcha_response')
        print("user_response:", user_response)

       
        captcha_key = request.session.get('captcha_key')
        
      
        if captcha_key:
           
            hashed_user_response = hashlib.sha256(user_response.encode()).hexdigest()
            print("hashed",hashed_user_response)
            
           
            captcha = CaptchaStore.objects.filter(hashkey=captcha_key).first()
            
           
            if captcha and captcha.response == hashed_user_response:
              
                return HttpResponse('Captcha verification successful')
            else:
              
                return HttpResponse('Invalid captcha response', status=400)
        else:
           
            return HttpResponse('Captcha key not found in session', status=400)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant