Skip to content

Commit

Permalink
Update recaptcha configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Feb 4, 2015
1 parent 3c1f86b commit 65ab8c3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
14 changes: 10 additions & 4 deletions docs/form.rst
Expand Up @@ -130,13 +130,19 @@ Flask-WTF also provides Recaptcha support through a :class:`RecaptchaField`::
This comes together with a number of configuration, which you have to
implement them.

===================== ===============================================
===================== =========================================================
RECAPTCHA_PUBLIC_KEY **required** A public key.
RECAPTCHA_PRIVATE_KEY **required** A private key.
RECAPTCHA_API_SERVER **optional** Specify your Recaptcha API server.
RECAPTCHA_OPTIONS **optional** A dict of configuration options.
https://www.google.com/recaptcha/admin/create
===================== ===============================================
RECAPTCHA_PARAMETERS **optional** A dict of JavaScript (api.js) parameters.
RECAPTCHA_DATA_ATTRS **optional** A dict of data attributes options.
https://developers.google.com/recaptcha/docs/display
===================== ==========================================================

Example of RECAPTCHA_PARAMETERS, and RECAPTCHA_DATA_ATTRS::

RECAPTCHA_PARAMETERS = {'hl': 'zh', 'render': 'explicit'}
RECAPTCHA_DATA_ATTRS = {'theme': 'dark'}

For testing your application, if ``app.testing`` is ``True``, recaptcha
field will always be valid for you convenience.
Expand Down
21 changes: 10 additions & 11 deletions flask_wtf/recaptcha/validators.py
Expand Up @@ -11,24 +11,23 @@
import json

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.',
'missing-input-response': 'The response parameter is missing.',
'invalid-input-response': 'The response parameter is invalid or malformed.'
}


__all__ = ["Recaptcha"]


class Recaptcha(object):

"""Validates a ReCaptcha."""

_error_codes = {
'missing-input-secret': 'The secret parameter is missing.',
'invalid-input-secret': 'The secret parameter is invalid or malformed.',
'missing-input-response': 'The response parameter is missing.',
'invalid-input-response': 'The response parameter is invalid or malformed.',
}

def __init__(self, message=None):
if message is None:
message = self._error_codes['missing-input-response']
message = RECAPTCHA_ERROR_CODES['missing-input-response']
self.message = message

def __call__(self, form, field):
Expand Down Expand Up @@ -72,7 +71,7 @@ def _validate_recaptcha(self, response, remote_addr):
return True

for error in json_resp["error-codes"]:
if error in self._error_codes:
raise ValidationError(self._error_codes[error])
if error in RECAPTCHA_ERROR_CODES:
raise ValidationError(RECAPTCHA_ERROR_CODES[error])

return False
45 changes: 18 additions & 27 deletions flask_wtf/recaptcha/widgets.py
Expand Up @@ -2,30 +2,14 @@

from flask import current_app, Markup
from flask import json
from werkzeug import url_encode
JSONEncoder = json.JSONEncoder

RECAPTCHA_HTML = u'''
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="%(public_key)s"></div>
<noscript>
<div style="width: 302px; height: 352px;">
<div style="width: 302px; height: 352px; position: relative;">
<div style="width: 302px; height: 352px; position: absolute;">
<iframe src="https://www.google.com/recaptcha/api/fallback?k=%(public_key)s"
frameborder="0" scrolling="no"
style="width: 302px; height:352px; border: none;">
</iframe>
</div>
<div style="width: 250px; height: 80px; position: absolute; border: none;
bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;">
<textarea id="g-recaptcha-response" name="g-recaptcha-response"
class="g-recaptcha-response"
style="width: 250px; height: 80px; border: 1px solid #c1c1c1;
margin: 0px; padding: 0px; resize: none;"></textarea>
</div>
</div>
</div>
</noscript>
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>
'''

__all__ = ["RecaptchaWidget"]
Expand All @@ -34,11 +18,18 @@
class RecaptchaWidget(object):

def recaptcha_html(self, public_key):
html = current_app.config.get('RECAPTCHA_HTML', RECAPTCHA_HTML)

return Markup(html % dict(
public_key=public_key
))
html = current_app.config.get('RECAPTCHA_HTML')
if html:
return Markup(html)
params = current_app.config.get('RECAPTCHA_PARAMETERS')
script = RECAPTCHA_SCRIPT
if params:
script += u'?' + url_encode(params)

attrs = current_app.config.get('RECAPTCHA_DATA_ATTRS', {})
attrs['sitekey'] = public_key
snippet = u' '.join([u'data-%s="%s"' % (k, attrs[k]) for k in attrs])
return Markup(RECAPTCHA_TEMPLATE % (script, snippet))

def __call__(self, field, error=None, **kwargs):
"""Returns the recaptcha input HTML."""
Expand Down

0 comments on commit 65ab8c3

Please sign in to comment.