##// END OF EJS Templates
py3: trivial renaming of unicode to str
py3: trivial renaming of unicode to str

File last commit:

r8081:620c13a3 default
r8081:620c13a3 default
Show More
recaptcha.py
59 lines | 2.1 KiB | text/x-python | PythonLexer
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 # -*- coding: utf-8 -*-
Mads Kiilerich
scripts: initial run of import cleanup using isort
r7718 import json
Mads Kiilerich
py3: migrate from urllib2 to urllib...
r8068 import urllib.parse
import urllib.request
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187
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)
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 def submit(g_recaptcha_response, private_key, remoteip):
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 """
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 Submits a reCAPTCHA request for verification. Returns RecaptchaResponse for the request
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 g_recaptcha_response -- The value of g_recaptcha_response from the form
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 private_key -- your reCAPTCHA private key
Mads Kiilerich
spelling: more consistent casing of 'IP'
r5125 remoteip -- the user's IP address
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 """
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 if not (g_recaptcha_response and len(g_recaptcha_response)):
return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol')
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187
def encode_if_necessary(s):
Mads Kiilerich
py3: trivial renaming of unicode to str
r8081 if isinstance(s, str):
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 return s.encode('utf-8')
return s
Mads Kiilerich
py3: migrate from urllib2 to urllib...
r8068 params = urllib.parse.urlencode({
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 'secret': encode_if_necessary(private_key),
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 'remoteip': encode_if_necessary(remoteip),
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 'response': encode_if_necessary(g_recaptcha_response),
Mads Kiilerich
py3: fix recaptcha Request parameter type...
r8046 }).encode('ascii')
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187
Mads Kiilerich
py3: migrate from urllib2 to urllib...
r8068 req = urllib.request.Request(
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 url="https://www.google.com/recaptcha/api/siteverify",
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 data=params,
headers={
"Content-type": "application/x-www-form-urlencoded",
"User-agent": "reCAPTCHA Python"
}
)
Mads Kiilerich
py3: migrate from urllib2 to urllib...
r8068 httpresp = urllib.request.urlopen(req)
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 return_values = json.loads(httpresp.read())
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 httpresp.close()
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 if not (isinstance(return_values, dict)):
return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol')
Mads Kiilerich
flake8: fix some E712 comparison to True should be 'if cond is True:' or 'if cond:'...
r7722 elif (("success" in return_values) and ((return_values["success"] is True) or (return_values["success"] == "true"))):
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 return RecaptchaResponse(is_valid=True)
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 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])
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 else:
Patrick Vane
recaptcha: Update to Google recaptcha API v2 (Issue #313)...
r7170 return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol')