Skip to content

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

@GianfrancoParionaMendoza

Description

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions