recaptcha 5.0.0 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b146b489348963aec2bc4576903ff9d4f130fb65b7fe268558d4ce1430be0078
4
- data.tar.gz: 63c8a898a6e1e207e3988105a8916954e959fb77dc762d77c03ebd6c76bd95f1
3
+ metadata.gz: adcc7a846c1b2cbac386af0946cd3c810803860f85ca363e5453582354c618b0
4
+ data.tar.gz: b1ac10dd569bc7220b81505c9c6565d3db900a3c737ed1c184dcd1c96a12c963
5
5
  SHA512:
6
- metadata.gz: 3850b9716c496b63d25485ce9cf422a5610aeb5f348acbe6fd5bb0e20cd4834290890f6f8aa6927fe997085b19371f9414a57954176c2244f84e855d6f7ec9d0
7
- data.tar.gz: 492f92ab871182c685f4fa098cea1bdc616ae377b2df376c7e24e3ae65739c02871d7dd7fe95ba89d5ded14445cd480904cd180437482d67d6d98cffbc2f224c
6
+ metadata.gz: 8f9f6271ee715f1fbe543ed1b27cc8088678f5da8326718ad60587128e550fbb9073558fa147a4b608469da82af4cfd86ee255c39a7f69e0bd6b1d590ac109d8
7
+ data.tar.gz: 94fbd105e5b6f1cf23fd23cb5dac6040d9e642d5110beae4b9982c6a232f24323ed9ab145b3aa9ef7f6d5786537a20ea03dfacde5638c5f82d5921999c398bd7
@@ -1,5 +1,15 @@
1
1
  ## Next
2
2
 
3
+ ## 5.3.0
4
+ * turbolinks support
5
+
6
+ ## 5.2.0
7
+ * remove dependency on rails methods
8
+
9
+ ## 5.1.0
10
+ * Added default translations for rails/i18n
11
+ * use recaptcha.net for the script tag
12
+
3
13
  ## 5.0.0
4
14
  * Changed host to Recaptcha.net
5
15
  * Add v3 API support
