rails_cloudflare_turnstile 0.1.3 → 0.1.4

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: 945f56fd2e6ef412bd71d68a58ca63978499ac4948ac4cdb640852a0d7f13cbf
4
- data.tar.gz: 446c952679cf658fbc9e6e031043ca272ef8ef0ce5b3c6d40229461643725c07
3
+ metadata.gz: 74e8c3cd9c2aa0d7b6efc991fe4ab26d5e4386b308a391ab38b611857df26bde
4
+ data.tar.gz: 1dae15eaf445e80b32cbd507fddee58cbc34f1c71b9ff5f5487ceb04873593a5
5
5
  SHA512:
6
- metadata.gz: 6d27317d8fba8949675177a9fbbe798d0f57f7a26c2fad0ae72fe0b00337f6721dab1c298af562839a012d2f68764db99fe07fe2cb78ce44d6c996fa5ee0dc8b
7
- data.tar.gz: 5549a4a67796a43b4effb39c4d50bef488f47164f3efcfb285c3ce8f64da445d23c1aefe4adc318da4964f79ea28af125d21972001da79ab5d00291649be4bda
6
+ metadata.gz: a1c3a53ed7a4dc9291aa5eb5acc1895dfd06eeaae8ee213faaee5236f4d4e5222e71f3bc8752f55864ca6637d8bde92c846c4d15eb52002c6dbabf6f28132074
7
+ data.tar.gz: e502bd59212eeec1372f927f2c70206ebe6d7ea37a2c9947ce510d723038d1a049f9c724b3f530c37010d0ac2f9ef97cb2113f1af7c7ed92f454fc91b7273a18
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  ChangeLog
2
2
  =========
3
3
 
4
+ 0.1.4
5
+ -----
6
+ - add data-callback support; mock javascript
7
+
4
8
  0.1.3
5
9
  -----
6
10
  - Add mocked functionality in dev/test
@@ -0,0 +1 @@
1
+ <svg width="54" height="54" viewBox="0 0 54 54" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M27.3146 7.26134C22.346 7.11258 17.5089 8.87182 13.7968 12.1778l1.2307-6.74308-3.1939-.58208L9.67207 16.6889 21.5124 18.8498 22.0945 15.6559l-6.0801-1.1096c2.9126-2.5542 6.6454-3.9781 10.5192-4.0126C30.4073 10.4993 34.1648 11.8565 37.1224 14.3585s4.9188 5.9826 5.5269 9.8085C43.2573 27.9929 42.4718 31.91 40.4356 35.2057c-2.0362 3.2956-5.1877 5.7509-8.8813 6.9191S23.8704 43.0965 20.3094 41.5711c-3.5609-1.5254-6.456-4.2784-8.1586-7.7582-1.7026-3.4797-2.0995-7.455-1.1185-11.2027L7.89023 21.7861c-1.02566 3.9081-.81053 8.0386.61559 11.8191C9.93194 37.3856 12.4985 40.629 15.8498 42.8861c3.3514 2.257 7.3217 3.4159 11.361 3.3162C31.2501 46.1025 35.1584 44.7491 38.3943 42.3294c3.2359-2.4196 5.6393-5.7858 6.8771-9.632C46.5092 28.8511 46.5202 24.7151 45.3029 20.8623s-3.6026-7.2317-6.8256-9.6685c-3.2229-2.43693-7.124-3.81119-11.1627-3.93246z" style="fill: rgb(243, 128, 32)"></path><path clip-rule="evenodd" d="M38.8474 21.9189 35.9285 19 24.4761 30.4524l-4.5531-4.553L17 28.8224l7.4833 7.4832 2.923-2.923L27.3949 33.3713 38.8474 21.9189z" fill-rule="evenodd" style="fill: rgb(243, 128, 32);"></path></svg>
@@ -0,0 +1,20 @@
1
+ (function() {
2
+ function mock_cloudflare_turnstile_response() {
3
+ setTimeout(function() {
4
+ console.log("setting mock cloudflare turnstile to ✓");
5
+ for (let elem of document.getElementsByClassName("cf-turnstile")) {
6
+ elem.getElementsByTagName("p")[0].style.color = 'green';
7
+ elem.getElementsByTagName("p").innerHTML = "Mocked CAPTCHA succeeded"
8
+ if (elem.dataset.callback !== undefined) {
9
+ eval(elem.dataset.callback).call("mocked");
10
+ }
11
+ }
12
+ }, 1500);
13
+ }
14
+
15
+ if (document.readyState !== 'loading') {
16
+ mock_cloudflare_turnstile_response()
17
+ } else {
18
+ document.addEventListener('DOMContentLoaded', mock_cloudflare_turnstile_response);
19
+ }
20
+ })();
@@ -16,9 +16,12 @@ module RailsCloudflareTurnstile
16
16
  # Timeout for operations with Cloudflare
