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

Added support to choose between Google Recaptcha V2 and V3 #956

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ch-rigu
Copy link
Contributor

@ch-rigu ch-rigu commented Feb 26, 2025

Hello,

I've added support to choose between Google reCAPTCHA V2 and V3. To make a selection, you need to specify the correct keys and set the version (v2 or v3).

I made a small modification in auth.py because it was not stopping user login if the CAPTCHA was or incorrect. The now first checks for errors, including the reCAPTCHA challenge. If there is an error, it stops; if there are no errors, it will attempt to log in.

In recaptcha.py, I've added logic to automatically select either reCAPTCHA V2 or V3.

example:

common.py

from py4web.utils.recaptcha import ReCaptcha
#for recaptcha v3
recaptcha = ReCaptcha(settings.RECAPTCHA_API_KEY_V3, settings.RECAPTCHA_API_SECRET_V3, "v3")
or 
#for recaptcha v2
recaptcha = ReCaptcha(settings.RECAPTCHA_API_KEY_V2, settings.RECAPTCHA_API_SECRET_V2, "v2")

# in the section that auth is defined
auth.extra_form_fields = {"login": [recaptcha.field], "register": [recaptcha.field], "request_reset_password": [recaptcha.field], }



auth.enable(uses=(session, T, db, recaptcha.fixture), env=dict(T=T))

auth.html

  [[try:]]
  [[=form]]
  [[except:]]
  [[pass]]
  [[=recaptcha]]

A try/except block must be implemented when the captcha is enabled (at least for now) because, when the login is successful, it throws the error: NameError: name 'form' is not defined. Using a try/except works as a patch, and the login functions normally.

Greetings.
Chris.

Support for Recaptcha V2 and V3
Small fix to ensure that the reCAPTCHA stops the login process if the challenge is not passed. The current implementation allows login even if the CAPTCHA is empty or invalid.
added name to form in auth.
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

Successfully merging this pull request may close these issues.

1 participant