Skip to content
Permalink
Browse files

Merge pull request #145 from lipis/patch-2

Updated reCAPTCHA code to the latest version
  • Loading branch information...
lepture committed Aug 26, 2014
2 parents c3717f3 + fd9c87f commit 182f98a01c0ccc234bfe90afef9f88820ce9694a
Showing with 9 additions and 24 deletions.
  1. +0 −2 docs/form.rst
  2. +8 −17 flask_wtf/recaptcha/widgets.py
  3. +1 −5 tests/test_recaptcha.py
@@ -131,8 +131,6 @@ This comes together with a number of configuration, which you have to
implement them.

===================== ==============================================
RECAPTCHA_USE_SSL Enable/disable recaptcha through ssl.
Default is False.
RECAPTCHA_PUBLIC_KEY **required** A public key.
RECAPTCHA_PRIVATE_KEY **required** A private key.
RECAPTCHA_OPTIONS **optional** A dict of configuration options.
@@ -18,18 +18,14 @@ def default(self, o):
_JSONEncoder = JSONEncoder


RECAPTCHA_API_SERVER = 'http://api.recaptcha.net/'
RECAPTCHA_SSL_API_SERVER = 'https://www.google.com/recaptcha/api/'
RECAPTCHA_API_SERVER = '//www.google.com/recaptcha/api/'
RECAPTCHA_HTML = u'''
<script type="text/javascript">var RecaptchaOptions = %(options)s;</script>
<script type="text/javascript" src="%(script_url)s"></script>
<noscript>
<div><iframe src="%(frame_url)s" height="300" width="500"></iframe></div>
<div>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge" />
</div>
<iframe src="%(frame_url)s" height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
</noscript>
'''

@@ -38,22 +34,17 @@ def default(self, o):

class RecaptchaWidget(object):

def recaptcha_html(self, server, query, options):
def recaptcha_html(self, query, options):
html = current_app.config.get('RECAPTCHA_HTML', RECAPTCHA_HTML)
return Markup(html % dict(
script_url='%schallenge?%s' % (server, query),
frame_url='%snoscript?%s' % (server, query),
script_url='%schallenge?%s' % (RECAPTCHA_API_SERVER, query),
frame_url='%snoscript?%s' % (RECAPTCHA_API_SERVER, query),
options=json.dumps(options, cls=_JSONEncoder)
))

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

if current_app.config.get('RECAPTCHA_USE_SSL', False):
server = RECAPTCHA_SSL_API_SERVER
else:
server = RECAPTCHA_API_SERVER

try:
public_key = current_app.config['RECAPTCHA_PUBLIC_KEY']
except KeyError:
@@ -84,4 +75,4 @@ def __call__(self, field, error=None, **kwargs):

options.update(current_app.config.get('RECAPTCHA_OPTIONS', {}))

return self.recaptcha_html(server, query, options)
return self.recaptcha_html(query, options)
@@ -32,12 +32,8 @@ def inex():

def test_recaptcha(self):
response = self.client.get('/')
assert b'http://api.recaptcha.net' in response.data
assert b'//www.google.com/recaptcha/api/' in response.data

def test_ssl_recaptcha(self):
self.app.config['RECAPTCHA_USE_SSL'] = True
response = self.client.get('/')
assert b'https://www.google.com/recaptcha/api/' in response.data

def test_invalid_recaptcha(self):
response = self.client.post('/', data={})

0 comments on commit 182f98a

Please sign in to comment.
You can’t perform that action at this time.