Class: Recaptcha::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/recaptcha/configuration.rb

Overview

This class enables detailed configuration of the Recaptcha services.

You can configure Recaptcha by updating Recaptcha.configuration directly:

  Recaptcha.configuration # => instance of Recaptcha::Configuration

or by using a Recaptcha.configure block:

  Recaptcha.configure do |config|
    config # => instance of Recaptcha::Configuration
  end

Your are able to customize all attributes listed below. Some attributes like verify_url usually do not need to be changed.

The site key and secret key have no useful default value so you must configure them in one of the following ways.

  • via RECAPTCHA_SITE_KEY and RECAPTCHA_SECRET_KEY enviroment variables
  • by setting config.site_key and config.secret_key directly

Values within this configuration take precedence over values from environment variables. It is also possible to override this configuration via the options hash passed to specific methods like Verify#verify_recaptcha_v2.

Setting the keys with this Configuration:

  Recaptcha.configure do |config|
    config.site_key   = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
    config.secret_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
  end

If you only intend to use one of the API versions, that's all you have to do. If you would like to use both v2 and v3 APIs—or both a v2 checkbox and an invisible captcha—, then you need a different key for each. You can configure multiple keys like this:

  Recaptcha.configure do |config|
    config.site_key_v2_checkbox    = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyc'
    config.secret_key_v2_checkbox  = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxc'
    config.site_key_v2_invisible   = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyi'
    config.secret_key_v2_invisible = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxi'
    config.site_key_v3             = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyy3'
    config.secret_key_v3           = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxx3'
  end

and the helpers will automatically use the correct key. (In the backend, you will need to use verify_recaptcha_v2_checkbox and verify_recaptcha_v2_invisible instead of simply verify_recaptcha in order for it to know which key to use.)

v3 is not simply a replacement for v2; they each have different use cases. Read Should I use reCAPTCHA v2 or v3?.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/recaptcha/configuration.rb', line 79

def initialize
  @skip_verify_env = %w[test cucumber]
  @handle_timeouts_gracefully = HANDLE_TIMEOUTS_GRACEFULLY

  @site_key                = ENV['RECAPTCHA_SITE_KEY']
  @secret_key              = ENV['RECAPTCHA_SECRET_KEY']
  @site_key_v2_checkbox    = ENV['RECAPTCHA_SITE_KEY_V2_CHECKBOX']
  @secret_key_v2_checkbox  = ENV['RECAPTCHA_SECRET_KEY_V2_CHECKBOX']
  @site_key_v2_invisible   = ENV['RECAPTCHA_SITE_KEY_V2_INVISIBLE']
  @secret_key_v2_invisible = ENV['RECAPTCHA_SECRET_KEY_V2_INVISIBLE']
  @site_key_v3             = ENV['RECAPTCHA_SITE_KEY_V3']
  @secret_key_v3           = ENV['RECAPTCHA_SECRET_KEY_V3']

  @verify_url = nil
  @api_server_url = nil
  @logger = Logger.new('/dev/null')
end

Instance Attribute Details

#api_server_urlObject


129
130
131
# File 'lib/recaptcha/configuration.rb', line 129

def api_server_url
  @api_server_url || CONFIG.fetch('server_url')
end

#handle_timeouts_gracefullyObject

Returns the value of attribute handle_timeouts_gracefully


65
66
67
# File 'lib/recaptcha/configuration.rb', line 65

def handle_timeouts_gracefully
  @handle_timeouts_gracefully
end

#hostnameObject

Returns the value of attribute hostname


65
66
67
# File 'lib/recaptcha/configuration.rb', line 65

def hostname
  @hostname
end

#loggerObject

Returns the value of attribute logger


65
66
67
# File 'lib/recaptcha/configuration.rb', line 65

def logger
  @logger
end

#proxyObject

Returns the value of attribute proxy


65
66
67
# File 'lib/recaptcha/configuration.rb', line 65

