1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 80 81 82 83 84 85 86
|
# frozen_string_literal: true
module Recaptcha
# This class enables detailed configuration of the recaptcha services.
#
# By calling
#
# Recaptcha.configuration # => instance of Recaptcha::Configuration
#
# or
# Recaptcha.configure do |config|
# config # => instance of Recaptcha::Configuration
# end
#
# you are able to perform configuration updates.
#
# Your are able to customize all attributes listed below. All values have
# sensitive default and will very likely not need to be changed.
#
# Please note that the site and secret key for the reCAPTCHA API Access
# have no useful default value. The keys may be set via the Shell enviroment
# or using this configuration. Settings within this configuration always take
# precedence.
#
# Setting the keys with this Configuration
#
# Recaptcha.configure do |config|
# config.site_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
# config.secret_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
# end
#
class Configuration
DEFAULTS = {
'free_server_url' => 'https://www.recaptcha.net/recaptcha/api.js',
'enterprise_server_url' => 'https://www.recaptcha.net/recaptcha/enterprise.js',
'free_verify_url' => 'https://www.recaptcha.net/recaptcha/api/siteverify',
'enterprise_verify_url' => 'https://recaptchaenterprise.googleapis.com/v1/projects'
}.freeze
attr_accessor :default_env, :skip_verify_env, :proxy, :secret_key, :site_key, :handle_timeouts_gracefully,
:hostname, :enterprise, :enterprise_api_key, :enterprise_project_id, :response_limit
attr_writer :api_server_url, :verify_url
def initialize # :nodoc:
@default_env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || (Rails.env if defined? Rails.env)
@skip_verify_env = %w[test cucumber]
@handle_timeouts_gracefully = true
@secret_key = ENV['RECAPTCHA_SECRET_KEY']
@site_key = ENV['RECAPTCHA_SITE_KEY']
@enterprise = ENV['RECAPTCHA_ENTERPRISE'] == 'true'
@enterprise_api_key = ENV['RECAPTCHA_ENTERPRISE_API_KEY']
@enterprise_project_id = ENV['RECAPTCHA_ENTERPRISE_PROJECT_ID']
@verify_url = nil
@api_server_url = nil
@response_limit = 4000
end
def secret_key!
secret_key || raise(RecaptchaError, "No secret key specified.")
end
def site_key!
site_key || raise(RecaptchaError, "No site key specified.")
end
def enterprise_api_key!
enterprise_api_key || raise(RecaptchaError, "No Enterprise API key specified.")
end
def enterprise_project_id!
enterprise_project_id || raise(RecaptchaError, "No Enterprise project ID specified.")
end
def api_server_url
@api_server_url || (enterprise ? DEFAULTS.fetch('enterprise_server_url') : DEFAULTS.fetch('free_server_url'))
end
def verify_url
@verify_url || (enterprise ? DEFAULTS.fetch('enterprise_verify_url') : DEFAULTS.fetch('free_verify_url'))
end
end
end
|