Skip to content

Commit

Permalink
Merge pull request #63 from cdvv7788/develop
Browse files Browse the repository at this point in the history
Add support for nocaptcha recaptcha (recaptcha2)
  • Loading branch information
jpaidoussi committed Jan 13, 2015
2 parents 36c7dcc + e466a68 commit b20b39d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
32 changes: 27 additions & 5 deletions captcha/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
DEFAULT_API_SSL_SERVER = "//www.google.com/recaptcha/api" # made ssl agnostic
DEFAULT_API_SERVER = "//www.google.com/recaptcha/api" # made ssl agnostic
DEFAULT_VERIFY_SERVER = "www.google.com"
DEFAULT_WIDGET_TEMPLATE = 'captcha/widget.html'
if getattr(settings, "NOCAPTCHA", False):
DEFAULT_WIDGET_TEMPLATE = 'captcha/widget_nocaptcha.html'
else:
DEFAULT_WIDGET_TEMPLATE = 'captcha/widget.html'
DEFAULT_WIDGET_TEMPLATE_AJAX = 'captcha/widget_ajax.html'

API_SSL_SERVER = getattr(settings, "CAPTCHA_API_SSL_SERVER",
Expand Down Expand Up @@ -96,12 +99,19 @@ def submit(recaptcha_challenge_field,
error_code='incorrect-captcha-sol'
)

params = urlencode({
if getattr(settings, "NOCAPTCHA", False):
params = urlencode({
'secret': want_bytes(private_key),
'response': want_bytes(recaptcha_response_field),
'remoteip': want_bytes(remoteip),
})
else:
params = urlencode({
'privatekey': want_bytes(private_key),
'remoteip': want_bytes(remoteip),
'challenge': want_bytes(recaptcha_challenge_field),
'response': want_bytes(recaptcha_response_field),
})
})

if not PY2:
params = params.encode('utf-8')
Expand All @@ -111,6 +121,9 @@ def submit(recaptcha_challenge_field,
else:
verify_url = 'http://%s/recaptcha/api/verify' % VERIFY_SERVER

if getattr(settings, "NOCAPTCHA", False):
verify_url = 'https://%s/recaptcha/api/siteverify' % VERIFY_SERVER

req = Request(
url=verify_url,
data=params,
Expand All @@ -121,11 +134,20 @@ def submit(recaptcha_challenge_field,
)

httpresp = urlopen(req)
if getattr(settings, "NOCAPTCHA", False):
data = json.load(httpresp)
return_code = data['success']
return_values = [return_code, None]
if return_code:
return_code = 'true'
else:
return_code = 'false'
else:
return_values = httpresp.read().splitlines()
return_code = return_values[0]

return_values = httpresp.read().splitlines()
httpresp.close()

return_code = return_values[0]
if not PY2:
return_code = return_code.decode('utf-8')

Expand Down
33 changes: 33 additions & 0 deletions captcha/templates/captcha/widget_nocaptcha.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script src='https://www.google.com/recaptcha/api.js'></script>
<script type="text/javascript">
var DjangoRecaptchaOptions = {{options}};
if (typeof RecaptchaOptions !== 'object') {
RecaptchaOptions = DjangoRecaptchaOptions;
} else {
for (key in DjangoRecaptchaOptions) {
RecaptchaOptions[key] = DjangoRecaptchaOptions[key];
}
}
</script>
<div class="g-recaptcha" data-sitekey="{{ public_key }}"></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 }}"
frameborder="0" scrolling="no"
style="width: 302px; height:352px; border-style: none;">
</iframe>
</div>
<div style="width: 250px; height: 80px; position: absolute; border-style: none;
bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;">
<textarea id="g-recaptcha-response" name="g-recaptcha-response"
class="recaptcha_challenge_field"
style="width: 250px; height: 80px; border: 1px solid #c1c1c1;
margin: 0px; padding: 0px; resize: none;" value="">
</textarea>
<input type='hidden' name='recaptcha_response_field' value='manual_challenge' />
</div>
</div>
</div>
</noscript>
8 changes: 6 additions & 2 deletions captcha/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@


class ReCaptcha(forms.widgets.Widget):
recaptcha_challenge_name = 'recaptcha_challenge_field'
recaptcha_response_name = 'recaptcha_response_field'
if getattr(settings, "NOCAPTCHA", False):
recaptcha_response_name = 'g-recaptcha-response'
recaptcha_challenge_name = 'g-recaptcha-response'
else:
recaptcha_challenge_name = 'recaptcha_challenge_field'
recaptcha_response_name = 'recaptcha_response_field'

def __init__(self, public_key=None, use_ssl=None, attrs={}, *args,
**kwargs):
Expand Down

0 comments on commit b20b39d

Please sign in to comment.