friendly_captcha 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eddd73641f804f90fb306a848c11edcf5bc5344813668227bd98c00f347ced3d
4
+ data.tar.gz: a0b035d74b73bc75d1ba268d1d6c2cbfdda18dc954def48174de7c4b06e8e831
5
+ SHA512:
6
+ metadata.gz: 3743201b4c2c0240f50a71bd95699e1a67feb9a9dfe505cd24ba3acdb9485cada506a09fd126aee5a7840f8a0f251f66e3a4d6c3673caaf707a9a52fcde9826c
7
+ data.tar.gz: da9dfdf28683a4a9b678508fcc0ea27aeefab6e59abec1cbf4f2ffadbc688ae7b906e7927f9ac6373edac0dd660d24a30bf71be8a9a456bc0af6072ff924f9de
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-20
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at w.wohanka@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in friendly_captcha.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,87 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ friendly_captcha (0.2.0)
5
+ dry-configurable (~> 1.0)
6
+ dry-monads (~> 1.6)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.3)
12
+ coderay (1.1.3)
13
+ concurrent-ruby (1.1.10)
14
+ diff-lcs (1.5.0)
15
+ dry-configurable (1.3.0)
16
+ dry-core (~> 1.1)
17
+ zeitwerk (~> 2.6)
18
+ dry-core (1.1.0)
19
+ concurrent-ruby (~> 1.0)
20
+ logger
21
+ zeitwerk (~> 2.6)
22
+ dry-monads (1.8.3)
23
+ concurrent-ruby (~> 1.0)
24
+ dry-core (~> 1.1)
25
+ zeitwerk (~> 2.6)
26
+ json (2.12.2)
27
+ language_server-protocol (3.17.0.5)
28
+ lint_roller (1.1.0)
29
+ logger (1.7.0)
30
+ method_source (1.1.0)
31
+ parallel (1.27.0)
32
+ parser (3.3.8.0)
33
+ ast (~> 2.4.1)
34
+ racc
35
+ prism (1.4.0)
36
+ pry (0.14.2)
37
+ coderay (~> 1.1)
38
+ method_source (~> 1.0)
39
+ racc (1.7.3)
40
+ rainbow (3.1.1)
41
+ rake (13.0.6)
42
+ regexp_parser (2.10.0)
43
+ rspec (3.12.0)
44
+ rspec-core (~> 3.12.0)
45
+ rspec-expectations (~> 3.12.0)
46
+ rspec-mocks (~> 3.12.0)
47
+ rspec-core (3.12.0)
48
+ rspec-support (~> 3.12.0)
49
+ rspec-expectations (3.12.1)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.12.0)
52
+ rspec-mocks (3.12.1)
53
+ diff-lcs (>= 1.2.0, < 2.0)
54
+ rspec-support (~> 3.12.0)
55
+ rspec-support (3.12.0)
56
+ rubocop (1.75.7)
57
+ json (~> 2.3)
58
+ language_server-protocol (~> 3.17.0.2)
59
+ lint_roller (~> 1.1.0)
60
+ parallel (~> 1.10)
61
+ parser (>= 3.3.0.2)
62
+ rainbow (>= 2.2.2, < 4.0)
63
+ regexp_parser (>= 2.9.3, < 3.0)
64
+ rubocop-ast (>= 1.44.0, < 2.0)
65
+ ruby-progressbar (~> 1.7)
66
+ unicode-display_width (>= 2.4.0, < 4.0)
67
+ rubocop-ast (1.44.1)
68
+ parser (>= 3.3.7.2)
69
+ prism (~> 1.4)
70
+ ruby-progressbar (1.13.0)
71
+ unicode-display_width (3.1.4)
72
+ unicode-emoji (~> 4.0, >= 4.0.4)
73
+ unicode-emoji (4.0.4)
74
+ zeitwerk (2.6.6)
75
+
76
+ PLATFORMS
77
+ x86_64-linux
78
+
79
+ DEPENDENCIES
80
+ friendly_captcha!
81
+ pry (~> 0.14)
82
+ rake (>= 12.0)
83
+ rspec (~> 3.9)
84
+ rubocop (~> 1.0)
85
+
86
+ BUNDLED WITH
87
+ 2.6.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 hendrixfan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,237 @@
1
+ # FriendlyCaptcha Ruby Gem
2
+
3
+ A Ruby gem for integrating [Friendly Captcha v2](https://friendlycaptcha.com/) into your Ruby and Rails applications. Friendly Captcha is a privacy-first, GDPR-compliant captcha service that protects your websites from spam and abuse without tracking users or requiring them to solve puzzles.
4
+
5
+ ## Features
6
+
7
+ - **Privacy-First**: No user tracking or data collection
8
+ - **Accessibility**: Works seamlessly for all users
9
+ - **Easy Integration**: Drop-in replacement for other captcha services
10
+ - **Rails Support**: Built-in helpers for Rails applications
11
+ - **v2 API**: Uses the latest Friendly Captcha v2 API
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'friendly_captcha'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```bash
24
+ $ bundle install
25
+ ```
26
+
27
+ Or install it yourself as:
28
+
29
+ ```bash
30
+ $ gem install friendly_captcha
31
+ ```
32
+
33
+ ## Setup
34
+
35
+ ### 1. Get Your Keys
36
+
37
+ 1. Sign up for a [Friendly Captcha account](https://app.friendlycaptcha.eu/)
38
+ 2. Create a new application to get your:
39
+ - **Site Key** (for frontend widget)
40
+ - **API Key** (for backend verification)
41
+
42
+ ### 2. Configure the Gem
43
+
44
+ Create an initializer file `config/initializers/friendly_captcha.rb`:
45
+
46
+ ```ruby
47
+ FriendlyCaptcha.configure do |config|
48
+ config.api_key = ENV['FRIENDLY_CAPTCHA_API_KEY'] # Your API key
49
+ config.site_key = ENV['FRIENDLY_CAPTCHA_SITE_KEY'] # Your site key
50
+
51
+ # Optional: Use EU endpoint for GDPR compliance
52
+ # config.verification_endpoint = FriendlyCaptcha.config.eu_endpoint
53
+
54
+ # Optional: Enable strict mode (reject when verification fails)
55
+ # config.strict_mode = true
56
+ end
57
+ ```
58
+
59
+ ## Usage
60
+
61
+ ### In Views (Rails)
62
+
63
+ Add the captcha widget to your forms:
64
+
65
+ ```erb
66
+ <%= form_with url: "/submit" do |form| %>
67
+ <%= form.text_field :email, placeholder: "Email" %>
68
+
69
+ <!-- Add the Friendly Captcha widget -->
70
+ <%= friendly_captcha %>
71
+
72
+ <%= form.submit "Submit" %>
73
+ <% end %>
74
+ ```
75
+
76
+ #### Customization Options
77
+
78
+ ```erb
79
+ <!-- Basic usage -->
80
+ <%= friendly_captcha %>
81
+
82
+ <!-- With custom options -->
83
+ <%= friendly_captcha(
84
+ sitekey: "your-custom-site-key", # Override default site key
85
+ start: "focus", # auto, focus, or none
86
+ lang: "de", # Language code
87
+ form_field_name: "custom-field" # Custom form field name
88
+ ) %>
89
+ ```
90
+
91
+ ### In Controllers (Rails)
92
+
93
+ Verify the captcha response in your controller:
94
+
95
+ ```ruby
96
+ class ContactController < ApplicationController
97
+ def create
98
+ result = verify_friendly_captcha
99
+
100
+ if result[:success]
101
+ # Captcha verification successful
102
+ # Process the form...
103
+ redirect_to success_path, notice: "Thank you for your submission!"
104
+ else
105
+ # Captcha verification failed
106
+ flash.now[:error] = "Please complete the captcha verification."
107
+ render :new
108
+ end
109
+ end
110
+ end
111
+ ```
112
+
113
+ #### Advanced Verification
114
+
115
+ ```ruby
116
+ # With custom options
117
+ result = verify_friendly_captcha(
118
+ api_key: "custom-api-key",
119
+ sitekey: "custom-site-key",
120
+ field_name: "custom-field-name",
121
+ endpoint: FriendlyCaptcha.config.eu_endpoint
122
+ )
123
+
124
+ # Check the result
125
+ if result[:success]
126
+ puts "Verification successful!"
127
+ puts "Challenge timestamp: #{result[:challenge_timestamp]}" if result[:challenge_timestamp]
128
+ else
129
+ puts "Verification failed: #{result[:error]}"
130
+ puts "Error code: #{result[:error_code]}" if result[:error_code]
131
+ puts "Fallback mode: #{result[:fallback]}" if result[:fallback]
132
+ end
133
+ ```
134
+
135
+ ### Non-Rails Usage
136
+
137
+ For non-Rails Ruby applications:
138
+
139
+ ```ruby
140
+ require 'friendly_captcha'
141
+
142
+ # Configure
143
+ FriendlyCaptcha.configure do |config|
144
+ config.api_key = "your-api-key"
145
+ config.site_key = "your-site-key"
146
+ end
147
+
148
+ # Generate widget HTML
149
+ include FriendlyCaptcha::ViewHelpers
150
+ widget_html = friendly_captcha
151
+
152
+ # Verify response (in your request handler)
153
+ include FriendlyCaptcha::ControllerHelpers
154
+
155
+ # Mock params for non-Rails usage
156
+ def params
157
+ { 'frc-captcha-response' => 'user-response-token' }
158
+ end
159
+
160
+ result = verify_friendly_captcha
161
+ ```
162
+
163
+ ## Configuration Options
164
+
165
+ | Option | Description | Default |
166
+ |--------|-------------|---------|
167
+ | `api_key` | Your Friendly Captcha API key | `nil` |
168
+ | `site_key` | Your Friendly Captcha site key | `nil` |
169
+ | `verification_endpoint` | API endpoint for verification | `https://global.frcapi.com/api/v2/captcha/siteverify` |
170
+ | `eu_endpoint` | EU-only API endpoint | `https://eu.frcapi.com/api/v2/captcha/siteverify` |
171
+ | `strict_mode` | Reject when verification fails | `false` |
172
+ | `source` | Modern widget script URL | Latest v2 SDK |
173
+ | `source_compat` | Legacy browser script URL | Latest v2 SDK |
174
+
175
+ ## Widget Options
176
+
177
+ | Attribute | Description | Default |
178
+ |-----------|-------------|---------|
179
+ | `sitekey` | Your site key | From config |
180
+ | `start` | When to start solving (`auto`, `focus`, `none`) | `auto` |
181
+ | `lang` | Language code (e.g., `en`, `de`, `fr`) | Auto-detected |
182
+ | `form_field_name` | Name of the hidden form field | `frc-captcha-response` |
183
+
184
+ ## Error Handling
185
+
186
+ The gem includes intelligent error handling:
187
+
188
+ - **Non-strict mode** (default): If verification fails due to network issues or API problems, the captcha is accepted to avoid blocking legitimate users
189
+ - **Strict mode**: All captcha responses must be successfully verified
190
+
191
+ ```ruby
192
+ # Enable strict mode
193
+ FriendlyCaptcha.configure do |config|
194
+ config.strict_mode = true
195
+ end
196
+ ```
197
+
198
+ ## Testing
199
+
200
+ In your test environment, you might want to bypass captcha verification:
201
+
202
+ ```ruby
203
+ # In test environment
204
+ if Rails.env.test?
205
+ # Mock successful verification
206
+ allow_any_instance_of(FriendlyCaptcha::ControllerHelpers)
207
+ .to receive(:verify_friendly_captcha)
208
+ .and_return({ success: true })
209
+ end
210
+ ```
211
+
212
+ ## Development
213
+
214
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt.
215
+
216
+ To install this gem onto your local machine, run `bundle exec rake install`.
217
+
218
+ ## Migration from v1
219
+
220
+ If you're upgrading from Friendly Captcha v1:
221
+
222
+ 1. Update your API keys in the dashboard
223
+ 2. Change form field name from `frc-captcha-solution` to `frc-captcha-response`
224
+ 3. Update your configuration to use `api_key` instead of `secret`
225
+ 4. The widget will automatically use the new v2 scripts
226
+
227
+ ## Contributing
228
+
229
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hendrixfan/friendly_captcha. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hendrixfan/friendly_captcha/blob/main/CODE_OF_CONDUCT.md).
230
+
231
+ ## License
232
+
233
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
234
+
235
+ ## Code of Conduct
236
+
237
+ Everyone interacting in the FriendlyCaptcha project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hendrixfan/friendly_captcha/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "friendly_captcha"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/friendly_captcha/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "friendly_captcha"
7
+ spec.version = FriendlyCaptcha::VERSION
8
+ spec.authors = ["hendrixfan"]
9
+ spec.email = ["w.wohanka@gmail.com"]
10
+
11
+ spec.summary = "Ruby gem for Friendly Captcha v2 - Privacy-first bot protection"
12
+ spec.description = "A Ruby gem for integrating Friendly Captcha v2 into your applications. " \
13
+ "Friendly Captcha provides privacy-first, GDPR-compliant bot protection " \
14
+ "without tracking users or requiring them to solve puzzles."
15
+ spec.homepage = "https://github.com/hendrixfan/friendly_captcha"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 2.6.0"
18
+
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/hendrixfan/friendly_captcha"
22
+ spec.metadata["changelog_uri"] = "https://github.com/hendrixfan/friendly_captcha/blob/main/CHANGELOG.md"
23
+ spec.metadata["documentation_uri"] = "https://github.com/hendrixfan/friendly_captcha#readme"
24
+ spec.metadata["bug_tracker_uri"] = "https://github.com/hendrixfan/friendly_captcha/issues"
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
29
+ `git ls-files -z`.split("\x0").reject do |f|
30
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
31
+ end
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ # Runtime dependencies
38
+ spec.add_dependency 'dry-configurable', '~> 1.0'
39
+ spec.add_dependency 'dry-monads', '~> 1.6'
40
+
41
+ # Development dependencies
42
+ spec.add_development_dependency 'rake', '>= 12.0'
43
+ spec.add_development_dependency 'rspec', '~> 3.9'
44
+ spec.add_development_dependency 'pry', '~> 0.14'
45
+ spec.add_development_dependency 'rubocop', '~> 1.0'
46
+
47
+ # For more information and examples about making a new gem, checkout our
48
+ # guide at: https://bundler.io/guides/creating_gem.html
49
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "http_call"
4
+
5
+ module FriendlyCaptcha
6
+ module ControllerHelpers
7
+ def verify_friendly_captcha(options = {})
8
+ response_token = friendly_captcha_response_token(options)
9
+
10
+ return { success: false, error: "No captcha response provided" } if response_token.empty?
11
+
12
+ result = FriendlyCaptcha::HttpCall.new.call(
13
+ url: options[:endpoint] || FriendlyCaptcha.config.verification_endpoint,
14
+ body: {
15
+ response: response_token,
16
+ sitekey: options[:sitekey] || FriendlyCaptcha.config.site_key
17
+ },
18
+ headers: {
19
+ 'X-API-Key' => options[:api_key] || FriendlyCaptcha.config.api_key,
20
+ 'Content-Type' => 'application/json'
21
+ }
22
+ )
23
+
24
+ case result
25
+ when FriendlyCaptcha::HttpCall::Success
26
+ status_code, response_body = result.value!
27
+ parse_verification_response(status_code, response_body)
28
+ when FriendlyCaptcha::HttpCall::Failure
29
+ handle_verification_failure(result.failure)
30
+ end
31
+ end
32
+
33
+ def friendly_captcha_response_token(options = {})
34
+ field_name = options[:field_name] || 'frc-captcha-response'
35
+ response_param = params[field_name]
36
+
37
+ if response_param.is_a?(String)
38
+ response_param.strip
39
+ else
40
+ ''
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def parse_verification_response(status_code, response_body)
47
+ return handle_verification_failure("HTTP #{status_code}") unless status_code == 200
48
+
49
+ begin
50
+ parsed_response = JSON.parse(response_body)
51
+
52
+ if parsed_response['success']
53
+ {
54
+ success: true,
55
+ challenge_timestamp: parsed_response.dig('data', 'challenge', 'timestamp')
56
+ }
57
+ else
58
+ error_info = parsed_response['error'] || {}
59
+ {
60
+ success: false,
61
+ error: error_info['detail'] || 'Unknown verification error',
62
+ error_code: error_info['error_code']
63
+ }
64
+ end
65
+ rescue JSON::ParserError
66
+ handle_verification_failure("Invalid JSON response")
67
+ end
68
+ end
69
+
70
+ def handle_verification_failure(error)
71
+ if FriendlyCaptcha.config.strict_mode
72
+ {
73
+ success: false,
74
+ error: "Verification failed: #{error}"
75
+ }
76
+ else
77
+ # In non-strict mode, assume success when verification fails
78
+ # This prevents blocking users when the API is down
79
+ {
80
+ success: true,
81
+ error: "Verification failed but allowed in non-strict mode: #{error}",
82
+ fallback: true
83
+ }
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,49 @@
1
+ require 'net/http'
2
+ require 'openssl'
3
+ require 'dry/monads'
4
+
5
+ module FriendlyCaptcha
6
+ class HttpCall
7
+ include ::Dry::Monads[:try, :result]
8
+
9
+ Errors = [
10
+ ::SocketError,
11
+ ::SystemCallError,
12
+ ::Timeout::Error,
13
+ ::EOFError,
14
+ ::IOError,
15
+ ::Net::OpenTimeout,
16
+ ::Net::HTTPBadResponse,
17
+ ::Net::HTTPHeaderSyntaxError,
18
+ ::Net::ProtocolError,
19
+ ::Net::ReadTimeout,
20
+ ::Net::WriteTimeout,
21
+ ::OpenSSL::SSL::SSLError,
22
+ ::URI::InvalidURIError
23
+ ]
24
+
25
+ def call(url:, body:, **options)
26
+ Try[*Errors] { perform(url, body, **options) }.to_result.bind do |response|
27
+ Success(response)
28
+ end
29
+ end
30
+
31
+ def perform(url, body)
32
+ uri = URI(url)
33
+
34
+ options = {
35
+ use_ssl: uri.scheme.eql?('https'),
36
+ open_timeout: 500,
37
+ read_timeout: 500
38
+ }
39
+
40
+ ::Net::HTTP.start(uri.host, uri.port, options) do |http|
41
+ request = ::Net::HTTP::Post.new(uri, { 'Content-Type' => 'application/json' })
42
+ request.body = body.to_json
43
+ response = http.request(request)
44
+
45
+ [Integer(response.code, 10), response.body]
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,10 @@
1
+ module FriendlyCaptcha
2
+ class Railtie < Rails::Railtie
3
+ ActiveSupport.on_load(:action_view) do
4
+ include FriendlyCaptcha::ViewHelpers
5
+ end
6
+ ActiveSupport.on_load(:action_controller) do
7
+ include FriendlyCaptcha::ControllerHelpers
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyCaptcha
4
+ VERSION = "0.2.0"
5
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyCaptcha
4
+ module ViewHelpers
5
+ def friendly_captcha(opts = {})
6
+ site_key = opts[:sitekey] || opts[:site_key] || FriendlyCaptcha.config.site_key
7
+ start_mode = opts[:start] || "auto"
8
+ form_field_name = opts[:form_field_name] || "frc-captcha-response"
9
+ language = opts[:lang] || opts[:language]
10
+
11
+ raise ArgumentError, "site_key is required" unless site_key
12
+
13
+ # Build widget HTML attributes
14
+ widget_attrs = {
15
+ "class" => "frc-captcha",
16
+ "data-sitekey" => site_key,
17
+ "data-start" => start_mode
18
+ }
19
+
20
+ widget_attrs["data-form-field-name"] = form_field_name unless form_field_name == "frc-captcha-response"
21
+ widget_attrs["lang"] = language if language
22
+
23
+ # Convert attributes to HTML string
24
+ attr_string = widget_attrs.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")
25
+
26
+ html = <<-HTML
27
+ <div #{attr_string}></div>
28
+ HTML
29
+
30
+ # Add scripts - both modern and legacy support
31
+ html << %(<script type="module" src="#{FriendlyCaptcha.config.source}" async defer></script>\n)
32
+ html << %(<script nomodule src="#{FriendlyCaptcha.config.source_compat}" async defer></script>\n)
33
+
34
+ html.respond_to?(:html_safe) ? html.html_safe : html
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "friendly_captcha/version"
4
+ require_relative "friendly_captcha/view_helpers"
5
+ require_relative "friendly_captcha/controller_helpers"
6
+
7
+ if defined?(Rails)
8
+ require 'friendly_captcha/railtie'
9
+ end
10
+
11
+ require 'dry-configurable'
12
+
13
+ module FriendlyCaptcha
14
+ class Error < StandardError; end
15
+ extend Dry::Configurable
16
+
17
+ # v2 API uses API key instead of secret
18
+ setting :api_key
19
+ setting :site_key
20
+ setting :verification_endpoint, default: "https://global.frcapi.com/api/v2/captcha/siteverify"
21
+ setting :eu_endpoint, default: "https://eu.frcapi.com/api/v2/captcha/siteverify"
22
+ setting :source, default: 'https://cdn.jsdelivr.net/npm/@friendlycaptcha/sdk@0.1.22/site.min.js'
23
+ setting :source_compat, default: 'https://cdn.jsdelivr.net/npm/@friendlycaptcha/sdk@0.1.22/site.compat.min.js'
24
+ setting :strict_mode, default: false
25
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: friendly_captcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - hendrixfan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-monads
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.14'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ description: A Ruby gem for integrating Friendly Captcha v2 into your applications.
98
+ Friendly Captcha provides privacy-first, GDPR-compliant bot protection without tracking
99
+ users or requiring them to solve puzzles.
100
+ email:
101
+ - w.wohanka@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".ruby-version"
109
+ - CHANGELOG.md
110
+ - CODE_OF_CONDUCT.md
111
+ - Gemfile
112
+ - Gemfile.lock
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - bin/console
117
+ - bin/setup
118
+ - friendly_captcha.gemspec
119
+ - lib/friendly_captcha.rb
120
+ - lib/friendly_captcha/controller_helpers.rb
121
+ - lib/friendly_captcha/http_call.rb
122
+ - lib/friendly_captcha/railtie.rb
123
+ - lib/friendly_captcha/version.rb
124
+ - lib/friendly_captcha/view_helpers.rb
125
+ homepage: https://github.com/hendrixfan/friendly_captcha
126
+ licenses:
127
+ - MIT
128
+ metadata:
129
+ allowed_push_host: https://rubygems.org
130
+ homepage_uri: https://github.com/hendrixfan/friendly_captcha
131
+ source_code_uri: https://github.com/hendrixfan/friendly_captcha
132
+ changelog_uri: https://github.com/hendrixfan/friendly_captcha/blob/main/CHANGELOG.md
133
+ documentation_uri: https://github.com/hendrixfan/friendly_captcha#readme
134
+ bug_tracker_uri: https://github.com/hendrixfan/friendly_captcha/issues
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: 2.6.0
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubygems_version: 3.5.11
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Ruby gem for Friendly Captcha v2 - Privacy-first bot protection
154
+ test_files: []