Class: Rack::Recaptcha

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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,options = {})
  @app = app
  @paths = options[:paths] && [options[:paths]].flatten.compact
  self.class.private_key = options[:private_key]
  self.class.public_key = options[:public_key]
  self.class.proxy_host = options[:proxy_host]
  self.class.proxy_port = options[:proxy_port]
  self.class.proxy_user = options[:proxy_user]
  self.class.proxy_password = options[:proxy_password]
end

Class Attribute Details

.private_keyObject

Returns the value of attribute private_key.



13
14
15
# File 'lib/rack/recaptcha.rb', line 13

def private_key
  @private_key
end

.proxy_hostObject

Returns the value of attribute proxy_host.



13
14
15
# File 'lib/rack/recaptcha.rb', line 13

def proxy_host
  @proxy_host
end

.proxy_passwordObject

Returns the value of attribute proxy_password.



13
14
15
# File 'lib/rack/recaptcha.rb', line 13

def proxy_password
  @proxy_password
end

.proxy_portObject

Returns the value of attribute proxy_port.



13
14
15
# File 'lib/rack/recaptcha.rb', line 13

def proxy_port
  @proxy_port
end

.proxy_userObject

Returns the value of attribute proxy_user.



13
14
15
# File 'lib/rack/recaptcha.rb', line 13

def proxy_user
  @proxy_user
end

.public_keyObject

Returns the value of attribute public_key.



13
14
15
# File 'lib/rack/recaptcha.rb', line 13

def public_key
  @public_key
end

.test_modeObject

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!(options = {})
  value = options[:return]
  self.test_mode = value.nil? ? true : options[: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