-
-
Notifications
You must be signed in to change notification settings - Fork 308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use reCaptcha api v2 #164
Merged
Merged
Use reCaptcha api v2 #164
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
92c6e39
Use reCaptcha api v2
git-commit 3501ad8
PEP8: Just use one space
git-commit 2b67753
Renamed `from_bytes` to `to_unicode`
git-commit f2dc22e
Renamed function parameter of `to_unicode`
git-commit f5586b5
Removed `recaptcha_challenge_field` from tests
git-commit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,27 +7,23 @@ | |
from flask import request, current_app | ||
from wtforms import ValidationError | ||
from werkzeug import url_encode | ||
from .._compat import to_bytes | ||
from .._compat import to_bytes, from_bytes | ||
import json | ||
|
||
RECAPTCHA_VERIFY_SERVER = 'https://www.google.com/recaptcha/api/verify' | ||
RECAPTCHA_VERIFY_SERVER = 'https://www.google.com/recaptcha/api/siteverify' | ||
|
||
__all__ = ["Recaptcha"] | ||
|
||
|
||
class Recaptcha(object): | ||
|
||
"""Validates a ReCaptcha.""" | ||
|
||
_error_codes = { | ||
'invalid-site-public-key': 'The public key for reCAPTCHA is invalid', | ||
'invalid-site-private-key': 'The private key for reCAPTCHA is invalid', | ||
'invalid-referrer': ( | ||
'The public key for reCAPTCHA is not valid for ' | ||
'this domainin' | ||
), | ||
'verify-params-incorrect': ( | ||
'The parameters passed to reCAPTCHA ' | ||
'verification are incorrect' | ||
) | ||
'missing-input-secret': 'The secret parameter is missing.', | ||
'invalid-input-secret': 'The secret parameter is invalid or malformed.', | ||
'missing-input-response': 'The response parameter is missing.', | ||
'invalid-input-response': 'The response parameter is invalid or malformed.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just use one space. PEP8 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need babel to do the translation. |
||
} | ||
|
||
def __init__(self, message=u'Invalid word. Please try again.'): | ||
|
@@ -38,46 +34,42 @@ def __call__(self, form, field): | |
return True | ||
|
||
if request.json: | ||
challenge = request.json.get('recaptcha_challenge_field', '') | ||
response = request.json.get('recaptcha_response_field', '') | ||
response = request.json.get('g-recaptcha-response', '') | ||
else: | ||
challenge = request.form.get('recaptcha_challenge_field', '') | ||
response = request.form.get('recaptcha_response_field', '') | ||
response = request.form.get('g-recaptcha-response', '') | ||
remote_ip = request.remote_addr | ||
|
||
if not challenge or not response: | ||
if not response: | ||
raise ValidationError(field.gettext(self.message)) | ||
|
||
if not self._validate_recaptcha(challenge, response, remote_ip): | ||
if not self._validate_recaptcha(response, remote_ip): | ||
field.recaptcha_error = 'incorrect-captcha-sol' | ||
raise ValidationError(field.gettext(self.message)) | ||
|
||
def _validate_recaptcha(self, challenge, response, remote_addr): | ||
def _validate_recaptcha(self, response, remote_addr): | ||
"""Performs the actual validation.""" | ||
try: | ||
private_key = current_app.config['RECAPTCHA_PRIVATE_KEY'] | ||
except KeyError: | ||
raise RuntimeError("No RECAPTCHA_PRIVATE_KEY config set") | ||
|
||
data = url_encode({ | ||
'privatekey': private_key, | ||
'secret': private_key, | ||
'remoteip': remote_addr, | ||
'challenge': challenge, | ||
'response': response | ||
}) | ||
|
||
response = http.urlopen(RECAPTCHA_VERIFY_SERVER, to_bytes(data)) | ||
http_response = http.urlopen(RECAPTCHA_VERIFY_SERVER, to_bytes(data)) | ||
|
||
if response.code != 200: | ||
if http_response.code != 200: | ||
return False | ||
|
||
rv = [l.strip() for l in response.readlines()] | ||
json_resp = json.loads(from_bytes(http_response.read())) | ||
|
||
if rv and rv[0] == to_bytes('true'): | ||
if json_resp["success"]: | ||
return True | ||
|
||
if len(rv) > 1: | ||
error = rv[1] | ||
for error in json_resp["error-codes"]: | ||
if error in self._error_codes: | ||
raise RuntimeError(self._error_codes[error]) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd call it
to_unicode
.bytes
is a keyword, try another one.