Mercurial > kallithea
view kallithea/lib/recaptcha.py @ 7239:44f7f73da4a6
recaptcha: Update to Google recaptcha API v2 (Issue #313)
Recaptcha stopped working. It was using google recaptcha v1, and
https://developers.google.com/recaptcha/docs/faq says: "Any calls to the v1 API
will not work after March 31, 2018."
Note:
1) Not using 'async defer' - it's not used anywhere else, plus I have no idea
of what exactly it does.
2) The recaptcha div has an ID so the label for="recaptcha_field" can reference
it (although I'm not certain a div can technically have a label, but it's
better than removing the label, or the for="").
author | Patrick Vane <patrick_vane@lowentry.com> |
---|---|
date | Tue, 03 Apr 2018 16:20:05 +0200 |
parents | 70262a0dca6b |
children | 0a277465fddf |
line wrap: on
line source
# -*- coding: utf-8 -*- import urllib import urllib2 import json class RecaptchaResponse(object): def __init__(self, is_valid, error_code=None): self.is_valid = is_valid self.error_code = error_code def __repr__(self): return '<RecaptchaResponse:%s>' % (self.is_valid) def submit(g_recaptcha_response, private_key, remoteip): """ Submits a reCAPTCHA request for verification. Returns RecaptchaResponse for the request g_recaptcha_response -- The value of g_recaptcha_response from the form private_key -- your reCAPTCHA private key remoteip -- the user's IP address """ if not (g_recaptcha_response and len(g_recaptcha_response)): return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol') def encode_if_necessary(s): if isinstance(s, unicode): return s.encode('utf-8') return s params = urllib.urlencode({ 'secret': encode_if_necessary(private_key), 'remoteip': encode_if_necessary(remoteip), 'response': encode_if_necessary(g_recaptcha_response), }) req = urllib2.Request( url="https://www.google.com/recaptcha/api/siteverify", data=params, headers={ "Content-type": "application/x-www-form-urlencoded", "User-agent": "reCAPTCHA Python" } ) httpresp = urllib2.urlopen(req) return_values = json.loads(httpresp.read()) httpresp.close() if not (isinstance(return_values, dict)): return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol') elif (("success" in return_values) and ((return_values["success"] == True) or (return_values["success"] == "true"))): return RecaptchaResponse(is_valid=True) elif (("error-codes" in return_values) and isinstance(return_values["error-codes"], list) and (len(return_values["error-codes"]) > 0)): return RecaptchaResponse(is_valid=False, error_code=return_values["error-codes"][0]) else: return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol')