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

Add support to customize RECAPTCHA_VERIFY_SERVER and RECAPTCHA_SCRIPT #342

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ Configuration
Recaptcha
---------

========================= ==============================================
``RECAPTCHA_PUBLIC_KEY`` **required** A public key.
``RECAPTCHA_PRIVATE_KEY`` **required** A private key.
https://www.google.com/recaptcha/admin
``RECAPTCHA_PARAMETERS`` **optional** A dict of configuration options.
``RECAPTCHA_HTML`` **optional** Override default HTML template
for Recaptcha.
``RECAPTCHA_DATA_ATTRS`` **optional** A dict of ``data-`` attrs to use
for Recaptcha div
========================= ==============================================
============================ ==============================================
``RECAPTCHA_PUBLIC_KEY`` **required** A public key.
``RECAPTCHA_PRIVATE_KEY`` **required** A private key.
https://www.google.com/recaptcha/admin
``RECAPTCHA_PARAMETERS`` **optional** A dict of configuration options.
``RECAPTCHA_HTML`` **optional** Override default HTML template
for Recaptcha.
``RECAPTCHA_VERIFY_SERVER`` **optional** Override default verify server
address for Recaptcha.
``RECAPTCHA_SCRIPT`` **optional** Override default recaptcha script
address for Recaptcha.
``RECAPTCHA_DATA_ATTRS`` **optional** A dict of ``data-`` attrs to use
for Recaptcha div
============================ ==============================================

Logging
-------
Expand Down
3 changes: 3 additions & 0 deletions examples/recaptcha/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

RECAPTCHA_PUBLIC_KEY = '6LeYIbsSAAAAACRPIllxA7wvXjIE411PfdB2gt2J'
RECAPTCHA_PRIVATE_KEY = '6LeYIbsSAAAAAJezaIq3Ft_hSTo0YtyeFG-JgRtu'
# If the default server is blocked, you can switch to the mirror server
# RECAPTCHA_VERIFY_SERVER = 'https://recaptcha.net/recaptcha/api/siteverify'
# RECAPTCHA_SCRIPT = 'https://recaptcha.net/recaptcha/api.js'

app = Flask(__name__)
app.config.from_object(__name__)
Expand Down
6 changes: 4 additions & 2 deletions flask_wtf/recaptcha/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from .._compat import to_bytes, to_unicode

RECAPTCHA_VERIFY_SERVER = 'https://www.google.com/recaptcha/api/siteverify'
DEFAULT_RECAPTCHA_VERIFY_SERVER = 'https://www.google.com/recaptcha/api/siteverify'
RECAPTCHA_ERROR_CODES = {
'missing-input-secret': 'The secret parameter is missing.',
'invalid-input-secret': 'The secret parameter is invalid or malformed.',
Expand Down Expand Up @@ -62,7 +62,9 @@ def _validate_recaptcha(self, response, remote_addr):
'response': response
})

http_response = http.urlopen(RECAPTCHA_VERIFY_SERVER, to_bytes(data))
recaptcha_verify_server = current_app.config.get(
'RECAPTCHA_VERIFY_SERVER', DEFAULT_RECAPTCHA_VERIFY_SERVER)
http_response = http.urlopen(recaptcha_verify_server, to_bytes(data))

if http_response.code != 200:
return False
Expand Down
5 changes: 3 additions & 2 deletions flask_wtf/recaptcha/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

JSONEncoder = json.JSONEncoder

RECAPTCHA_SCRIPT = u'https://www.google.com/recaptcha/api.js'
DEFAULT_RECAPTCHA_SCRIPT = u'https://www.google.com/recaptcha/api.js'
RECAPTCHA_TEMPLATE = u'''
<script src='%s' async defer></script>
<div class="g-recaptcha" %s></div>
Expand All @@ -21,7 +21,8 @@ def recaptcha_html(self, public_key):
if html:
return Markup(html)
params = current_app.config.get('RECAPTCHA_PARAMETERS')
script = RECAPTCHA_SCRIPT
script = current_app.config.get('RECAPTCHA_SCRIPT',
DEFAULT_RECAPTCHA_SCRIPT)
if params:
script += u'?' + url_encode(params)

Expand Down