Module: Recaptcha::Verify

Defined in:
lib/recaptcha/verify.rb,
lib/recaptcha/verify/verify_result.rb,
lib/recaptcha/verify/verify_response.rb

Defined Under Namespace

Classes: MissingResponseTokenErrorResult, ResponseTokenTooLongErrorResult, TimeoutErrorResult, VerifyError, VerifyResponse, VerifyResult, VerifyResultError, VerifyResultV2, VerifyResultV3

Constant Summary collapse

G_RESPONSE_LIMIT =
4000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recaptcha_verify_resultObject (readonly)

Returns the value of attribute recaptcha_verify_result


37
38
39
# File 'lib/recaptcha/verify.rb', line 37

def recaptcha_verify_result
  @recaptcha_verify_result
end

Class Method Details

.get(request_hash, options) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/recaptcha/verify.rb', line 10

def get(request_hash, options)
  recaptcha_logger.debug %(Calling Recaptcha::Verify.get(#{request_hash.inspect}))
  http = if Recaptcha.configuration.proxy
    proxy_server = URI.parse(Recaptcha.configuration.proxy)
    Net::HTTP::Proxy(proxy_server.host, proxy_server.port, proxy_server.user, proxy_server.password)
  else
    Net::HTTP
  end
  query = URI.encode_www_form(request_hash)
  uri = URI.parse(Recaptcha.configuration.verify_url + '?' + query)
  http_instance = http.new(uri.host, uri.port)
  http_instance.read_timeout = http_instance.open_timeout = options[:timeout] || DEFAULT_TIMEOUT
  http_instance.use_ssl = true if uri.port == 443
  request = Net::HTTP::Get.new(uri.request_uri)
  http_response = http_instance.request(request)
  response = VerifyResponse.new(http_response)
  recaptcha_logger.debug %(Response JSON: #{response.json})
  response
end

.recaptcha_loggerObject


30
31
32
# File 'lib/recaptcha/verify.rb', line 30

def recaptcha_logger
  Recaptcha.configuration.logger
end

.skip?(env) ⇒ Boolean

Returns true if the given enviroment should be skipped according to config.skip_verify_env.


133
134
135
136
# File 'lib/recaptcha/verify.rb', line 133

def self.skip?(env)
  env ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || (Rails.env if defined? Rails.env)
  Recaptcha.configuration.skip_verify_env.include? env
end

Instance Method Details

#verify_recaptcha_v2(options = {}) ⇒ Boolean Also known as: verify_recaptcha

Verifies a response token using the reCAPTCHA v2 API


46
47
48
49
50
51
52
53
54
# File 'lib/recaptcha/verify.rb', line 46

def verify_recaptcha_v2(options = {})
  options = {model: options} unless options.is_a? Hash
  response_token = options[:response] || params['g-recaptcha-response'].to_s

  _verify_recaptcha(:v2, response_token, options.freeze) do
    response = recaptcha_verify_via_api_v2_call(request, response_token, options)
    VerifyResultV2.new(response, options[:hostname])
  end
end

#verify_recaptcha_v2!(options = {}) ⇒ Boolean Also known as: verify_recaptcha!

Same as #verify_recaptcha_v2 but raises an error if there is any failure to verify the response token and validate the response (including if the hostname didn't match).


82
83
84
# File 'lib/recaptcha/verify.rb', line 82

def verify_recaptcha_v2!(options = {})
  verify_recaptcha(options) || raise(VerifyError)
end

#verify_recaptcha_v2_checkbox(options = {}) ⇒ Boolean

This is the backend counterpart to the ClientHelper#recaptcha_v2_checkbox view helper.

Verifies a response token using the reCAPTCHA v2 API and the config.secret_key_v2_checkbox secret key.


65
66
67
68
69
# File 'lib/recaptcha/verify.rb', line 65

def verify_recaptcha_v2_checkbox(options = {})
  options = {model: options} unless options.is_a? Hash
  options[:secret_key] ||= Recaptcha.configuration.secret_key_v2_checkbox!
  verify_recaptcha_v2(options)
end

#verify_recaptcha_v2_invisible(options = {}) ⇒ Boolean


72
73
74
75
76
# File 'lib/recaptcha/verify.rb', line 72

def verify_recaptcha_v2_invisible(options = {})
  options = {model: options} unless options.is_a? Hash
  options[:secret_key] ||= Recaptcha.configuration.secret_key_v2_invisible!
  verify_recaptcha_v2(options)
end

#verify_recaptcha_v3(options = {}) ⇒ Recaptcha::Verify::VerifyResult, Boolean

Verifies a response token using the reCAPTCHA v3 API, which returns a score for the given request/action without user friction.

Unlike the v2 API, the reCAPTCHA v3 API is not binary (is not simplify verified/success or not). The v3 API returns a score, so rather than returning a boolean like verify_recaptcha_v2 does, verify_recaptcha_v3 returns a VerifyResult which has a score method on it. The VerifyResult object also gives you access to error_codes and anything else returned in the API response.


100
101
102
103
104
105
106
107
108
109
# File 'lib/recaptcha/verify.rb', line 100

def verify_recaptcha_v3(options = {})
  options.key?(:action) || raise(Recaptcha::RecaptchaError, 'action is required')
  action = options[:action]
  response_token = options[:response] || get_response_token_for_action(action)

  _verify_recaptcha(:v3, response_token, options.freeze) do
    response = recaptcha_verify_via_api_v3_call(request, response_token, options)
    VerifyResultV3.new(response, options[:hostname], action)
  end
end

#verify_recaptcha_v3!(options = {}) ⇒ Recaptcha::Verify::VerifyResult, Boolean

Same as #verify_recaptcha_v3 but raises an error if there is any failure (including if no response token was submitted/found).


116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/recaptcha/verify.rb', line 116

def verify_recaptcha_v3!(options = {})
  result = verify_recaptcha_v3(options)
  # result could be false or a VerifyResult
  unless result && result.valid?
    if @recaptcha_verify_result
      raise(VerifyError, @recaptcha_verify_result.errors.to_sentence)
    else
      raise(VerifyError)
    end
  end
  result
end