Skip to content

Commit

Permalink
[Updates📢] DRY (Do not Repeat Youself) up the code for reCAPTCHA vali…
Browse files Browse the repository at this point in the history
…dation across multiple routes. #10
  • Loading branch information
[esekyi] committed Oct 26, 2024
1 parent 1e1eecc commit 43cc0c2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/services/recaptcha_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Providing google recaptcha verification service."""
import requests
from flask import current_app


def verify_recaptcha(recaptcha_response):
"""Verifies the Google reCAPTCHA response.
Args:
recaptcha_response (str): The token received from the frontend reCAPTCHA.
Returns:
bool: True if the verification is successful, False otherwise.
"""
secret_key = current_app.config.get('RECAPTCHA_SECRET_KEY')
if not secret_key:
raise ValueError(
"RECAPTCHA_SECRET_KEY is missing in the configuration.")

data = {
'secret': secret_key,
'response': recaptcha_response
}
response = requests.post(
'https://www.google.com/recaptcha/api/siteverify', data=data)

if response.status_code == 200:
result = response.json()
return result.get('success', False)

return False

0 comments on commit 43cc0c2

Please sign in to comment.