-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Updates📢] DRY (Do not Repeat Youself) up the code for reCAPTCHA vali…
…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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |