Skip to content

feat: reCAPTCHA implementation #Closes Implement reCAPTCHA on the Ion login page #1765

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

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from

Conversation

ElijahFeldman7
Copy link

Note : add RECAPTCHA_SECRET_KEY and RECAPTCHA_SITE_KEY to secret.py
Proposed changes

  • counts number of failed login attempts, and resets it when a correct login happens
  • boolean func to check if there has been >3 login attempts
  • if boolean is true then it calls reCAPTCHA
    Brief description of rationale
  • this helps stop bots from logging in a lot

@ElijahFeldman7 ElijahFeldman7 requested a review from a team as a code owner April 21, 2025 14:36
Copy link
Member

@alanzhu0 alanzhu0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Elijah, thanks for your PR. This implementation with the failed logins wasn't exactly what I had in mind, but it's quite a good idea. Let's keep it in. But in addition, can you add the client-side invisible reCAPTCHA that is always active? https://developers.google.com/recaptcha/docs/invisible. This lets reCAPTCHA decide based on client indicators if a login is suspicious, and if so, it automatically activates when the login button is clicked.

@ElijahFeldman7
Copy link
Author

okay, check out my new commit. you will have to create 2 individual applications for invisible and checkbox recaptcha. Let me know if you find any problems.

Copy link
Member

@alanzhu0 alanzhu0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, thanks Elijah. Please see a few comments on the code. Can you make some reCAPTCHA keys on your own in your Google account to test this out, and show me in an 8th period? I finally read the reCAPTCHA documentation so I actually understand this now, sorry about my uncertainty earlier today. And you are right you will need 2 different keys.

Once we finish the testing I will ask for all the keys for production use. I guess I didn't provide the secret key either.

Also, please get the CI checks to pass using pre-commit, and squash your commits into one.

auth_form=form,
added_context={"auth_message": "reCAPTCHA verification failed. Please try again."},
)
except requests.exceptions.RequestException as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it fails to connect to reCAPTCHA, let's let the login proceed without it, just in case something weird is going on we don't want to lock out everyone. It should send an email to [email protected] though saying reCAPTCHA can't be reached, and also possibly send a warning error after logging in saying that recaptcha couldn't be reached.

data={
"secret": secret_key_to_use,
"response": recaptcha_response,
"remoteip": request.META.get("REMOTE_ADDR"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do this:

if "HTTP_X_REAL_IP" in request.META:
            ip = request.META["HTTP_X_REAL_IP"]
else:
            ip = (request.META.get("REMOTE_ADDR", ""),)

if isinstance(ip, set):
            ip = ip[0]            

(from: https://github.com/tjcsl/ion/blob/dev/intranet/middleware/access_log.py)

if you want you could abstract this to a helper function and replace all instances in Ion accessing the IP with that helper function

this is needed because Ion is behind a reverse proxy

@@ -32,6 +32,17 @@

{% block js %}
{{ block.super }}
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if JS is disabled? does it block the user from logging in? I think that would be the best way to go

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, it does because the backend checks for g-recaptcha thing right

@alanzhu0
Copy link
Member

alanzhu0 commented May 7, 2025

Any updates on this @ElijahFeldman7 ?

@ElijahFeldman7
Copy link
Author

I’ve been busy the past few weeks but I can start implementing the changes and testing next week.

@alanzhu0
Copy link
Member

alanzhu0 commented May 7, 2025

Sure, no rush

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.

2 participants