17
17
  attr_accessor :timeout
18
18
 
19
- # size for the widget ("regular" or "compact")
19
+ # size for the widget (:regular or :compact)
20
20
  attr_accessor :size
21
21
 
22
+ # theme for the widget (:auto, :light, or :dark)
23
+ attr_accessor :theme
24
+
22
25
  attr_accessor :enabled
23
26
 
24
27
  attr_accessor :mock_enabled
@@ -31,6 +34,7 @@ module RailsCloudflareTurnstile
31
34
  @mock_enabled = nil
32
35
  @timeout = 5.0
33
36
  @size = :regular
37
+ @theme = :auto
34
38
  @validation_url = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
35
39
  end
36
40
 
@@ -39,6 +43,7 @@ module RailsCloudflareTurnstile
39
43
  raise "Must set secret key" if @secret_key.nil?
40
44
  @size = @size.to_sym
41
45
  raise "Size must be one of ':regular' or ':compact'" unless [:regular, :compact].include? @size
46
+ raise "Theme must be one of :auto, :light, or :dark" unless [:auto, :light, :dark].include? @theme
42
47
  end
43
48
 
44
49
  def disabled?
@@ -0,0 +1,9 @@
1
+ module RailsCloudflareTurnstile
2
+ class Engine < ::Rails::Engine
3
+ initializer "rails_cloudflare_turnstile.precompile" do |app|
4
+ %w[javascripts images].each do |sub|
5
+ app.config.assets.paths << root.join("assets", sub).to_s
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,6 +1,6 @@
1
1
  module RailsCloudflareTurnstile
2
2
  class Railtie < ::Rails::Railtie
3
- initializer "rails_cloudflare_turnstile" do
3
+ initializer "rails_cloudflare_turnstile" do |app|
4
4
  ActiveSupport.on_load(:action_controller) do
5
5
  include RailsCloudflareTurnstile::ControllerHelpers
6
6
  end
@@ -8,6 +8,8 @@ module RailsCloudflareTurnstile
8
8
  ActiveSupport.on_load(:action_view) do
9
9
  include RailsCloudflareTurnstile::ViewHelpers
10
10
  end
11
+
12
+ app.config.assets.precompile += %w[mock_cloudflare_turnstile_api.js turnstile-logo.svg]
11
13
  end
12
14
  end
13
15
  end
@@ -1,3 +1,3 @@
1
1
  module RailsCloudflareTurnstile
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -2,39 +2,45 @@
2
2
 
3
3
  module RailsCloudflareTurnstile
4
4
  module ViewHelpers
5
- def cloudflare_turnstile(action: "other")
5
+ def cloudflare_turnstile(action: "other", data_callback: nil)
6
6
  if RailsCloudflareTurnstile.enabled?
7
7
  content_tag(:div, class: "cloudflare-turnstile") do
8
- concat turnstile_div(action)
8
+ concat turnstile_div(action, data_callback: data_callback)
9
9
  end
10
10
  elsif RailsCloudflareTurnstile.mock_enabled?
