cloudflare-turnstile-rails 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7a94bbf6a29069c0d4f5f291ca3e80d368942aad2e03f20ae7b64091f9d900e
4
- data.tar.gz: 3156465de7ffac378b48b61250377536272acd9bd15998d61be263701fe92657
3
+ metadata.gz: 7a86ed02899dc2410a686143de9c188b3d15cfef8e4928bc87cc00bd1741fb58
4
+ data.tar.gz: 566026c1569d205f3a91beec381ec29729e6afe78084b9a6fb60ef4ceb0b3fad
5
5
  SHA512:
6
- metadata.gz: e7304843b0657e901e07f3264e6ab6676914af2b9d6a8f38a0778b53587cff1ba7454c71793fe45cac69bb7741e8264e3495337fb4544bd5f88551e569847a5d
7
- data.tar.gz: 808592cee1ae30ea1a6c19a9887bfe62d33979b991810bb078bec618a0e2e37ec4a93152d7f5552c4786245a25f4497e2ab7f0cdf925c387f0e5588468cb4a52
6
+ metadata.gz: 5a5a699a90fcb1a704de8a46ec57571c5219794d853d94a0d7bbac05e3e91dfa7ee20ddefaa1245d246a8b64bba082f58800821db4e2d4accc3ac16252eca204
7
+ data.tar.gz: e3655f9b725839b328f3699fd7dfb8def8f91d8011bd10dd4c8e4904cc6a12c61453a85bde4c666cd8d26185cb623da406ec634828785ff27d5833b6e5854594
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Rails](https://img.shields.io/badge/Rails-5.0%20to%208.1-D30001?logo=rubyonrails&logoColor=white)](https://github.com/vkononov/cloudflare-turnstile-rails/blob/main/Appraisals)
6
6
  [![Test Matrix](https://img.shields.io/github/actions/workflow/status/vkononov/cloudflare-turnstile-rails/test.yml?branch=main&label=Test%20Matrix&logo=github)](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/test.yml)
7
7
  [![Lint](https://img.shields.io/github/actions/workflow/status/vkononov/cloudflare-turnstile-rails/lint.yml?branch=main&label=Lint&logo=github)](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/lint.yml)
8
- [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
8
+ [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/license/MIT)
9
9
 
10
10
  Cloudflare Turnstile gem for Ruby on Rails with built-in Turbo and Turbolinks support and CSP compliance.
11
11
 
@@ -16,7 +16,7 @@ Supports **Rails 5.0 → latest** and **Ruby 2.6 → latest**, with the full Rai
16
16
  ## Features
17
17
 
18
18
  * **One‑line integration**: `<%= cloudflare_turnstile_tag %>` in views, `valid_turnstile?(model:)` in controllers — no extra wiring.
19
- * **Turbo & Turbo Streams aware**: Automatically re‑initializes widgets on `turbo:load`, `turbo:before-stream-render`, and DOM mutations.
19
+ * **Turbo & Turbo Streams aware**: Automatically re‑initializes widgets on `turbo:render` and `turbo:before-stream-render`.
20
20
  * **Legacy Turbolinks support**: Includes a helper for Turbolinks to handle remote form submissions with validation errors.
21
21
  * **CSP nonce support**: Honours Rails' `content_security_policy_nonce` for secure inline scripts.
22
22
  * **Rails Engine & Asset pipeline**: Ships a precompiled JS helper via Railtie — no manual asset setup.
@@ -154,6 +154,22 @@ Supports **Rails 5.0 → latest** and **Ruby 2.6 → latest**, with the full Rai
154
154
  end
155
155
  ```
156
156
 
157
+ The automatic message is a regular flash, so it survives exactly one redirect. If you **re-render the form** instead of redirecting, pass `flash: :now` to scope the message to that render, so it doesn't also appear on the next page the visitor opens:
158
+
159
+ ```ruby
160
+ def create
161
+ if valid_turnstile?(flash: :now)
162
+ # Passed: no message is set either way
163
+ redirect_to dashboard_path, notice: 'Success!'
164
+ else
165
+ # Failed: flash.now[:alert] is automatically set
166
+ render :new, status: :unprocessable_entity
167
+ end
168
+ end
169
+ ```
170
+
171
+ Passing a `model` is the other option for a re-rendered form: the failure goes to `model.errors` and no flash is set at all.
172
+
157
173
  * You may also pass additional **siteverify** parameters (e.g., `secret`, `response`, `remoteip`, `idempotency_key`) supported by Cloudflare's API:
158
174
  [Cloudflare Server-Side Validation Parameters](https://developers.cloudflare.com/turnstile/get-started/server-side-validation/#required-parameters)
159
175
 
@@ -302,9 +318,9 @@ Cloudflare provides dummy sitekeys and secret keys for development and testing.
302
318
  | Sitekey | Description | Visibility |
303
319
  |----------------------------|---------------------------------|------------|
304
320
  | `1x00000000000000000000AA` | Always passes | visible |
305
- | `2x00000000000000000000AB` | Always blocks | visible |
321
+ | `2x00000000000000000000AB` | Always fails | visible |
306
322
  | `1x00000000000000000000BB` | Always passes | invisible |
307
- | `2x00000000000000000000BB` | Always blocks | invisible |
323
+ | `2x00000000000000000000BB` | Always fails | invisible |
308
324
  | `3x00000000000000000000FF` | Forces an interactive challenge | visible |
309
325
 
310
326
  ### Dummy Secret Keys
@@ -393,10 +409,11 @@ If you run into any issues after upgrading Rails, please [open an issue](https:/
393
409
 
394
410
  ### Setup
395
411
 
396
- Install dependencies, linters, and prepare everything in one step:
412
+ Install the Ruby dependencies, then the JavaScript dependencies used by the ESLint step:
397
413
 
398
414
  ```bash
399
415
  bin/setup
416
+ npm install
400
417
  ```
401
418
 
402
419
  ### Running the Test Suite
@@ -414,13 +431,13 @@ bundle exec appraisal rake test
414
431
 
415
432
  ### Code Linting
416
433
 
417
- Enforce code style with RuboCop (latest Ruby only):
434
+ Run RuboCop and ESLint together (autocorrecting by default, or pass `--no-fix` to only report):
418
435
 
419
436
  ```bash
420
- bundle exec rubocop
437
+ bin/lint
421
438
  ```
422
439
 
423
- > **CI Note:** We run this via [.github/workflows/lint.yml](https://github.com/vkononov/cloudflare-turnstile-rails/blob/main/.github/workflows/lint.yml) on the latest Ruby only.
440
+ > **CI Note:** We run `bin/lint --no-fix` via [.github/workflows/lint.yml](https://github.com/vkononov/cloudflare-turnstile-rails/blob/main/.github/workflows/lint.yml) on the latest Ruby only.
424
441
 
425
442
  ### Generating Rails Apps Locally
426
443
 
@@ -461,4 +478,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/vkonon
461
478
 
462
479
  ## License
463
480
 
464
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
481
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/license/MIT).
@@ -17,10 +17,18 @@ module Cloudflare
17
17
  result
18
18
  end
19
19
 
20
- def valid_turnstile?(model: nil, **opts)
20
+ # flash: :keep carries the automatic failure message into the next request,
21
+ # for a redirect. flash: :now scopes it to the response being rendered. The
22
+ # keyword shadows the controller's own flash method, hence request.flash.
23
+ def valid_turnstile?(model: nil, flash: :keep, **opts)
21
24
  response = verify_turnstile(model: model, **opts)
22
25
  success = response.is_a?(VerificationResponse) && response.success?
23
- flash[:alert] = ErrorMessage.default if !success && model.nil?
26
+
27
+ if !success && model.nil?
28
+ store = flash == :now ? request.flash.now : request.flash
29
+ store[:alert] = ErrorMessage.default
30
+ end
31
+
24
32
  success
25
33
  end
26
34
 
@@ -48,9 +48,7 @@ module Cloudflare
48
48
 
49
49
  module Verification
50
50
  def self.verify(response: nil, secret: nil, remoteip: nil, idempotency_key: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
51
- if (response.nil? || response.strip.empty?) && ::Rails.env.test? && Rails.configuration.auto_populate_response_in_test_env # rubocop:disable Layout/LineLength
52
- response = 'dummy-response'
53
- end
51
+ response = 'dummy-response' if auto_populate_test_response?(response)
54
52
 
55
53
  secret ||= Rails.configuration.secret_key
56
54
  if secret.nil? || secret.strip.empty?
@@ -89,6 +87,13 @@ module Cloudflare
89
87
 
90
88
  VerificationResponse.new(json)
91
89
  end
90
+
91
+ def self.auto_populate_test_response?(response)
92
+ (response.nil? || response.strip.empty?) &&
93
+ ::Rails.env.test? &&
94
+ Rails.configuration.auto_populate_response_in_test_env
95
+ end
96
+ private_class_method :auto_populate_test_response?
92
97
  end
93
98
  end
94
99
  end
@@ -1,7 +1,7 @@
1
1
  module Cloudflare
2
2
  module Turnstile
3
3
  module Rails
4
- VERSION = '1.1.0'.freeze
4
+ VERSION = '1.2.0'.freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudflare-turnstile-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Kononov
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubygems_version: 4.0.10
86
+ rubygems_version: 3.7.2
87
87
  specification_version: 4
88
88
  summary: Cloudflare Turnstile gem for Rails with built-in Turbo and Turbolinks support
89
89
  and CSP compliance