From 4c7ec02e527b74703259b49715a5100259523038 Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Wed, 27 Jun 2018 10:17:07 +0800 Subject: [PATCH] Add support to customize `RECAPTCHA_VERIFY_SERVER` and `RECAPTCHA_SCRIPT` --- docs/config.rst | 24 ++++++++++++++---------- examples/recaptcha/app.py | 3 +++ flask_wtf/recaptcha/validators.py | 6 ++++-- flask_wtf/recaptcha/widgets.py | 5 +++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index c3e09299..c9a29091 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -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 ------- diff --git a/examples/recaptcha/app.py b/examples/recaptcha/app.py index e2bda74b..a176a245 100644 --- a/examples/recaptcha/app.py +++ b/examples/recaptcha/app.py @@ -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__) diff --git a/flask_wtf/recaptcha/validators.py b/flask_wtf/recaptcha/validators.py index 0fa00b04..71459e95 100644 --- a/flask_wtf/recaptcha/validators.py +++ b/flask_wtf/recaptcha/validators.py @@ -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.', @@ -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 diff --git a/flask_wtf/recaptcha/widgets.py b/flask_wtf/recaptcha/widgets.py index 229537f4..5580dc9d 100644 --- a/flask_wtf/recaptcha/widgets.py +++ b/flask_wtf/recaptcha/widgets.py @@ -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'''
@@ -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)