Skip to content

Commit

Permalink
Added try/except to recaptcha response verification
Browse files Browse the repository at this point in the history
  • Loading branch information
johnyu95 committed Sep 19, 2023
1 parent c070b49 commit 05828c1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
21 changes: 13 additions & 8 deletions app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ def status():
def technical_support():
if request.method == 'POST':
if current_app.config['RECAPTCHA_ENABLED']:
# Verify recaptcha token and return error if failed
recaptcha_response = requests.post(
url='https://www.google.com/recaptcha/api/siteverify?secret={}&response={}'
.format(current_app.config["RECAPTCHA_PRIVATE_KEY"],
request.form["g-recaptcha-response"])).json()

if recaptcha_response['success'] is False or recaptcha_response['score'] < current_app.config[
"RECAPTCHA_THRESHOLD"]:
try:
# Verify recaptcha token and return error if failed
recaptcha_response = requests.post(
url='https://www.google.com/recaptcha/api/siteverify?secret={}&response={}'
.format(current_app.config["RECAPTCHA_PRIVATE_KEY"],
request.form["g-recaptcha-response"])).json()

if recaptcha_response['success'] is False or recaptcha_response['score'] < current_app.config[
"RECAPTCHA_THRESHOLD"]:
flash('Recaptcha failed, please try again.', category='danger')
render_template('main/contact.html')
except:
current_app.logger.exception("Recaptcha failed to get a response.")
flash('Recaptcha failed, please try again.', category='danger')
render_template('main/contact.html')

Expand Down
28 changes: 20 additions & 8 deletions app/request/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,26 @@ def new():

if flask_request.method == "POST":
if current_app.config['RECAPTCHA_ENABLED']:
# Verify recaptcha token and return error if failed
recaptcha_response = requests.post(
url='https://www.google.com/recaptcha/api/siteverify?secret={}&response={}'
.format(current_app.config["RECAPTCHA_PRIVATE_KEY"],
request.form["g-recaptcha-response"])).json()

if recaptcha_response['success'] is False or recaptcha_response['score'] < current_app.config[
"RECAPTCHA_THRESHOLD"]:
try:
# Verify recaptcha token and return error if failed
recaptcha_response = requests.post(
url='https://www.google.com/recaptcha/api/siteverify?secret={}&response={}'
.format(current_app.config["RECAPTCHA_PRIVATE_KEY"],
flask_request.form["g-recaptcha-response"])).json()

if recaptcha_response['success'] is False or recaptcha_response['score'] < current_app.config[
"RECAPTCHA_THRESHOLD"]:
flash('Recaptcha failed, please try again.', category='danger')
return render_template(
new_request_template,
form=form,
kiosk_mode=kiosk_mode,
category=category,
agency=agency,
title=title,
)
except:
current_app.logger.exception("Recaptcha failed to get a response.")
flash('Recaptcha failed, please try again.', category='danger')
return render_template(
new_request_template,
Expand Down
11 changes: 6 additions & 5 deletions app/templates/main/contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ <h1 class="text-center">Technical Support</h1>

<br>
{% if config['RECAPTCHA_ENABLED'] %}
<input id="submit"
class="g-recaptcha"
value="Send"
aria-label="Submit button"
<input id="send"
name="send"
type="submit"
value="Send"
aria-label="Send button"
class="g-recaptcha"
data-sitekey='{{ config['RECAPTCHA_PUBLIC_KEY'] }}'
data-callback='onSubmitTechnicalSupport'
data-action='technicalSupport'>
{% else %}
<input id="submit" name="submit" type="submit" value="Send" aria-label="Submit button">
<input id="send" name="send" type="submit" value="Send" aria-label="Send button">
{% endif %}
</form>
</div>
Expand Down

0 comments on commit 05828c1

Please sign in to comment.