Captacha

Captcha

Class that allows the integration of the application with the captcha provider.

class Captcha:
     @classmethod
     def verify_captcha(cls, captcha_response):
         captcha = models.CaptchaSettings.load()
         if captcha.provider == models.CaptchaSettings.HCAPTCHA:
             return cls.verify_hcaptcha(
                 captcha_response, captcha.secret_key, captcha.url_verify
             )

         if captcha.provider == models.CaptchaSettings.GOOGLE:
             return cls.verify_grcaptcha(
                 captcha_response, captcha.secret_key, captcha.url_verify
             )

         returnFalse

     @classmethod
     def verify_hcaptcha(self, captcha_response, key, url):
         params = {"secret": key, "response": captcha_response}

         try:
             response = requests.post(url, data=params)
             verify_rs = response.json()
             result = verify_rs.get("success", False)
         except Exception:
             result=False

         return result

     @classmethod
     def verify_grcaptcha(self, captcha_response, key, url):
         params = {"secret": key, "response": captcha_response}

         try:
             response = requests.post(url, data=params)
             verify_rs = response.json()
             result = verify_rs.get("success", False)
         except Exception:
             result=False

         return result

The methods are as follows:

  • verify_captcha: Method as entrypoint to verify the captcha token.

  • verify_hcaptcha: Method to verify the token with the HCaptcha provider.

  • verify_grcaptcha: Method to verify the token with the GrCaptcha provider.