bridgedb.txrecaptcha

Twisted-based reCAPTCHA client.

This client always uses TLS with strict hostname checking, unlike the official Google Python recaptcha-client, which is hardcoded to use plaintext HTTP.

Small portions of this code were taken from the official Google Python recaptcha-client module, version 1.0.6. Those portions are RecaptchaResponse, API_SERVER, They total 5 lines of code, which are copyright the authors of the recaptcha-client package.

Inheritance diagram of RecaptchaResponseError, RecaptchaResponse, RecaptchaResponseProtocol

API_SSL_SERVER = 'https://www.google.com/recaptcha/api'

This was taken from recaptcha.client.captcha.API_SSL_SERVER.

API_SERVER = 'https://www.google.com/recaptcha/api'

This was taken from recaptcha.client.captcha.API_SSL_SERVER.

GOOGLE_INTERNET_AUTHORITY_CA_CERT = <OpenSSL.crypto.X509 object>

(OpenSSL.crypto.X509) Only trust certificate for the reCAPTCHA API_SSL_SERVER which were signed by the Google Internet Authority CA.

exception RecaptchaResponseError[source]

Bases: exceptions.ValueError

There was an error with the reCaptcha API server’s response.

class RecaptchaResponse(is_valid, error_code=None)[source]

Bases: object

Taken from recaptcha.client.captcha.RecaptchaResponse.

class RecaptchaResponseProtocol(finished)[source]

Bases: twisted.internet.protocol.Protocol

HTML parser which creates a RecaptchaResponse from the body of the reCaptcha API server’s response.

Create a protocol for creating RecaptchaResponses.

Parameters:finished (twisted.internet.defer.Deferred) – A deferred which will have its callback() called with a RecaptchaResponse.
dataReceived(data)[source]

Called when some data is received from the connection.

connectionLost(reason)[source]

Called when the connection was closed.

Parameters:reason (twisted.python.failure.Failure) – A string explaning why the connection was closed, wrapped in a Failure instance.
submit(recaptcha_challenge_field, recaptcha_response_field, private_key, remoteip, agent=<twisted.web.client.Agent object>)[source]

Submits a reCaptcha request for verification. This function is a patched version of the recaptcha.client.captcha.submit() function in reCaptcha’s Python API.

It does two things differently:
  1. It uses Twisted for everything.
  2. It uses SSL/TLS for everything.

This function returns a twisted.internet.defer.Deferred. If you need a recaptcha.client.captcha.RecaptchaResponse to be returned, use the submit() function, which is an @inlineCallbacks wrapper for this function.

Parameters:
  • recaptcha_challenge_field (str) – The value of the HTTP POST recaptcha_challenge_field argument from the form.
  • recaptcha_response_field (str) – The value of the HTTP POST recaptcha_response_field argument from the form.
  • private_key (str) – The reCAPTCHA API private key.
  • remoteip (str) – An IP address to give to the reCaptcha API server.
Return type:

twisted.internet.defer.Deferred

Returns:

A Deferred which will callback with a recaptcha.RecaptchaResponse for the request.