Class: Rack::Recaptcha
- Inherits:
-
Object
- Object
- Rack::Recaptcha
- Defined in:
- lib/rack/recaptcha.rb,
lib/rack/recaptcha/helpers.rb
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- API_URL =
'http://www.google.com/recaptcha/api'
- API_SECURE_URL =
'https://www.google.com/recaptcha/api'
- VERIFY_URL =
'http://www.google.com/recaptcha/api/verify'
- CHALLENGE_FIELD =
'recaptcha_challenge_field'
- RESPONSE_FIELD =
'recaptcha_response_field'
Class Attribute Summary collapse
-
.private_key ⇒ Object
Returns the value of attribute private_key.
-
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
-
.proxy_password ⇒ Object
Returns the value of attribute proxy_password.
-
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
.proxy_user ⇒ Object
Returns the value of attribute proxy_user.
-
.public_key ⇒ Object
Returns the value of attribute public_key.
-
.test_mode ⇒ Object
Returns the value of attribute test_mode.
Class Method Summary collapse
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Recaptcha
constructor
Initialize the Rack Middleware.
- #verify(ip, challenge, response) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Recaptcha
Initialize the Rack Middleware. Some of the available options are:
:public_key -- your ReCaptcha API public key *(required)*
:private_key -- your ReCaptcha API private key *(required)*
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rack/recaptcha.rb', line 25 def initialize(app, = {}) @app = app @paths = [:paths] && [[:paths]].flatten.compact self.class.private_key = [:private_key] self.class.public_key = [:public_key] self.class.proxy_host = [:proxy_host] self.class.proxy_port = [:proxy_port] self.class.proxy_user = [:proxy_user] self.class.proxy_password = [:proxy_password] end |
Class Attribute Details
.private_key ⇒ Object
Returns the value of attribute private_key.
13 14 15 |
# File 'lib/rack/recaptcha.rb', line 13 def private_key @private_key end |
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
13 14 15 |
# File 'lib/rack/recaptcha.rb', line 13 def proxy_host @proxy_host end |
.proxy_password ⇒ Object
Returns the value of attribute proxy_password.
13 14 15 |
# File 'lib/rack/recaptcha.rb', line 13 def proxy_password @proxy_password end |
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
13 14 15 |
# File 'lib/rack/recaptcha.rb', line 13 def proxy_port @proxy_port end |
.proxy_user ⇒ Object
Returns the value of attribute proxy_user.
13 14 15 |
# File 'lib/rack/recaptcha.rb', line 13 def proxy_user @proxy_user end |
.public_key ⇒ Object
Returns the value of attribute public_key.
13 14 15 |
# File 'lib/rack/recaptcha.rb', line 13 def public_key @public_key end |
.test_mode ⇒ Object
Returns the value of attribute test_mode.
13 14 15 |
# File 'lib/rack/recaptcha.rb', line 13 def test_mode @test_mode end |
Class Method Details
.test_mode!(options = {}) ⇒ Object
15 16 17 18 |
# File 'lib/rack/recaptcha.rb', line 15 def test_mode!( = {}) value = [:return] self.test_mode = value.nil? ? true : [:return] end |
Instance Method Details
#_call(env) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rack/recaptcha.rb', line 40 def _call(env) request = Request.new(env) if request.params[CHALLENGE_FIELD] and request.params[RESPONSE_FIELD] value, msg = verify( request.ip, request.params[CHALLENGE_FIELD], request.params[RESPONSE_FIELD] ) env.merge!('recaptcha.valid' => value == 'true', 'recaptcha.msg' => msg) end @app.call(env) end |
#call(env) ⇒ Object
36 37 38 |
# File 'lib/rack/recaptcha.rb', line 36 def call(env) dup._call(env) end |
#verify(ip, challenge, response) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rack/recaptcha.rb', line 53 def verify(ip, challenge, response) params = { 'privatekey' => Rack::Recaptcha.private_key, 'remoteip' => ip, 'challenge' => challenge, 'response' => response } uri = URI.parse(VERIFY_URL) if self.class.proxy_host && self.class.proxy_port http = Net::HTTP.Proxy(self.class.proxy_host, self.class.proxy_port, self.class.proxy_user, self.class.proxy_password).start(uri.host, uri.port) else http = Net::HTTP.start(uri.host, uri.port) end request = Net::HTTP::Post.new(uri.path) request.form_data = params response = http.request(request) response.body.split("\n") end |