data/README.md CHANGED
@@ -270,7 +270,8 @@ For more information, refer to the [v3 documentation](https://developers.google.
270
270
  ### Examples
271
271
 
272
272
  With v3, you can let all users log in without any intervention at all if their score is above some
273
- threshold, and only show a v2 checkbox recaptcha challenge if it is below the threshold:
273
+ threshold, and only show a v2 checkbox recaptcha challenge (fall back to v2) if it is below the
274
+ threshold:
274
275
 
275
276
  ```erb
276
277
 
@@ -286,7 +287,7 @@ threshold, and only show a v2 checkbox recaptcha challenge if it is below the th
286
287
  # app/controllers/sessions_controller.rb
287
288
  def create
288
289
  success = verify_recaptcha(action: 'login', minimum_score: 0.5)
289
- checkbox_success = verify_recaptcha_tags unless success
290
+ checkbox_success = verify_recaptcha unless success
290
291
  if success || checkbox_success
291
292
  # Perform action
292
293
  else
@@ -298,6 +299,8 @@ def create
298
299
  end
299
300
  ```
300
301
 
302
+ (You can also find this [example](demo/rails/app/controllers/v3_captchas_controller.rb) in the demo app.)
303
+
301
304
  Another example:
302
305
 
303
306
  ```erb
@@ -332,7 +335,7 @@ end
332
335
 
333
336
  Adds an inline script tag that calls `grecaptcha.execute` for the given `site_key` and `action` and
334
337
  calls the `callback` with the resulting response token. You need to verify this token with
335
- [`verify_recaptcha`](#verify_recaptcha_use_with_v3) in your controller in order to get the
338
+ [`verify_recaptcha`](#verify_recaptcha-use-with-v3) in your controller in order to get the
336
339
  [score](https://developers.google.com/recaptcha/docs/v3#score).
337
340
 
338
341
  By default, this inserts a hidden `<input type="hidden" class="g-recaptcha-response">` tag. The
@@ -390,6 +393,7 @@ but only accepts the following options:
390
393
  | `:script` | Same as setting both `:inline_script` and `:external_script`. (default: `true`). |
391
394
  | `: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`) |
392
395
  | `: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. |
396
+ | `:turbolinks` | 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 turbolinks. |
393
397
 
394
398
  [JavaScript resource (api.js) parameters](https://developers.google.com/recaptcha/docs/invisible#js_param):
395
399
 
@@ -437,20 +441,17 @@ It is recommended to pass `external_script: false` on all but one of the calls t
437
441
  `recaptcha` since you only need to include the script tag once for a given `site_key`.
438
442
 
439
443
  ## I18n support
440
- reCAPTCHA passes two types of error explanation to a linked model. It will use the I18n gem
441
- to translate the default error message if I18n is available. To customize the messages to your locale,
442
- add these keys to your I18n backend:
443
-
444
- `recaptcha.errors.verification_failed` error message displayed if the captcha words didn't match
445
- `recaptcha.errors.recaptcha_unreachable` displayed if a timeout error occured while attempting to verify the captcha
446
444
 
447
- Also you can translate API response errors to human friendly by adding translations to the locale (`config/locales/en.yml`):
445
+ reCAPTCHA supports the I18n gem (it comes with English translations)
446
+ To override or add new languages, add to `config/locales/*.yml`
448
447
 
449
448
  ```yaml
449
+ # config/locales/en.yml
450
450
  en:
451
451
  recaptcha:
452
452
  errors:
453
- verification_failed: 'Fail'
453
+ verification_failed: 'reCAPTCHA was incorrect, please try again.'
454
+ recaptcha_unreachable: 'reCAPTCHA verification server error, please try again.'
454
455
  ```
455
456
 
456
457
  ## Testing
@@ -501,4 +502,3 @@ verify_recaptcha secret_key: '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx'
501
502
  - Check out the [wiki](https://github.com/ambethia/recaptcha/wiki) and leave whatever you found valuable there.
502
503
  - [Add multiple widgets to the same page](https://github.com/ambethia/recaptcha/wiki/Add-multiple-widgets-to-the-same-page)
503
504
  - [Use Recaptcha with Devise](https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise)
504
-
@@ -99,7 +99,8 @@ module Recaptcha
99
99
  end
100
100
  end
101
101
 
102
- def self.api_verification(verify_hash, timeout: DEFAULT_TIMEOUT)
102
+ def self.api_verification(verify_hash, timeout: nil)
103
+ timeout ||= DEFAULT_TIMEOUT
103
104
  http = if configuration.proxy
104
105
  proxy_server = URI.parse(configuration.proxy)
105
106
  Net::HTTP::Proxy(proxy_server.host, proxy_server.port, proxy_server.user, proxy_server.password)
@@ -10,8 +10,9 @@ module Recaptcha
10
10
  def self.recaptcha_v3(options = {})
11
11
  site_key = options[:site_key] ||= Recaptcha.configuration.site_key!
12
12
  action = options.delete(:action) || raise(Recaptcha::RecaptchaError, 'action is required')
13
- id = options.delete(:id) || "g-recaptcha-response-" + dasherize_action(action)
13
+ id = options.delete(:id) || "g-recaptcha-response-" + dasherize_action(action)
14
14
  name = options.delete(:name) || "g-recaptcha-response[#{action}]"
15
+ turbolinks = options.delete(:turbolinks)
15
16
  options[:render] = site_key
16
17
  options[:script_async] ||= false
17
18
  options[:script_defer] ||= false
@@ -22,8 +23,13 @@ module Recaptcha
22
23
  end
23
24
  options[:class] = "g-recaptcha-response #{options[:class]}"
24
25
 
26
+ if turbolinks
27
+ options[:onload] = recaptcha_v3_execute_function_name(action)
28
+ end
25
29
  html, tag_attributes = components(options)
26
- if recaptcha_v3_inline_script?(options)
30
+ if turbolinks
31
+ html << recaptcha_v3_onload_script(site_key, action, callback, id, options)
32
+ elsif recaptcha_v3_inline_script?(options)
27
33
  html << recaptcha_v3_inline_script(site_key, action, callback, id, options)
28
34
  end
29
35
  case element
@@ -177,10 +183,10 @@ module Recaptcha
177
183
  <<-HTML
178
184
  <script#{nonce_attr}>
179
185
  // Define function so that we can call it again later if we need to reset it
186
+ // This executes reCAPTCHA and then calls our callback.
180
187
  function #{recaptcha_v3_execute_function_name(action)}() {
181
188
  grecaptcha.ready(function() {
182
189
  grecaptcha.execute('#{site_key}', {action: '#{action}'}).then(function(token) {
183
- //console.log('#{id}', token)
184
190
  #{callback}('#{id}', token)
185
191
  });
186
192
  });
@@ -190,6 +196,7 @@ module Recaptcha
190
196
 
191
197
  // Async variant so you can await this function from another async function (no need for
192
198
  // an explicit callback function then!)
199
+ // Returns a Promise that resolves with the response token.
193
200
  async function #{recaptcha_v3_async_execute_function_name(action)}() {
194
201
  return new Promise((resolve, reject) => {
195
202
  grecaptcha.ready(async function() {
@@ -203,6 +210,24 @@ module Recaptcha
203
210
  HTML
204
211
  end
205
212
 
213
+ private_class_method def self.recaptcha_v3_onload_script(site_key, action, callback, id, options = {})
214
+ nonce = options[:nonce]
215
+ nonce_attr = " nonce='#{nonce}'" if nonce
216
+
217
+ <<-HTML
218
+ <script#{nonce_attr}>
219
+ function #{recaptcha_v3_execute_function_name(action)}() {
220
+ grecaptcha.ready(function() {
221
+ grecaptcha.execute('#{site_key}', {action: '#{action}'}).then(function(token) {
222
+ #{callback}('#{id}', token)
223
+ });
224
+ });
225
+ };
226
+ #{recaptcha_v3_define_default_callback(callback) if recaptcha_v3_define_default_callback?(callback, action, options)}
227
+ </script>
228
+ HTML
229
+ end
230
+
206
231
  private_class_method def self.recaptcha_v3_inline_script?(options)
207
232
  !Recaptcha.skip_env?(options[:env]) &&
208
233
  options[:script] != false &&
@@ -215,7 +240,6 @@ module Recaptcha
215
240
  var element = document.getElementById(id);
216
241
  element.value = token;
217
242
  }
218
- </script>
219
243
  HTML
220
244
  end
221
245
 
@@ -281,13 +305,13 @@ module Recaptcha
281
305
  # Returns a camelized string that is safe for use in a JavaScript variable/function name.
282
306
  # sanitize_action_for_js('my/action') => 'MyAction'
283
307
  private_class_method def self.sanitize_action_for_js(action)
284
- action.to_s.gsub(/\W/, '_').camelize
308
+ action.to_s.gsub(/\W/, '_').split(/\/|_/).map(&:capitalize).join
285
309
  end
286
310
 
287
311
  # Returns a dasherized string that is safe for use as an HTML ID
288
312
  # dasherize_action('my/action') => 'my-action'
289
313
  private_class_method def self.dasherize_action(action)
290
- action.to_s.gsub(/\W/, '-').dasherize
314
+ action.to_s.gsub(/\W/, '-').tr('_', '-')
291
315
  end
292
316
 
293
317
  private_class_method def self.hash_to_query(hash)
@@ -9,5 +9,27 @@ module Recaptcha
9
9
  ActiveSupport.on_load(:action_controller) do
10
10
  include Recaptcha::Adapters::ControllerMethods
11
11
  end
12
+
13
+ initializer 'recaptcha' do |app|
14
+ Recaptcha::Railtie.instance_eval do
15
+ pattern = pattern_from app.config.i18n.available_locales
16
+
17
+ add("rails/locales/#{pattern}.yml")
18
+ end
19
+ end
20
+
21
+ class << self
22
+ protected
23
+
24
+ def add(pattern)
25
+ files = Dir[File.join(File.dirname(__FILE__), '../..', pattern)]
26
+ I18n.load_path.concat(files)
27
+ end
28
+
29
+ def pattern_from(args)
30
+ array = Array(args || [])
31
+ array.blank? ? '*' : "{#{array.join ','}}"
32
+ end
33
+ end
12
34
  end
13
35
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Recaptcha
4
- VERSION = '5.0.0'
4
+ VERSION = '5.3.0'
5
5
  end
@@ -0,0 +1,5 @@
1
+ en:
2
+ recaptcha:
3
+ errors:
4
+ verification_failed: reCAPTCHA verification failed, please try again.
5
+ recaptcha_unreachable: Oops, we failed to validate your reCAPTCHA response. Please try again.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason L Perry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-05 00:00:00.000000000 Z
11
+ date: 2020-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -154,6 +154,7 @@ files:
154
154
  - lib/recaptcha/rails.rb
155
155
  - lib/recaptcha/railtie.rb
156
156
  - lib/recaptcha/version.rb
157
+ - rails/locales/en.yml
157
158
  homepage: http://github.com/ambethia/recaptcha
158
159
  licenses:
159
160
  - MIT