Module: Recaptcha

Defined in:
lib/recaptcha.rb,
lib/recaptcha/helpers.rb,
lib/recaptcha/railtie.rb,
lib/recaptcha/version.rb,
lib/recaptcha/configuration.rb,
lib/recaptcha/adapters/view_methods.rb,
lib/recaptcha/adapters/controller_methods.rb

Defined Under Namespace

Modules: Adapters, Helpers Classes: Configuration, Railtie, RecaptchaError, VerifyError

Constant Summary collapse

DEFAULT_TIMEOUT =
3
VERSION =
'5.15.0'

Class Method Summary collapse

Class Method Details

.action_valid?(action, expected_action) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'lib/recaptcha.rb', line 125

def self.action_valid?(action, expected_action)
  case expected_action
  when nil, FalseClass then true
  else action == expected_action.to_s
  end
end

.api_verification_enterprise(query_params, body, project_id, timeout: nil) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/recaptcha.rb', line 170

def self.api_verification_enterprise(query_params, body, project_id, timeout: nil)
  query = URI.encode_www_form(query_params)
  uri = URI.parse("#{configuration.verify_url}/#{project_id}/assessments?#{query}")
  http_instance = http_client_for(uri: uri, timeout: timeout)
  request = Net::HTTP::Post.new(uri.request_uri)
  request['Content-Type'] = 'application/json; charset=utf-8'
  request.body = JSON.generate(body)
  JSON.parse(http_instance.request(request).body)
end

.api_verification_free(verify_hash, timeout: nil, json: false) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/recaptcha.rb', line 155

def self.api_verification_free(verify_hash, timeout: nil, json: false)
  if json
    uri = URI.parse(configuration.verify_url)
    request = Net::HTTP::Post.new(uri.request_uri)
    request['Content-Type'] = 'application/json; charset=utf-8'
    request.body = JSON.generate(verify_hash)
  else
    query = URI.encode_www_form(verify_hash)
    uri = URI.parse("#{configuration.verify_url}?#{query}")
    request = Net::HTTP::Get.new(uri.request_uri)
  end
  http_instance = http_client_for(uri: uri, timeout: timeout)
  JSON.parse(http_instance.request(request).body)
end

.configurationObject

Gives access to the current Configuration.



25
26
27
# File 'lib/recaptcha.rb', line 25

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Allows easy setting of multiple configuration options. See Configuration for all available options. – The temp assignment is only used to get a nicer rdoc. Feel free to remove this hack. ++

Yields:

  • (config)


35
36
37
38
# File 'lib/recaptcha.rb', line 35

def self.configure
  config = configuration
  yield(config)
end

.hostname_valid?(hostname, validation) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
# File 'lib/recaptcha.rb', line 115

def self.hostname_valid?(hostname, validation)
  validation ||= configuration.hostname

  case validation
  when nil, FalseClass then true
  when String then validation == hostname
  else validation.call(hostname)
  end
end

.http_client_for(uri:, timeout: nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/recaptcha.rb', line 140

def self.http_client_for(uri:, timeout: nil)
  timeout ||= DEFAULT_TIMEOUT
  http = if configuration.proxy
    proxy_server = URI.parse(configuration.proxy)
    Net::HTTP::Proxy(proxy_server.host, proxy_server.port, proxy_server.user, proxy_server.password)
  else
    Net::HTTP
  end
  instance = http.new(uri.host, uri.port)
  instance.read_timeout = instance.open_timeout = timeout
  instance.use_ssl = true if uri.port == 443

  instance
end

.invalid_response?(resp) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/recaptcha.rb', line 57

def self.invalid_response?(resp)
  resp.empty? || resp.length > configuration.response_limit
end

.score_above_threshold?(score, minimum_score) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/recaptcha.rb', line 132

def self.score_above_threshold?(score, minimum_score)
  !minimum_score || (score && score >= minimum_score)
end

.score_below_threshold?(score, maximum_score) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/recaptcha.rb', line 136

def self.score_below_threshold?(score, maximum_score)
  !maximum_score || (score && score <= maximum_score)
end

.skip_env?(env) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/recaptcha.rb', line 53

def self.skip_env?(env)
  configuration.skip_verify_env.include?(env || configuration.default_env)
end

.verify_via_api_call(response, options) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/recaptcha.rb', line 61

def self.verify_via_api_call(response, options)
  if Recaptcha.configuration.enterprise
    verify_via_api_call_enterprise(response, options)
  else
    verify_via_api_call_free(response, options)
  end
end

.verify_via_api_call_enterprise(response, options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/recaptcha.rb', line 69

def self.verify_via_api_call_enterprise(response, options)
  site_key = options.fetch(:site_key) { configuration.site_key! }
  api_key = options.fetch(:enterprise_api_key) { configuration.enterprise_api_key! }
  project_id = options.fetch(:enterprise_project_id) { configuration.enterprise_project_id! }

  query_params = { 'key' => api_key }
  body = { 'event' => { 'token' => response, 'siteKey' => site_key } }
  body['event']['expectedAction'] = options[:action] if options.key?(:action)
  body['event']['userIpAddress'] = options[:remote_ip] if options.key?(:remote_ip)

  reply = api_verification_enterprise(query_params, body, project_id, timeout: options[:timeout])
  score = reply.dig('riskAnalysis', 'score')
  token_properties = reply['tokenProperties']
  success = !token_properties.nil? &&
    token_properties['valid'].to_s == 'true' &&
    hostname_valid?(token_properties['hostname'], options[:hostname]) &&
    action_valid?(token_properties['action'], options[:action]) &&
    score_above_threshold?(score, options[:minimum_score]) &&
    score_below_threshold?(score, options[:maximum_score])

  if options[:with_reply] == true
    [success, reply]
  else
    success
  end
end

.verify_via_api_call_free(response, options) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/recaptcha.rb', line 96

def self.verify_via_api_call_free(response, options)
  secret_key = options.fetch(:secret_key) { configuration.secret_key! }
  verify_hash = { 'secret' => secret_key, 'response' => response }
  verify_hash['remoteip'] = options[:remote_ip] if options.key?(:remote_ip)

  reply = api_verification_free(verify_hash, timeout: options[:timeout], json: options[:json])
  success = reply['success'].to_s == 'true' &&
    hostname_valid?(reply['hostname'], options[:hostname]) &&
    action_valid?(reply['action'], options[:action]) &&
    score_above_threshold?(reply['score'], options[:minimum_score]) &&
    score_below_threshold?(reply['score'], options[:maximum_score])

  if options[:with_reply] == true
    [success, reply]
  else
    success
  end
end

.with_configuration(config) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/recaptcha.rb', line 40

def self.with_configuration(config)
  original_config = {}

  config.each do |key, value|
    original_config[key] = configuration.send(key)
    configuration.send("#{key}=", value)
  end

  yield if block_given?
ensure
  original_config.each { |key, value| configuration.send("#{key}=", value) }
end