Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Unified Diff: appengine/monorail/framework/captcha.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « appengine/monorail/framework/banned.py ('k') | appengine/monorail/framework/csp_report.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/monorail/framework/captcha.py
diff --git a/appengine/monorail/framework/captcha.py b/appengine/monorail/framework/captcha.py
new file mode 100644
index 0000000000000000000000000000000000000000..2aa9c19a143d904c7bf69781c165138f426ba362
--- /dev/null
+++ b/appengine/monorail/framework/captcha.py
@@ -0,0 +1,58 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is govered by a BSD-style
+# license that can be found in the LICENSE file or at
+# https://developers.google.com/open-source/licenses/bsd
+
+"""A simple python interface to ReCAPTCHA."""
+
+import json
+import logging
+import urllib
+import urllib2
+
+from services import secrets_svc
+
+def Verify(remote_ip, response):
+ """Check the user's guess at a captcha solution.
+
+ Args:
+ remote_ip: user's IP address.
+ challenge: The captcha challenge presented to the user.
+ guess: The user's response to the captcha.
+
+ Returns:
+ A pair (correct, error_msg) where error_msg will indicate
+ why a response was deemed incorrect. It is logged so that
+ you can see, e.g., if you have the wrong private key.
+ """
+ # If the use did not enter anything, that is always incorrect
+ if not response:
+ logging.info('response was blank')
+ return False, 'incorrect-captcha-sol'
+
+ resp = _AskRecaptcha(remote_ip, response)
+ if not resp['success']:
+ if 'error-codes' in resp:
+ return False, resp['error-codes']
+ else:
+ return False, 'incorrect-captcha-sol'
+
+ return True, ''
+
+def _AskRecaptcha(remote_ip, response):
+ """Ask the ReCAPTCHA backend to verify the user's guess."""
+ recaptcha_server_request = urllib2.Request(
+ url='https://www.google.com/recaptcha/api/siteverify',
+ data=urllib.urlencode({
+ 'secret': secrets_svc.GetRecaptchaPrivateKey(),
+ 'remoteip': remote_ip,
+ 'response': response}),
+ headers={
+ 'Content-type': 'application/x-www-form-urlencoded',
+ 'User-agent': 'reCAPTCHA Python'})
+ recaptcha_server_response = urllib2.urlopen(recaptcha_server_request)
+ resp = json.loads(recaptcha_server_response.read())
+ recaptcha_server_response.close()
+
+ return resp
+
« no previous file with comments | « appengine/monorail/framework/banned.py ('k') | appengine/monorail/framework/csp_report.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698