recaptcha 4.0.0 → 5.16.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
+
1
2
  # reCAPTCHA
3
+ [![Gem Version](https://badge.fury.io/rb/recaptcha.svg)](https://badge.fury.io/rb/recaptcha)
2
4
 
3
5
  Author: Jason L Perry (http://ambethia.com)<br/>
4
6
  Copyright: Copyright (c) 2007-2013 Jason L Perry<br/>
@@ -6,42 +8,89 @@ License: [MIT](http://creativecommons.org/licenses/MIT/)<br/>
6
8
  Info: https://github.com/ambethia/recaptcha<br/>
7
9
  Bugs: https://github.com/ambethia/recaptcha/issues<br/>
8
10
 
9
- This plugin adds helpers for the [reCAPTCHA API](https://www.google.com/recaptcha). In your
10
- views you can use the `recaptcha_tags` method to embed the needed javascript,
11
- and you can validate in your controllers with `verify_recaptcha` or `verify_recaptcha!`,
12
- which throws an error on failiure.
11
+ This gem provides helper methods for the [reCAPTCHA API](https://www.google.com/recaptcha). In your
12
+ views you can use the `recaptcha_tags` method to embed the needed javascript, and you can validate
13
+ in your controllers with `verify_recaptcha` or `verify_recaptcha!`, which raises an error on
14
+ failure.
15
+
16
+
17
+ # Table of Contents
18
+ 1. [Obtaining a key](#obtaining-a-key)
19
+ 2. [Rails Installation](#rails-installation)
20
+ 3. [Sinatra / Rack / Ruby Installation](#sinatra--rack--ruby-installation)
21
+ 4. [reCAPTCHA V2 API & Usage](#recaptcha-v2-api-and-usage)
22
+ - [`recaptcha_tags`](#recaptcha_tags)
23
+ - [`verify_recaptcha`](#verify_recaptcha)
24
+ - [`invisible_recaptcha_tags`](#invisible_recaptcha_tags)
25
+ 5. [reCAPTCHA V3 API & Usage](#recaptcha-v3-api-and-usage)
26
+ - [`recaptcha_v3`](#recaptcha_v3)
27
+ - [`verify_recaptcha` (use with v3)](#verify_recaptcha-use-with-v3)
28
+ - [`recaptcha_reply`](#recaptcha_reply)
29
+ 6. [I18n Support](#i18n-support)
30
+ 7. [Testing](#testing)
31
+ 8. [Alternative API Key Setup](#alternative-api-key-setup)
32
+
33
+ ## Obtaining a key
34
+
35
+ Go to the [reCAPTCHA admin console](https://www.google.com/recaptcha/admin) to obtain a reCAPTCHA API key.
36
+
37
+ The reCAPTCHA type(s) that you choose for your key will determine which methods to use below.
38
+
39
+ | reCAPTCHA type | Methods to use | Description |
40
+ |----------------------------------------------|----------------|-------------|
41
+ | v3 | [`recaptcha_v3`](#recaptcha_v3) | Verify requests with a [score](https://developers.google.com/recaptcha/docs/v3#score)
42
+ | v2 Checkbox<br/>("I'm not a robot" Checkbox) | [`recaptcha_tags`](#recaptcha_tags) | Validate requests with the "I'm not a robot" checkbox |
43
+ | v2 Invisible<br/>(Invisible reCAPTCHA badge) | [`invisible_recaptcha_tags`](#invisible_recaptcha_tags) | Validate requests in the background |
44
+
45
+ Note: You can _only_ use methods that match your key's type. You cannot use v2 methods with a v3
46
+ key or use `recaptcha_tags` with a v2 Invisible key, for example. Otherwise you will get an
47
+ error like "Invalid key type" or "This site key is not enabled for the invisible captcha."
48
+
49
+ Note: Enter `localhost` or `127.0.0.1` as the domain if using in development with `localhost:3000`.
13
50
 
14
51
  ## Rails Installation
15
52
 
16
- [obtain a reCAPTCHA API key](https://www.google.com/recaptcha/admin).
53
+ **If you are having issues with Rails 7, Turbo, and Stimulus, make sure to check [this Wiki page](https://github.com/ambethia/recaptcha/wiki/Recaptcha-with-Turbo-and-Stimulus)!**
17
54
 
18
- ```Ruby
19
- gem "recaptcha", require: "recaptcha/rails"
55
+ ```ruby
56
+ gem "recaptcha"
20
57
  ```
21
58
 
22
- Keep keys out of the code base with environment variables.<br/>
23
- Set in production and locally use [dotenv](https://github.com/bkeepers/dotenv), make sure to add it above recaptcha.
59
+ You can keep keys out of the code base with environment variables or with Rails [secrets](https://api.rubyonrails.org/classes/Rails/Application.html#method-i-secrets).<br/>
24
60
 
25
- Otherwise see [Alternative API key setup](#alternative-api-key-setup).
61
+ In development, you can use the [dotenv](https://github.com/bkeepers/dotenv) gem. (Make sure to add it above `gem 'recaptcha'`.)
26
62
 
27
- ```
28
- export RECAPTCHA_SITE_KEY = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
63
+ See [Alternative API key setup](#alternative-api-key-setup) for more ways to configure or override
64
+ keys. See also the
65
+ [Configuration](https://www.rubydoc.info/github/ambethia/recaptcha/master/Recaptcha/Configuration)
66
+ documentation.
67
+
68
+ ```shell
69
+ export RECAPTCHA_SITE_KEY = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
29
70
  export RECAPTCHA_SECRET_KEY = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
30
71
  ```
31
72
 
32
- Add `recaptcha_tags` to the forms you want to protect.
73
+ If you have an Enterprise API key:
33
74
 
34
- ```Erb
75
+ ```shell
76
+ export RECAPTCHA_ENTERPRISE = 'true'
77
+ export RECAPTCHA_ENTERPRISE_API_KEY = 'AIzvFyE3TU-g4K_Kozr9F1smEzZSGBVOfLKyupA'
78
+ export RECAPTCHA_ENTERPRISE_PROJECT_ID = 'my-project'
79
+ ```
80
+
81
+ Add `recaptcha_tags` to the forms you want to protect:
82
+
83
+ ```erb
35
84
  <%= form_for @foo do |f| %>
36
- # ... other tags
85
+ #
37
86
  <%= recaptcha_tags %>
38
- # ... other tags
87
+ #
39
88
  <% end %>
40
89
  ```
41
90
 
42
- And, add `verify_recaptcha` logic to each form action that you've protected.
91
+ Then, add `verify_recaptcha` logic to each form action that you've protected:
43
92
 
44
- ```Ruby
93
+ ```ruby
45
94
  # app/controllers/users_controller.rb
46
95
  @user = User.new(params[:user].permit(:name))
47
96
  if verify_recaptcha(model: @user) && @user.save
@@ -50,6 +99,7 @@ else
50
99
  render 'new'
51
100
  end
52
101
  ```
102
+ Please note that this setup uses [`reCAPTCHA_v2`](#recaptcha-v2-api-and-usage). For a `recaptcha_v3` use, please refer to [`reCAPTCHA_v3 setup`](#examples).
53
103
 
54
104
  ## Sinatra / Rack / Ruby installation
55
105
 
@@ -57,70 +107,410 @@ See [sinatra demo](/demo/sinatra) for details.
57
107
 
58
108
  - add `gem 'recaptcha'` to `Gemfile`
59
109
  - set env variables
60
- - `include Recaptcha::ClientHelper` where you need `recaptcha_tags`
61
- - `include Recaptcha::Verify` where you need `verify_recaptcha`
110
+ - `include Recaptcha::Adapters::ViewMethods` where you need `recaptcha_tags`
111
+ - `include Recaptcha::Adapters::ControllerMethods` where you need `verify_recaptcha`
62
112
 
63
- ## recaptcha_tags
64
113
 
65
- Some of the options available:
114
+ ## reCAPTCHA v2 API and Usage
115
+
116
+ ### `recaptcha_tags`
117
+
118
+ Use this when your key's reCAPTCHA type is "v2 Checkbox".
119
+
120
+ The following options are available:
121
+
122
+ | Option | Description |
123
+ |---------------------|-------------|
124
+ | `:theme` | Specify the theme to be used per the API. Available options: `dark` and `light`. (default: `light`) |
125
+ | `:ajax` | Render the dynamic AJAX captcha per the API. (default: `false`) |
126
+ | `:site_key` | Override site API key from configuration |
127
+ | `:error` | Override the error code returned from the reCAPTCHA API (default: `nil`) |
128
+ | `:size` | Specify a size (default: `nil`) |
129
+ | `:nonce` | Optional. Sets nonce attribute for script. Can be generated via `SecureRandom.base64(32)`. (default: `nil`) |
130
+ | `:id` | Specify an html id attribute (default: `nil`) |
131
+ | `:callback` | Optional. Name of success callback function, executed when the user submits a successful response |
132
+ | `:expired_callback` | Optional. Name of expiration callback function, executed when the reCAPTCHA response expires and the user needs to re-verify. |
133
+ | `:error_callback` | Optional. Name of error callback function, executed when reCAPTCHA encounters an error (e.g. network connectivity) |
134
+ | `:noscript` | Include `<noscript>` content (default: `true`)|
135
+
136
+ [JavaScript resource (api.js) parameters](https://developers.google.com/recaptcha/docs/invisible#js_param):
137
+
138
+ | Option | Description |
139
+ |---------------------|-------------|
140
+ | `:onload` | Optional. The name of your callback function to be executed once all the dependencies have loaded. (See [explicit rendering](https://developers.google.com/recaptcha/docs/display#explicit_render)) |
141
+ | `:render` | Optional. Whether to render the widget explicitly. Defaults to `onload`, which will render the widget in the first g-recaptcha tag it finds. (See [explicit rendering](https://developers.google.com/recaptcha/docs/display#explicit_render)) |
142
+ | `:hl` | Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. (See [language codes](https://developers.google.com/recaptcha/docs/language)) |
143
+ | `:script` | Alias for `:external_script`. If you do not need to add a script tag by helper you can set the option to `false`. It's necessary when you add a script tag manualy (default: `true`). |
144
+ | `:external_script` | Set to `false` to avoid including a script tag for the external `api.js` resource. Useful when including multiple `recaptcha_tags` on the same page. |
145
+ | `:script_async` | Set to `false` to load the external `api.js` resource synchronously. (default: `true`) |
146
+ | `:script_defer` | Set to `true` to defer loading of external `api.js` until HTML documen has been parsed. (default: `true`) |
66
147
 
67
- | Option | Description |
68
- |-------------|-------------|
69
- | :noscript | Include <noscript> content (default `true`)|
70
- | :display | Takes a hash containing the `theme` and `tabindex` options per the API. (default `nil`), options: 'red', 'white', 'blackglass', 'clean', 'custom'|
71
- | :ajax | Render the dynamic AJAX captcha per the API. (default `false`)|
72
- | :site_key | Override site API key |
73
- | :error | Override the error code returned from the reCAPTCHA API (default `nil`)|
74
- | :size | Specify a size (default `nil`)|
75
- | :hl | Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. (See [language codes](https://developers.google.com/recaptcha/docs/language)) |
76
- | :id | Specify an html id attribute (default `nil`)|
77
- | :script | If you do not need to add a script tag by helper you can set the option to false. It's necessary when you add a script tag manualy (default `true`)|
148
+ Any unrecognized options will be added as attributes on the generated tag.
78
149
 
79
150
  You can also override the html attributes for the sizes of the generated `textarea` and `iframe`
80
- elements, if CSS isn't your thing. Inspect the source of `recaptcha_tags` to see these options.
151
+ elements, if CSS isn't your thing. Inspect the [source of `recaptcha_tags`](https://github.com/ambethia/recaptcha/blob/master/lib/recaptcha/helpers.rb)
152
+ to see these options.
81
153
 
82
- ## verify_recaptcha
154
+ Note that you cannot submit/verify the same response token more than once or you will get a
155
+ `timeout-or-duplicate` error code. If you need reset the captcha and generate a new response token,
156
+ then you need to call `grecaptcha.reset()`.
83
157
 
84
- This method returns `true` or `false` after processing the parameters from the reCAPTCHA widget. Why
85
- isn't this a model validation? Because that violates MVC. You can use it like this, or how ever you
86
- like. Passing in the ActiveRecord object is optional, if you do--and the captcha fails to verify--an
87
- error will be added to the object for you to use.
158
+ ### `verify_recaptcha`
159
+
160
+ This method returns `true` or `false` after processing the response token from the reCAPTCHA widget.
161
+ This is usually called from your controller, as seen [above](#rails-installation).
162
+
163
+ Passing in the ActiveRecord object via `model: object` is optional. If you pass a `model`—and the
164
+ captcha fails to verify—an error will be added to the object for you to use (available as
165
+ `object.errors`).
166
+
167
+ Why isn't this a model validation? Because that violates MVC. You can use it like this, or how ever
168
+ you like.
88
169
 
89
170
  Some of the options available:
90
171
 
91
- | Option | Description |
92
- |--------------|-------------|
93
- | :model | Model to set errors.
94
- | :attribute | Model attribute to receive errors. (default :base)
95
- | :message | Custom error message.
96
- | :secret_key | Override secret API key.
97
- | :timeout | The number of seconds to wait for reCAPTCHA servers before give up. (default `3`)
98
- | :response | Custom response parameter. (default: params['g-recaptcha-response'])
99
- | :hostname | Expected hostname or a callable that validates the hostname, see [domain validation](https://developers.google.com/recaptcha/docs/domain_validation) and [hostname](https://developers.google.com/recaptcha/docs/verify#api-response) docs. (default: `nil`, but can be changed by setting `config.hostname`)
100
- | :env | Current environment. The request to verify will be skipped if the environment is specified in configuration under `skip_verify_env`
172
+ | Option | Description |
173
+ |---------------------------|-------------|
174
+ | `:model` | Model to set errors.
175
+ | `:attribute` | Model attribute to receive errors. (default: `:base`)
176
+ | `:message` | Custom error message.
177
+ | `:secret_key` | Override the secret API key from the configuration.
178
+ | `:enterprise_api_key` | Override the Enterprise API key from the configuration.
179
+ | `:enterprise_project_id ` | Override the Enterprise project ID from the configuration.
180
+ | `:timeout` | The number of seconds to wait for reCAPTCHA servers before give up. (default: `3`)
181
+ | `:response` | Custom response parameter. (default: `params['g-recaptcha-response-data']`)
182
+ | `:hostname` | Expected hostname or a callable that validates the hostname, see [domain validation](https://developers.google.com/recaptcha/docs/domain_validation) and [hostname](https://developers.google.com/recaptcha/docs/verify#api-response) docs. (default: `nil`, but can be changed by setting `config.hostname`)
183
+ | `:env` | Current environment. The request to verify will be skipped if the environment is specified in configuration under `skip_verify_env`
184
+ | `:json` | Boolean; defaults to false; if true, will submit the verification request by POST with the request data in JSON
101
185
 
102
- ## I18n support
103
- reCAPTCHA passes two types of error explanation to a linked model. It will use the I18n gem
104
- to translate the default error message if I18n is available. To customize the messages to your locale,
105
- add these keys to your I18n backend:
106
186
 
107
- `recaptcha.errors.verification_failed` error message displayed if the captcha words didn't match
108
- `recaptcha.errors.recaptcha_unreachable` displayed if a timeout error occured while attempting to verify the captcha
187
+ ### `invisible_recaptcha_tags`
188
+
189
+ Use this when your key's reCAPTCHA type is "v2 Invisible".
190
+
191
+ For more information, refer to: [Invisible reCAPTCHA](https://developers.google.com/recaptcha/docs/invisible).
192
+
193
+ This is similar to `recaptcha_tags`, with the following additional options that are only available
194
+ on `invisible_recaptcha_tags`:
195
+
196
+ | Option | Description |
197
+ |---------------------|-------------|
198
+ | `:ui` | The type of UI to render for this "invisible" widget. (default: `:button`)<br/>`:button`: Renders a `<button type="submit">` tag with `options[:text]` as the button text.<br/>`:invisible`: Renders a `<div>` tag.<br/>`:input`: Renders a `<input type="submit">` tag with `options[:text]` as the button text. |
199
+ | `:text` | The text to show for the button. (default: `"Submit"`)
200
+ | `:inline_script` | If you do not need this helper to add an inline script tag, you can set the option to `false` (default: `true`).
201
+
202
+ It also accepts most of the options that `recaptcha_tags` accepts, including the following:
203
+
204
+ | Option | Description |
205
+ |---------------------|-------------|
206
+ | `:site_key` | Override site API key from configuration |
207
+ | `:nonce` | Optional. Sets nonce attribute for script tag. Can be generated via `SecureRandom.base64(32)`. (default: `nil`) |
208
+ | `:id` | Specify an html id attribute (default: `nil`) |
209
+ | `:script` | Same as setting both `:inline_script` and `:external_script`. If you only need one or the other, use `:inline_script` and `:external_script` instead. |
210
+ | `:callback` | Optional. Name of success callback function, executed when the user submits a successful response |
211
+ | `:expired_callback` | Optional. Name of expiration callback function, executed when the reCAPTCHA response expires and the user needs to re-verify. |
212
+ | `:error_callback` | Optional. Name of error callback function, executed when reCAPTCHA encounters an error (e.g. network connectivity) |
213
+
214
+ [JavaScript resource (api.js) parameters](https://developers.google.com/recaptcha/docs/invisible#js_param):
215
+
216
+ | Option | Description |
217
+ |---------------------|-------------|
218
+ | `:onload` | Optional. The name of your callback function to be executed once all the dependencies have loaded. (See [explicit rendering](https://developers.google.com/recaptcha/docs/display#explicit_render)) |
219
+ | `:render` | Optional. Whether to render the widget explicitly. Defaults to `onload`, which will render the widget in the first g-recaptcha tag it finds. (See [explicit rendering](https://developers.google.com/recaptcha/docs/display#explicit_render)) |
220
+ | `:hl` | Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. (See [language codes](https://developers.google.com/recaptcha/docs/language)) |
221
+ | `:external_script` | Set to `false` to avoid including a script tag for the external `api.js` resource. Useful when including multiple `recaptcha_tags` on the same page. |
222
+ | `:script_async` | Set to `false` to load the external `api.js` resource synchronously. (default: `true`) |
223
+ | `:script_defer` | Set to `false` to defer loading of external `api.js` until HTML documen has been parsed. (default: `true`) |
224
+
225
+ ### With a single form on a page
226
+
227
+ 1. The `invisible_recaptcha_tags` generates a submit button for you.
228
+
229
+ ```erb
230
+ <%= form_for @foo do |f| %>
231
+ # ... other tags
232
+ <%= invisible_recaptcha_tags text: 'Submit form' %>
233
+ <% end %>
234
+ ```
235
+
236
+ Then, add `verify_recaptcha` to your controller as seen [above](#rails-installation).
237
+
238
+ ### With multiple forms on a page
239
+
240
+ 1. You will need a custom callback function, which is called after verification with Google's reCAPTCHA service. This callback function must submit the form. Optionally, `invisible_recaptcha_tags` currently implements a JS function called `invisibleRecaptchaSubmit` that is called when no `callback` is passed. Should you wish to override `invisibleRecaptchaSubmit`, you will need to use `invisible_recaptcha_tags script: false`, see lib/recaptcha/client_helper.rb for details.
241
+ 2. The `invisible_recaptcha_tags` generates a submit button for you.
242
+
243
+ ```erb
244
+ <%= form_for @foo, html: {id: 'invisible-recaptcha-form'} do |f| %>
245
+ # ... other tags
246
+ <%= invisible_recaptcha_tags callback: 'submitInvisibleRecaptchaForm', text: 'Submit form' %>
247
+ <% end %>
248
+ ```
249
+
250
+ ```javascript
251
+ // app/assets/javascripts/application.js
252
+ var submitInvisibleRecaptchaForm = function () {
253
+ document.getElementById("invisible-recaptcha-form").submit();
254
+ };
255
+ ```
256
+
257
+ Finally, add `verify_recaptcha` to your controller as seen [above](#rails-installation).
258
+
259
+ ### Programmatically invoke
260
+
261
+ 1. Specify `ui` option
262
+
263
+ ```erb
264
+ <%= form_for @foo, html: {id: 'invisible-recaptcha-form'} do |f| %>
265
+ # ... other tags
266
+ <button type="button" id="submit-btn">
267
+ Submit
268
+ </button>
269
+ <%= invisible_recaptcha_tags ui: :invisible, callback: 'submitInvisibleRecaptchaForm' %>
270
+ <% end %>
271
+ ```
272
+
273
+ ```javascript
274
+ // app/assets/javascripts/application.js
275
+ document.getElementById('submit-btn').addEventListener('click', function (e) {
276
+ // do some validation
277
+ if(isValid) {
278
+ // call reCAPTCHA check
279
+ grecaptcha.execute();
280
+ }
281
+ });
282
+
283
+ var submitInvisibleRecaptchaForm = function () {
284
+ document.getElementById("invisible-recaptcha-form").submit();
285
+ };
286
+ ```
287
+
288
+
289
+ ## reCAPTCHA v3 API and Usage
290
+
291
+ The main differences from v2 are:
292
+ 1. you must specify an [action](https://developers.google.com/recaptcha/docs/v3#actions) in both frontend and backend
293
+ 1. you can choose the minimum score required for you to consider the verification a success
294
+ (consider the user a human and not a robot)
295
+ 1. reCAPTCHA v3 is invisible (except for the reCAPTCHA badge) and will never interrupt your users;
296
+ you have to choose which scores are considered an acceptable risk, and choose what to do (require
297
+ two-factor authentication, show a v3 challenge, etc.) if the score falls below the threshold you
298
+ choose
299
+
300
+ For more information, refer to the [v3 documentation](https://developers.google.com/recaptcha/docs/v3).
301
+
302
+ ### Examples
303
+
304
+ With v3, you can let all users log in without any intervention at all if their score is above some
305
+ threshold, and only show a v2 checkbox recaptcha challenge (fall back to v2) if it is below the
306
+ threshold:
307
+
308
+ ```erb
309
+
310
+ <% if @show_checkbox_recaptcha %>
311
+ <%= recaptcha_tags %>
312
+ <% else %>
313
+ <%= recaptcha_v3(action: 'login', site_key: ENV['RECAPTCHA_SITE_KEY_V3']) %>
314
+ <% end %>
315
+
316
+ ```
317
+
318
+ ```ruby
319
+ # app/controllers/sessions_controller.rb
320
+ def create
321
+ success = verify_recaptcha(action: 'login', minimum_score: 0.5, secret_key: ENV['RECAPTCHA_SECRET_KEY_V3'])
322
+ checkbox_success = verify_recaptcha unless success
323
+ if success || checkbox_success
324
+ # Perform action
325
+ else
326
+ if !success
327
+ @show_checkbox_recaptcha = true
328
+ end
329
+ render 'new'
330
+ end
331
+ end
332
+ ```
333
+
334
+ (You can also find this [example](demo/rails/app/controllers/v3_captchas_controller.rb) in the demo app.)
335
+
336
+ Another example:
337
+
338
+ ```erb
339
+ <%= form_for @user do |f| %>
340
+
341
+ <%= recaptcha_v3(action: 'registration') %>
342
+
343
+ <% end %>
344
+ ```
345
+
346
+ ```ruby
347
+ # app/controllers/users_controller.rb
348
+ def create
349
+ @user = User.new(params[:user].permit(:name))
350
+ recaptcha_valid = verify_recaptcha(model: @user, action: 'registration')
351
+ if recaptcha_valid
352
+ if @user.save
353
+ redirect_to @user
354
+ else
355
+ render 'new'
356
+ end
357
+ else
358
+ # Score is below threshold, so user may be a bot. Show a challenge, require multi-factor
359
+ # authentication, or do something else.
360
+ render 'new'
361
+ end
362
+ end
363
+ ```
364
+
365
+
366
+ ### `recaptcha_v3`
367
+
368
+ Adds an inline script tag that calls `grecaptcha.execute` for the given `site_key` and `action` and
369
+ calls the `callback` with the resulting response token. You need to verify this token with
370
+ [`verify_recaptcha`](#verify_recaptcha-use-with-v3) in your controller in order to get the
371
+ [score](https://developers.google.com/recaptcha/docs/v3#score).
372
+
373
+ By default, this inserts a hidden `<input type="hidden" class="g-recaptcha-response">` tag. The
374
+ value of this input will automatically be set to the response token (by the default callback
375
+ function). This lets you include `recaptcha_v3` within a `<form>` tag and have it automatically
376
+ submit the token as part of the form submission.
377
+
378
+ Note: reCAPTCHA actually already adds its own hidden tag, like `<textarea
379
+ id="g-recaptcha-response-data-100000" name="g-recaptcha-response-data" class="g-recaptcha-response">`,
380
+ immediately ater the reCAPTCHA badge in the bottom right of the page — but since it is not inside of
381
+ any `<form>` element, and since it already passes the token to the callback, this hidden `textarea`
382
+ isn't helpful to us.
383
+
384
+ If you need to submit the response token to the server in a different way than via a regular form
385
+ submit, such as via [Ajax](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) or [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API),
386
+ then you can either:
387
+ 1. just extract the token out of the hidden `<input>` or `<textarea>` (both of which will have a
388
+ predictable name/id), like `document.getElementById('g-recaptcha-response-data-my-action').value`, or
389
+ 2. write and specify a custom `callback` function. You may also want to pass `element: false` if you
390
+ don't have a use for the hidden input element.
391
+
392
+ Note that you cannot submit/verify the same response token more than once or you
393
+ will get a `timeout-or-duplicate` error code. If you need reset the captcha and
394
+ generate a new response token, then you need to call `grecaptcha.execute(…)` or
395
+ `grecaptcha.enterprise.execute(…)` again. This helper provides a JavaScript
396
+ method (for each action) named `executeRecaptchaFor{action}` to make this
397
+ easier. That is the same method that is invoked immediately. It simply calls
398
+ `grecaptcha.execute` or `grecaptcha.enterprise.execute` again and then calls the
399
+ `callback` function with the response token.
400
+
401
+ You will also get a `timeout-or-duplicate` error if too much time has passed between getting the
402
+ response token and verifying it. This can easily happen with large forms that take the user a couple
403
+ minutes to complete. Unlike v2, where you can use the `expired-callback` to be notified when the
404
+ response expires, v3 appears to provide no such callback. See also
405
+ [1](https://github.com/google/recaptcha/issues/281) and
406
+ [2](https://stackoverflow.com/questions/54437745/recaptcha-v3-how-to-deal-with-expired-token-after-idle).
407
+
408
+ To deal with this, it is recommended to call the "execute" in your form's submit handler (or
409
+ immediately before sending to the server to verify if not using a form) rather than using the
410
+ response token that gets generated when the page first loads. The `executeRecaptchaFor{action}`
411
+ function mentioned above can be used if you want it to invoke a callback, or the
412
+ `executeRecaptchaFor{action}Async` variant if you want a `Promise` that you can `await`. See
413
+ [demo/rails/app/views/v3_captchas/index.html.erb](demo/rails/app/views/v3_captchas/index.html.erb)
414
+ for an example of this.
415
+
416
+ This helper is similar to the [`recaptcha_tags`](#recaptcha_tags)/[`invisible_recaptcha_tags`](#invisible_recaptcha_tags) helpers
417
+ but only accepts the following options:
418
+
419
+ | Option | Description |
420
+ |---------------------|-------------|
421
+ | `:site_key` | Override site API key |
422
+ | `:action` | The name of the [reCAPTCHA action](https://developers.google.com/recaptcha/docs/v3#actions). Actions are not case-sensitive and may only contain alphanumeric characters, slashes, and underscores, and must not be user-specific. |
423
+ | `:nonce` | Optional. Sets nonce attribute for script. Can be generated via `SecureRandom.base64(32)`. (default: `nil`) |
424
+ | `:callback` | Name of callback function to call with the token. When `element` is `:input`, this defaults to a function named `setInputWithRecaptchaResponseTokenFor#{sanitize_action(action)}` that sets the value of the hidden input to the token. |
425
+ | `:id` | Specify a unique `id` attribute for the `<input>` element if using `element: :input`. (default: `"g-recaptcha-response-data-"` + `action`) |
426
+ | `:name` | Specify a unique `name` attribute for the `<input>` element if using `element: :input`. (default: `g-recaptcha-response-data[action]`) |
427
+ | `:script` | Same as setting both `:inline_script` and `:external_script`. (default: `true`). |
428
+ | `:inline_script` | If `true`, adds an inline script tag that calls `grecaptcha.execute` for the given `site_key` and `action` and calls the `callback` with the resulting response token. Pass `false` if you want to handle calling `grecaptcha.execute` yourself. (default: `true`) |
429
+ | `:element` | The element to render, if any (default: `:input`)<br/>`:input`: Renders a hidden `<input type="hidden">` tag. The value of this will be set to the response token by the default `setInputWithRecaptchaResponseTokenFor{action}` callback.<br/>`false`: Doesn't render any tag. You'll have to add a custom callback that does something with the token. |
430
+ | `:turbo` | If `true`, calls the js function which executes reCAPTCHA after all the dependencies have been loaded. This cannot be used with the js param `:onload`. This makes reCAPTCHAv3 usable with turbo. |
431
+ | `:turbolinks` | Alias of `:turbo`. Will be deprecated soon. |
432
+ | `:ignore_no_element` | If `true`, adds null element checker for forms that can be removed from the page by javascript like modals with forms. (default: true) |
433
+
434
+ [JavaScript resource (api.js) parameters](https://developers.google.com/recaptcha/docs/invisible#js_param):
435
+
436
+ | Option | Description |
437
+ |---------------------|-------------|
438
+ | `:onload` | Optional. The name of your callback function to be executed once all the dependencies have loaded. (See [explicit rendering](https://developers.google.com/recaptcha/docs/display#explicit_render))|
439
+ | `:external_script` | Set to `false` to avoid including a script tag for the external `api.js` resource. Useful when including multiple `recaptcha_tags` on the same page.
440
+ | `:script_async` | Set to `true` to load the external `api.js` resource asynchronously. (default: `false`) |
441
+ | `:script_defer` | Set to `true` to defer loading of external `api.js` until HTML documen has been parsed. (default: `false`) |
442
+
443
+ If using `element: :input`, any unrecognized options will be added as attributes on the generated
444
+ `<input>` element.
445
+
446
+ ### `verify_recaptcha` (use with v3)
447
+
448
+ This works the same as for v2, except that you may pass an `action` and `minimum_score` if you wish
449
+ to validate that the action matches or that the score is above the given threshold, respectively.
450
+
451
+ ```ruby
452
+ result = verify_recaptcha(action: 'action/name')
453
+ ```
454
+
455
+ | Option | Description |
456
+ |------------------|-------------|
457
+ | `:action` | The name of the [reCAPTCHA action](https://developers.google.com/recaptcha/docs/v3#actions) that we are verifying. Set to `false` or `nil` to skip verifying that the action matches.
458
+ | `:minimum_score` | Provide a threshold to meet or exceed. Threshold should be a float between 0 and 1 which will be tested as `score >= minimum_score`. (Default: `nil`) |
459
+
460
+ ### Multiple actions on the same page
461
+
462
+ According to https://developers.google.com/recaptcha/docs/v3#placement,
463
+
464
+ > Note: You can execute reCAPTCHA as many times as you'd like with different actions on the same page.
465
+
466
+ You will need to verify each action individually with a separate call to `verify_recaptcha`.
467
+
468
+ ```ruby
469
+ result_a = verify_recaptcha(action: 'a')
470
+ result_b = verify_recaptcha(action: 'b')
471
+ ```
472
+
473
+ Because the response tokens for multiple actions may be submitted together in the same request, they
474
+ are passed as a hash under `params['g-recaptcha-response-data']` with the action as the key.
475
+
476
+ It is recommended to pass `external_script: false` on all but one of the calls to
477
+ `recaptcha` since you only need to include the script tag once for a given `site_key`.
109
478
 
110
- Also you can translate API response errors to human friendly by adding translations to the locale (`config/locales/en.yml`):
479
+ ## `recaptcha_reply`
111
480
 
112
- ```Yaml
481
+ After `verify_recaptcha` has been called, you can call `recaptcha_reply` to get the raw reply from recaptcha. This can allow you to get the exact score returned by recaptcha should you need it.
482
+
483
+ ```ruby
484
+ if verify_recaptcha(action: 'login')
485
+ redirect_to @user
486
+ else
487
+ score = recaptcha_reply['score']
488
+ Rails.logger.warn("User #{@user.id} was denied login because of a recaptcha score of #{score}")
489
+ render 'new'
490
+ end
491
+ ```
492
+
493
+ `recaptcha_reply` will return `nil` if the the reply was not yet fetched.
494
+
495
+ ## I18n support
496
+
497
+ reCAPTCHA supports the I18n gem (it comes with English translations)
498
+ To override or add new languages, add to `config/locales/*.yml`
499
+
500
+ ```yaml
501
+ # config/locales/en.yml
113
502
  en:
114
503
  recaptcha:
115
504
  errors:
116
- verification_failed: 'Fail'
505
+ verification_failed: 'reCAPTCHA was incorrect, please try again.'
506
+ recaptcha_unreachable: 'reCAPTCHA verification server error, please try again.'
117
507
  ```
118
508
 
119
509
  ## Testing
120
510
 
121
511
  By default, reCAPTCHA is skipped in "test" and "cucumber" env. To enable it during test:
122
512
 
123
- ```Ruby
513
+ ```ruby
124
514
  Recaptcha.configuration.skip_verify_env.delete("test")
125
515
  ```
126
516
 
@@ -128,21 +518,27 @@ Recaptcha.configuration.skip_verify_env.delete("test")
128
518
 
129
519
  ### Recaptcha.configure
130
520
 
131
- ```Ruby
521
+ ```ruby
132
522
  # config/initializers/recaptcha.rb
133
523
  Recaptcha.configure do |config|
134
524
  config.site_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
135
525
  config.secret_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
526
+
136
527
  # Uncomment the following line if you are using a proxy server:
137
528
  # config.proxy = 'http://myproxy.com.au:8080'
529
+
530
+ # Uncomment the following lines if you are using the Enterprise API:
531
+ # config.enterprise = true
532
+ # config.enterprise_api_key = 'AIzvFyE3TU-g4K_Kozr9F1smEzZSGBVOfLKyupA'
533
+ # config.enterprise_project_id = 'my-project'
138
534
  end
139
535
  ```
140
536
 
141
537
  ### Recaptcha.with_configuration
142
538
 
143
- For temporary overwrites (not thread safe).
539
+ For temporary overwrites (not thread-safe).
144
540
 
145
- ```Ruby
541
+ ```ruby
146
542
  Recaptcha.with_configuration(site_key: '12345') do
147
543
  # Do stuff with the overwritten site_key.
148
544
  end
@@ -152,14 +548,46 @@ end
152
548
 
153
549
  Pass in keys as options at runtime, for code base with multiple reCAPTCHA setups:
154
550
 
155
- ```Ruby
551
+ ```ruby
156
552
  recaptcha_tags site_key: '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
157
553
 
158
- and
554
+ # and
159
555
 
160
556
  verify_recaptcha secret_key: '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
161
557
  ```
162
558
 
559
+
560
+ ## hCaptcha support
561
+
562
+ [hCaptcha](https://hcaptcha.com) is an alternative service providing reCAPTCHA API.
563
+
564
+ To use hCaptcha:
565
+ 1. Set a site and a secret key as usual
566
+ 2. Set two options in `verify_url` and `api_service_url` pointing to hCaptcha API endpoints.
567
+ 3. Disable a response limit check by setting a `response_limit` to the large enough value (reCAPTCHA is limited by 4000 characters).
568
+ 4. It is not required to change a parameter name as [official docs suggest](https://docs.hcaptcha.com/switch) because API handles standard `g-recaptcha` for compatibility.
569
+
570
+ ```ruby
571
+ # config/initializers/recaptcha.rb
572
+ Recaptcha.configure do |config|
573
+ config.site_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy'
574
+ config.secret_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
575
+ config.verify_url = 'https://hcaptcha.com/siteverify'
576
+ config.api_server_url = 'https://hcaptcha.com/1/api.js'
577
+ config.response_limit = 100000
578
+ end
579
+ ```
580
+
581
+ hCaptcha uses a scoring system (higher number more likely to be a bot) which is inverse of the reCaptcha scoring system (lower number more likely to be a bot). As such, a `maximum_score` attribute is provided for use with hCaptcha.
582
+
583
+ ```ruby
584
+ result = verify_recaptcha(maximum_score: 0.7)
585
+ ```
586
+
587
+ | Option | Description |
588
+ |------------------|-------------|
589
+ | `:maximum_score` | Provide a threshold to meet or fall below. Threshold should be a float between 0 and 1 which will be tested as `score <= maximum_score`. (Default: `nil`) |
590
+
163
591
  ## Misc
164
592
  - Check out the [wiki](https://github.com/ambethia/recaptcha/wiki) and leave whatever you found valuable there.
165
593
  - [Add multiple widgets to the same page](https://github.com/ambethia/recaptcha/wiki/Add-multiple-widgets-to-the-same-page)