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.js'
API_SECURE_URL =
'https://www.google.com/recaptcha/api.js'
VERIFY_URL =
'https://www.google.com/recaptcha/api/siteverify'
RESPONSE_FIELD =
'g-recaptcha-response'

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
# File 'lib/rack/recaptcha.rb', line 40

def _call(env)
  request = Request.new(env)
  if request.params[RESPONSE_FIELD]
    value, msg = verify(
      request.ip,
      request.params[RESPONSE_FIELD]
    )
    env.merge!('recaptcha.valid' => value, '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, response) ⇒ Object



52
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
79
# File 'lib/rack/recaptcha.rb', line 52

def verify(ip, response)
  params = {
    'secret' => Rack::Recaptcha.private_key,
    'remoteip'   => ip,
    '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, :use_ssl => true)
  else
    http = Net::HTTP.start(uri.host, uri.port, :use_ssl => true)
  end

  request           = Net::HTTP::Post.new(uri.path)
  request.form_data = params
  response          = http.request(request)

  parsed_response = JSON.parse(response.body)
  success = parsed_response['success']
  error_messages = parsed_response['error-codes'] || []
  return [success, error_messages.join(',')]
end