def proxy
  @proxy
end

#secret_keyObject

Used to verify a response token against the reCAPTCHA API in your controller


71
72
73
# File 'lib/recaptcha/configuration.rb', line 71

def secret_key
  @secret_key
end

#secret_key_v2_checkboxObject

Returns the value of attribute secret_key_v2_checkbox


73
74
75
# File 'lib/recaptcha/configuration.rb', line 73

def secret_key_v2_checkbox
  @secret_key_v2_checkbox
end

#secret_key_v2_invisibleObject

Returns the value of attribute secret_key_v2_invisible


73
74
75
# File 'lib/recaptcha/configuration.rb', line 73

def secret_key_v2_invisible
  @secret_key_v2_invisible
end

#secret_key_v3Object

Returns the value of attribute secret_key_v3


73
74
75
# File 'lib/recaptcha/configuration.rb', line 73

def secret_key_v3
  @secret_key_v3
end

#site_keyObject

Used to identify your site to the reCAPTCHA script that runs in your frontend code


68
69
70
# File 'lib/recaptcha/configuration.rb', line 68

def site_key
  @site_key
end

#site_key_v2_checkboxObject

Returns the value of attribute site_key_v2_checkbox


73
74
75
# File 'lib/recaptcha/configuration.rb', line 73

def site_key_v2_checkbox
  @site_key_v2_checkbox
end

#site_key_v2_invisibleObject

Returns the value of attribute site_key_v2_invisible


73
74
75
# File 'lib/recaptcha/configuration.rb', line 73

def site_key_v2_invisible
  @site_key_v2_invisible
end

#site_key_v3Object

Returns the value of attribute site_key_v3


73
74
75
# File 'lib/recaptcha/configuration.rb', line 73

def site_key_v3
  @site_key_v3
end

#skip_verify_envObject

Returns the value of attribute skip_verify_env


65
66
67
# File 'lib/recaptcha/configuration.rb', line 65

def skip_verify_env
  @skip_verify_env
end

#verify_urlObject


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

def verify_url
  @verify_url || CONFIG.fetch('verify_url')
end

Instance Method Details

#secret_key!Object


113
114
115
# File 'lib/recaptcha/configuration.rb', line 113

def secret_key!
  secret_key || raise(RecaptchaError, "No secret key specified.")
end

#secret_key_v2_checkbox!Object


117
118
119
# File 'lib/recaptcha/configuration.rb', line 117

def secret_key_v2_checkbox!
  secret_key_v2_checkbox || secret_key || raise(RecaptchaError, "No secret key specified.")
end

#secret_key_v2_invisible!Object


121
122
123
# File 'lib/recaptcha/configuration.rb', line 121

def secret_key_v2_invisible!
  secret_key_v2_invisible || secret_key || raise(RecaptchaError, "No secret key specified.")
end

#secret_key_v3!Object


125
126
127
# File 'lib/recaptcha/configuration.rb', line 125

def secret_key_v3!
  secret_key_v3 || secret_key || raise(RecaptchaError, "No secret key specified.")
end

#site_key!Object


97
98
99
# File 'lib/recaptcha/configuration.rb', line 97

def site_key!
  site_key || raise(RecaptchaError, "No site key specified.")
end

#site_key_v2_checkbox!Object


101
102
103
# File 'lib/recaptcha/configuration.rb', line 101

def site_key_v2_checkbox!
  site_key_v2_checkbox || site_key || raise(RecaptchaError, "No site key specified.")
end

#site_key_v2_invisible!Object


105
106
107
# File 'lib/recaptcha/configuration.rb', line 105

def site_key_v2_invisible!
  site_key_v2_invisible || site_key || raise(RecaptchaError, "No site key specified.")
end

#site_key_v3!Object


109
110
111
# File 'lib/recaptcha/configuration.rb', line 109

def site_key_v3!
  site_key_v3 || site_key || raise(RecaptchaError, "No site key specified.")
end