turnstiled 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: da753c2bafbc45fa1b878958ab03e1131979c8cfa0299a0bb2cbdd48594da72b
4
+ data.tar.gz: 7f6a09f9e461ca65f612d1a5e1dc3259b6a88d70f6c542f42a85ad13b3b93845
5
+ SHA512:
6
+ metadata.gz: 99c3f6895fa3817093d3a3eb35da430d0877b49dcaf0e80b6a08524bb9552920fcdff7d7355525d5513ca370fba4a74cc8405a65d68951a0710a3c0aa53521bd
7
+ data.tar.gz: 802b6644f991b9fa2875bff02422f0865bdab972b320472c09cb4d50df3fcb85834c5b720cf1538e2f4f598fd2be3a45733e2727ce2b4ce9961c320ca5679d98
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2025 Displayful
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Turnstiled
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "turnstiled"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install turnstiled
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_tree ../../javascript/turnstiled .js
@@ -0,0 +1,20 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ export default class extends Controller {
4
+ static values = {
5
+ siteKey: String,
6
+ }
7
+
8
+ connect() {
9
+ turnstile.render(this.element, {
10
+ sitekey: this.siteKeyValue,
11
+ callback: this.callback.bind(this),
12
+ })
13
+ }
14
+
15
+ callback(token) {
16
+ this.dispatch('success', {
17
+ detail: { token },
18
+ })
19
+ }
20
+ }
@@ -0,0 +1,37 @@
1
+
2
+ function turnstile() {
3
+ }
4
+
5
+ turnstile.render = function(container, options) {
6
+ var element = document.createElement('div')
7
+ element.textContent = 'Cloudflare Turnstile'
8
+ element.style.background = 'yellow'
9
+ element.style.width = '100%'
10
+ element.style.height = '65px'
11
+ element.style.padding = '1rem'
12
+ element.style.display = 'flex'
13
+ element.style.alignItems = 'center'
14
+ element.style.justifyContent = 'center'
15
+
16
+ var input = Object.assign(document.createElement('input'), {
17
+ type: 'hidden',
18
+ name: 'cf-turnstile-response',
19
+ autocomplete: 'off',
20
+ ariaHidden: 'true',
21
+ })
22
+
23
+ container.appendChild(element)
24
+ container.appendChild(input)
25
+
26
+ setTimeout(function() {
27
+ element.textContent = 'Cloudflare Turnstile'
28
+ element.style.background = 'green'
29
+ element.style.color = 'white'
30
+
31
+ input.value = '1'
32
+
33
+ if (options.callback) {
34
+ options.callback('1')
35
+ }
36
+ }, 2000)
37
+ }
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turnstiled
4
+ module ControllerMethods
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def verify_turnstile_request(**)
9
+ before_action(:verify_turnstile_request, **)
10
+ end
11
+ end
12
+
13
+ def verify_turnstile_request
14
+ head :bad_request unless Turnstiled.verify(request)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module Turnstiled
2
+ class Engine < ::Rails::Engine
3
+ initializer 'turnstiled.controller' do
4
+ ActiveSupport.on_load(:action_controller_base) { include Turnstiled::ControllerMethods }
5
+ end
6
+
7
+ initializer 'turnstiled.controller' do
8
+ ActiveSupport.on_load(:action_view) { include Turnstiled::ViewHelper }
9
+ end
10
+
11
+ initializer 'turnstiled.assets' do |app|
12
+ app.config.assets.paths << root.join("app/javascript")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turnstiled
4
+ class Verifier
5
+ def verify(request)
6
+ return request.params['cf-turnstile-response'] == '1' if Turnstiled.mock
7
+
8
+ response = client.post('siteverify', {
9
+ secret: Turnstiled.site_secret,
10
+ response: request.params['cf-turnstile-response'],
11
+ remoteip: request.remote_ip,
12
+ })
13
+
14
+ response.body.fetch('success', false)
15
+ end
16
+
17
+ def client
18
+ @client ||= Faraday.new(client_options) do |f|
19
+ f.request :json
20
+ f.response :json
21
+ end
22
+ end
23
+
24
+ def client_options
25
+ {
26
+ url: 'https://challenges.cloudflare.com/turnstile/v0',
27
+ request: {
28
+ timeout: 3,
29
+ },
30
+ }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Turnstiled
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turnstiled
4
+ module ViewHelper
5
+ def turnstile_tag(size: 'compact', **options)
6
+ options = options.deep_merge(
7
+ class: "cf-turnstile #{options[:class]}",
8
+ data: {
9
+ sitekey: Turnstiled.site_key,
10
+ controller: 'turnstile',
11
+ turnstile_site_key_value: Turnstiled.site_key,
12
+ size:,
13
+ }
14
+ )
15
+
16
+ content_tag(:div, '', options)
17
+ end
18
+
19
+ def turnstile_javascript_tag
20
+ if ::Turnstiled.mock
21
+ javascript_include_tag('turnstiled/mock_turnstile_widget.js', defer: true, data: { turbo_track: 'reload' })
22
+ else
23
+ javascript_include_tag('https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit',
24
+ defer: true,
25
+ data: {
26
+ turbo_track: 'reload',
27
+ })
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/turnstiled.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "turnstiled/version"
2
+ require "turnstiled/engine"
3
+
4
+ module Turnstiled
5
+ autoload :ControllerMethods, 'turnstiled/controller_methods'
6
+ autoload :ViewHelper, 'turnstiled/view_helper'
7
+ autoload :Verifier, 'turnstiled/verifier'
8
+
9
+ mattr_accessor :site_key
10
+ mattr_accessor :site_secret
11
+ mattr_accessor :mock, default: Rails.env.local?
12
+
13
+ def self.verify(request)
14
+ Verifier.new.verify(request)
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turnstiled
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Displayful
8
+ - Henrik Hauge Bjørnskov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2025-02-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 7.0.0
21
+ - - "~>"
22
+ - !ruby/object:Gem::Version
23
+ version: '7.0'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: 7.0.0
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '7.0'
34
+ - !ruby/object:Gem::Dependency
35
+ name: faraday
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ description:
49
+ email:
50
+ - hb@displayful.co
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - app/assets/config/turnstiled/manifest.js
59
+ - app/javascript/turnstiled/controllers/turnstile_controller.js
60
+ - app/javascript/turnstiled/mock_turnstile_widget.js
61
+ - lib/turnstiled.rb
62
+ - lib/turnstiled/controller_methods.rb
63
+ - lib/turnstiled/engine.rb
64
+ - lib/turnstiled/verifier.rb
65
+ - lib/turnstiled/version.rb
66
+ - lib/turnstiled/view_helper.rb
67
+ homepage: https://github.com/displayful/turnstiled
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ allowed_push_host: https://rubygems.org
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.0.3.1
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Cloudflare Turnstile for Rails
91
+ test_files: []