11
11
  content_tag(:div, class: "cloudflare-turnstile") do
12
- concat mock_turnstile_div(action)
12
+ concat mock_turnstile_div(action, data_callback: data_callback)
13
13
  end
14
14
  end
15
15
  end
16
16
 
17
17
  def cloudflare_turnstile_script_tag
18
- return nil unless RailsCloudflareTurnstile.enabled?
19
- content_tag(:script, :src => js_src, "async" => true) do
20
- ""
18
+ if RailsCloudflareTurnstile.enabled?
19
+ content_tag(:script, :src => js_src, "async" => true) do
20
+ ""
21
+ end
22
+ elsif RailsCloudflareTurnstile.mock_enabled?
23
+ content_tag(:script, :src => mock_js, "async" => true) do
24
+ ""
25
+ end
21
26
  end
22
27
  end
23
28
 
24
29
  private
25
30
 
26
- def turnstile_div(action)
31
+ def turnstile_div(action, data_callback: nil)
27
32
  config = RailsCloudflareTurnstile.configuration
28
- content_tag(:div, :class => "cf-turnstile", "data-sitekey" => site_key, "data-size" => config.size, "data-action" => action) do
33
+ content_tag(:div, :class => "cf-turnstile", "data-sitekey" => site_key, "data-size" => config.size, "data-action" => action, "data-callback" => data_callback, "data-theme" => config.theme) do
29
34
  ""
30
35
  end
31
36
  end
32
37
 
33
- def mock_turnstile_div(action)
34
- content_tag(:div, class: "cf-turnstile", style: "width: 300px; height: 65px: border: 1px solid gray") do
38
+ def mock_turnstile_div(action, data_callback: nil)
39
+ content_tag(:div, :class => "cf-turnstile", :style => "width: 300px; height: 65px; border: 1px solid gray; display: flex; flex-direction: row; justify-content: center; align-items: center; margin: 10px;", "data-callback" => data_callback) do
35
40
  [
36
41
  tag.input(type: "hidden", name: "cf-turnstile-response", value: "mocked"),
37
- content_tag(:p) do
42
+ image_tag("turnstile-logo.svg"),
43
+ content_tag(:p, style: "margin: 0") do
38
44
  "CAPTCHA goes here in production"
39
45
  end
40
46
  ].reduce(:<<)
@@ -48,5 +54,9 @@ module RailsCloudflareTurnstile
48
54
  def js_src
49
55
  "https://challenges.cloudflare.com/turnstile/v0/api.js"
50
56
  end
57
+
58
+ def mock_js
59
+ javascript_path "mock_cloudflare_turnstile_api.js"
60
+ end
51
61
  end
52
62
  end
@@ -1,4 +1,5 @@
1
1
  require "rails_cloudflare_turnstile/version"
2
+ require "rails_cloudflare_turnstile/engine"
2
3
  require "rails_cloudflare_turnstile/configuration"
3
4
  require "rails_cloudflare_turnstile/errors"
4
5
  require "rails_cloudflare_turnstile/controller_helpers"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_cloudflare_turnstile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Brown
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-17 00:00:00.000000000 Z
11
+ date: 2023-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -62,9 +62,12 @@ files:
62
62
  - CHANGELOG.md
63
63
  - README.md
64
64
  - Rakefile
65
+ - app/assets/images/turnstile-logo.svg
66
+ - app/assets/javascripts/mock_cloudflare_turnstile_api.js
65
67
  - lib/rails_cloudflare_turnstile.rb
66
68
  - lib/rails_cloudflare_turnstile/configuration.rb
67
69
  - lib/rails_cloudflare_turnstile/controller_helpers.rb
70
+ - lib/rails_cloudflare_turnstile/engine.rb
68
71
  - lib/rails_cloudflare_turnstile/errors.rb
69
72
  - lib/rails_cloudflare_turnstile/railtie.rb
70
73
  - lib/rails_cloudflare_turnstile/version.rb