Module: Recaptcha::Helpers

Defined in:
lib/recaptcha/helpers.rb

Constant Summary collapse

DEFAULT_ERRORS =
{
  recaptcha_unreachable: 'Oops, we failed to validate your reCAPTCHA response. Please try again.',
  verification_failed: 'reCAPTCHA verification failed, please try again.'
}.freeze

Class Method Summary collapse

Class Method Details

.invisible_recaptcha_tags(custom) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/recaptcha/helpers.rb', line 91

def self.invisible_recaptcha_tags(custom)
  options = {callback: 'invisibleRecaptchaSubmit', ui: :button}.merge(custom)
  text = options.delete(:text)
  html, tag_attributes = components(options.dup)
  html << default_callback(options) if default_callback_required?(options)

  case options[:ui]
  when :button
    html << %(<button type="submit" #{tag_attributes}>#{text}</button>\n)
  when :invisible
    html << %(<div data-size="invisible" #{tag_attributes}></div>\n)
  when :input
    html << %(<input type="submit" #{tag_attributes} value="#{text}"/>\n)
  else
    raise(RecaptchaError, "ReCAPTCHA ui `#{options[:ui]}` is not valid.")
  end
  html.respond_to?(:html_safe) ? html.html_safe : html
end

.recaptcha_execute_method_nameObject



303
304
305
# File 'lib/recaptcha/helpers.rb', line 303

def self.recaptcha_execute_method_name
  Recaptcha.configuration.enterprise ? "grecaptcha.enterprise.execute" : "grecaptcha.execute"
end

.recaptcha_ready_method_nameObject



307
308
309
# File 'lib/recaptcha/helpers.rb', line 307

def self.recaptcha_ready_method_name
  Recaptcha.configuration.enterprise ? "grecaptcha.enterprise.ready" : "grecaptcha.ready"
end

.recaptcha_tags(options) ⇒ Object



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
87
88
89
# File 'lib/recaptcha/helpers.rb', line 48

def self.recaptcha_tags(options)
  if options.key?(:stoken)
    raise(RecaptchaError, "Secure Token is deprecated. Please remove 'stoken' from your calls to recaptcha_tags.")
  end
  if options.key?(:ssl)
    raise(RecaptchaError, "SSL is now always true. Please remove 'ssl' from your calls to recaptcha_tags.")
  end

  noscript = options.delete(:noscript)

  html, tag_attributes, fallback_uri = components(options.dup)
  html << %(<div #{tag_attributes}></div>\n)

  if noscript != false
    html << <<-HTML
      <noscript>
        <div>
          <div style="width: 302px; height: 422px; position: relative;">
            <div style="width: 302px; height: 422px; position: absolute;">
              <iframe
                src="#{fallback_uri}"
                name="ReCAPTCHA"
                style="width: 302px; height: 422px; border-style: none; border: 0; overflow: hidden;">
              </iframe>
            </div>
          </div>
          <div style="width: 300px; height: 60px; border-style: none;
            bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
            background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
            <textarea name="g-recaptcha-response"
              class="g-recaptcha-response"
              style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
              margin: 10px 25px; padding: 0px; resize: none;">
            </textarea>
          </div>
        </div>
      </noscript>
    HTML
  end

  html.respond_to?(:html_safe) ? html.html_safe : html
end

.recaptcha_v3(options = {}) ⇒ Object



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

def self.recaptcha_v3(options = {})
  site_key = options[:site_key] ||= Recaptcha.configuration.site_key!
  action = options.delete(:action) || raise(Recaptcha::RecaptchaError, 'action is required')
  id = options.delete(:id) || "g-recaptcha-response-data-#{dasherize_action(action)}"
  name = options.delete(:name) || "g-recaptcha-response-data[#{action}]"
  turbo = options.delete(:turbo) || options.delete(:turbolinks)
  options[:render] = site_key
  options[:script_async] ||= false
  options[:script_defer] ||= false
  options[:ignore_no_element] = options.key?(:ignore_no_element) ? options[:ignore_no_element] : true
  element = options.delete(:element)
  element = element == false ? false : :input
  if element == :input
    callback = options.delete(:callback) || recaptcha_v3_default_callback_name(action)
  end
  options[:class] = "g-recaptcha-response #{options[:class]}"

  if turbo
    options[:onload] = recaptcha_v3_execute_function_name(action)
  end
  html, tag_attributes = components(options)
  if turbo
    html << recaptcha_v3_onload_script(site_key, action, callback, id, options)
  elsif recaptcha_v3_inline_script?(options)
    html << recaptcha_v3_inline_script(site_key, action, callback, id, options)
  end
  case element
  when :input
    html << %(<input type="hidden" name="#{name}" id="#{id}" #{tag_attributes}/>\n)
  when false
    # No tag
    nil
  else
    raise(RecaptchaError, "ReCAPTCHA element `#{options[:element]}` is not valid.")
  end
  html.respond_to?(:html_safe) ? html.html_safe : html
end

.recaptcha_v3_async_execute_function_name(action) ⇒ Object

Returns the name of an async JavaScript function that executes the reCAPTCHA code.



265
266
267
# File 'lib/recaptcha/helpers.rb', line 265

def self.recaptcha_v3_async_execute_function_name(action)
  "#{recaptcha_v3_execute_function_name(action)}Async"
end

.recaptcha_v3_default_callback_name(action) ⇒ Object



269
270
271
# File 'lib/recaptcha/helpers.rb', line 269

def self.recaptcha_v3_default_callback_name(action)
  "setInputWithRecaptchaResponseTokenFor#{sanitize_action_for_js(action)}"
end

.recaptcha_v3_execute_function_name(action) ⇒ Object

Returns the name of the JavaScript function that actually executes the reCAPTCHA code (calls ‘grecaptcha.execute` or `grecaptcha.enterprise.execute`). You can call it again later to reset it.



260
261
262
# File 'lib/recaptcha/helpers.rb', line 260

def self.recaptcha_v3_execute_function_name(action)
  "executeRecaptchaFor#{sanitize_action_for_js(action)}"
end

.to_error_message(key) ⇒ Object



110
111
112
113
# File 'lib/recaptcha/helpers.rb', line 110

def self.to_error_message(key)
  default = DEFAULT_ERRORS.fetch(key) { raise ArgumentError "Unknown reCAPTCHA error - #{key}" }
  to_message("recaptcha.errors.#{key}", default)
end

.to_message(_key, default) ⇒ Object



116
117
118
# File 'lib/recaptcha/helpers.rb', line 116

def self.to_message(key, default)
  I18n.translate(key, default: default)
end