private_captcha 0.0.3 → 0.0.6
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 +4 -4
- data/README.md +23 -138
- data/lib/private_captcha/client.rb +5 -4
- data/lib/private_captcha/verify_output.rb +8 -2
- data/lib/private_captcha/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8cfbe57b68cd76d99caf830425f5c0b266807cd9f0b1290e7f973f10b3d6ff58
|
|
4
|
+
data.tar.gz: 9dc9b9cb80b0ed3efeaa15992233688836a07d0a2e26f8640f1801f023db0958
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9cfab92357a7f4a13d2b6f9aa63a2f523f29de75bcd0c2d79b252e49f65ca02cda3881ecadb7a2d5785eb9f85fa7e48df1561c4dad217718e532834ced62d8fc
|
|
7
|
+
data.tar.gz: 3cac0cac74a8e081a3a67e405481306d2ddfe01a8d9790de575fb45ee1ab40fa3bb08d4bcb86f2110bccc5b248c054434323cc371f0d814016352452113047f4
|
data/README.md
CHANGED
|
@@ -5,151 +5,36 @@
|
|
|
5
5
|
|
|
6
6
|
Ruby client for server-side verification of Private Captcha solutions.
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
Add this line to your application's Gemfile:
|
|
11
|
-
|
|
12
|
-
```ruby
|
|
13
|
-
gem 'private_captcha'
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
And then execute:
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
bundle install
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
Or install it yourself as:
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
gem install private_captcha
|
|
26
|
-
```
|
|
8
|
+
<mark>Please check the [official documentation](https://docs.privatecaptcha.com/docs/integrations/ruby/) for the in-depth and up-to-date information.</mark>
|
|
27
9
|
|
|
28
10
|
## Quick Start
|
|
29
11
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
client
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if result.success
|
|
42
|
-
puts 'Captcha verified successfully!'
|
|
43
|
-
else
|
|
44
|
-
puts "Verification failed: #{result.error_message}"
|
|
12
|
+
- Install gem `private_captcha`
|
|
13
|
+
```bash
|
|
14
|
+
gem install private_captcha
|
|
15
|
+
```
|
|
16
|
+
- Instantiate the client and call `verify()` method to check the captcha solution
|
|
17
|
+
```ruby
|
|
18
|
+
require 'private_captcha'
|
|
19
|
+
|
|
20
|
+
# Initialize the client with your API key
|
|
21
|
+
client = PrivateCaptcha::Client.new do |config|
|
|
22
|
+
config.api_key = 'your-api-key-here'
|
|
45
23
|
end
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
end
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
## Usage
|
|
52
|
-
|
|
53
|
-
### Web Framework Integration
|
|
54
|
-
|
|
55
|
-
#### Sinatra Example
|
|
56
|
-
|
|
57
|
-
```ruby
|
|
58
|
-
require 'sinatra'
|
|
59
|
-
require 'private_captcha'
|
|
60
|
-
|
|
61
|
-
client = PrivateCaptcha::Client.new do |config|
|
|
62
|
-
config.api_key = 'your-api-key'
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
post '/submit' do
|
|
24
|
+
|
|
25
|
+
# Verify a captcha solution
|
|
66
26
|
begin
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
rescue PrivateCaptcha::Error
|
|
73
|
-
status 400
|
|
74
|
-
'Captcha verification failed'
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
#### Rails Example
|
|
80
|
-
|
|
81
|
-
```ruby
|
|
82
|
-
class FormsController < ApplicationController
|
|
83
|
-
def submit
|
|
84
|
-
client = PrivateCaptcha::Client.new do |config|
|
|
85
|
-
config.api_key = 'your-api-key'
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
begin
|
|
89
|
-
client.verify_request(request)
|
|
90
|
-
# Process form data
|
|
91
|
-
render plain: 'Success!'
|
|
92
|
-
rescue PrivateCaptcha::Error
|
|
93
|
-
render plain: 'Captcha failed', status: :bad_request
|
|
27
|
+
result = client.verify('user-solution-from-frontend')
|
|
28
|
+
if result.ok?
|
|
29
|
+
puts 'Captcha verified successfully!'
|
|
30
|
+
else
|
|
31
|
+
puts "Verification failed: #{result.error_message}"
|
|
94
32
|
end
|
|
33
|
+
rescue PrivateCaptcha::Error => e
|
|
34
|
+
puts "Error: #{e.message}"
|
|
95
35
|
end
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
#### Rack Middleware
|
|
100
|
-
|
|
101
|
-
```ruby
|
|
102
|
-
require 'private_captcha'
|
|
103
|
-
|
|
104
|
-
use PrivateCaptcha::Middleware,
|
|
105
|
-
api_key: 'your-api-key',
|
|
106
|
-
failed_status_code: 403
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
## Configuration
|
|
110
|
-
|
|
111
|
-
### Client Options
|
|
112
|
-
|
|
113
|
-
```ruby
|
|
114
|
-
require 'private_captcha'
|
|
115
|
-
|
|
116
|
-
client = PrivateCaptcha::Client.new do |config|
|
|
117
|
-
config.api_key = 'your-api-key'
|
|
118
|
-
config.domain = PrivateCaptcha::Configuration::EU_DOMAIN # replace domain for self-hosting or EU isolation
|
|
119
|
-
config.form_field = 'private-captcha-solution' # custom form field name
|
|
120
|
-
config.max_backoff_seconds = 20 # maximum wait between retries
|
|
121
|
-
config.attempts = 5 # number of retry attempts
|
|
122
|
-
config.logger = Logger.new(STDOUT) # optional logger
|
|
123
|
-
end
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
### Non-standard backend domains
|
|
127
|
-
|
|
128
|
-
```ruby
|
|
129
|
-
require 'private_captcha'
|
|
130
|
-
|
|
131
|
-
# Use EU domain
|
|
132
|
-
eu_client = PrivateCaptcha::Client.new do |config|
|
|
133
|
-
config.api_key = 'your-api-key'
|
|
134
|
-
config.domain = PrivateCaptcha::Configuration::EU_DOMAIN # api.eu.privatecaptcha.com
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
# Or specify custom domain in case of self-hosting
|
|
138
|
-
custom_client = PrivateCaptcha::Client.new do |config|
|
|
139
|
-
config.api_key = 'your-api-key'
|
|
140
|
-
config.domain = 'your-custom-domain.com'
|
|
141
|
-
end
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### Retry Configuration
|
|
145
|
-
|
|
146
|
-
```ruby
|
|
147
|
-
result = client.verify(
|
|
148
|
-
'solution',
|
|
149
|
-
max_backoff_seconds: 15, # maximum wait between retries
|
|
150
|
-
attempts: 3 # number of retry attempts
|
|
151
|
-
)
|
|
152
|
-
```
|
|
36
|
+
```
|
|
37
|
+
- Integrate using Rack middleware or use with Sinatra or Rails with `client.verify_request()` helper
|
|
153
38
|
|
|
154
39
|
## Requirements
|
|
155
40
|
|
|
@@ -25,7 +25,7 @@ module PrivateCaptcha
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
28
|
-
def verify(solution, max_backoff_seconds: nil, attempts: nil)
|
|
28
|
+
def verify(solution, max_backoff_seconds: nil, attempts: nil, sitekey: nil)
|
|
29
29
|
raise EmptySolutionError if solution.nil? || solution.empty?
|
|
30
30
|
|
|
31
31
|
max_backoff = max_backoff_seconds || @config.max_backoff_seconds
|
|
@@ -52,7 +52,7 @@ module PrivateCaptcha
|
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
begin
|
|
55
|
-
response = do_verify(solution)
|
|
55
|
+
response = do_verify(solution, sitekey: sitekey)
|
|
56
56
|
error = nil
|
|
57
57
|
break
|
|
58
58
|
rescue RetriableError => e
|
|
@@ -81,7 +81,7 @@ module PrivateCaptcha
|
|
|
81
81
|
|
|
82
82
|
output = verify(solution)
|
|
83
83
|
|
|
84
|
-
unless output.
|
|
84
|
+
unless output.ok?
|
|
85
85
|
raise Error.new("captcha verification failed: #{output.error_message}",
|
|
86
86
|
trace_id: output.trace_id)
|
|
87
87
|
end
|
|
@@ -100,11 +100,12 @@ module PrivateCaptcha
|
|
|
100
100
|
end
|
|
101
101
|
|
|
102
102
|
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
103
|
-
def do_verify(solution)
|
|
103
|
+
def do_verify(solution, sitekey: nil)
|
|
104
104
|
request = Net::HTTP::Post.new(@endpoint)
|
|
105
105
|
request['X-Api-Key'] = @config.api_key
|
|
106
106
|
request['User-Agent'] = "private-captcha-ruby/#{VERSION}"
|
|
107
107
|
request['Content-Type'] = 'text/plain'
|
|
108
|
+
request['X-PC-Sitekey'] = sitekey if sitekey
|
|
108
109
|
request.body = solution
|
|
109
110
|
|
|
110
111
|
@logger.debug('Sending HTTP request') { "path=#{@endpoint.path} method=POST" }
|
|
@@ -15,7 +15,8 @@ module PrivateCaptcha
|
|
|
15
15
|
MAINTENANCE_MODE_ERROR = 9
|
|
16
16
|
TEST_PROPERTY_ERROR = 10
|
|
17
17
|
INTEGRITY_ERROR = 11
|
|
18
|
-
|
|
18
|
+
ORG_SCOPE_ERROR = 12
|
|
19
|
+
VERIFY_CODES_COUNT = 13
|
|
19
20
|
|
|
20
21
|
ERROR_MESSAGES = {
|
|
21
22
|
VERIFY_NO_ERROR => '',
|
|
@@ -29,7 +30,8 @@ module PrivateCaptcha
|
|
|
29
30
|
VERIFIED_BEFORE_ERROR => 'solution-verified-before',
|
|
30
31
|
MAINTENANCE_MODE_ERROR => 'maintenance-mode',
|
|
31
32
|
TEST_PROPERTY_ERROR => 'property-test',
|
|
32
|
-
INTEGRITY_ERROR => 'integrity-error'
|
|
33
|
+
INTEGRITY_ERROR => 'integrity-error',
|
|
34
|
+
ORG_SCOPE_ERROR => 'org-scope-error'
|
|
33
35
|
}.freeze
|
|
34
36
|
|
|
35
37
|
attr_accessor :success, :code, :origin, :timestamp
|
|
@@ -44,6 +46,10 @@ module PrivateCaptcha
|
|
|
44
46
|
@attempt = attempt
|
|
45
47
|
end
|
|
46
48
|
|
|
49
|
+
def ok?
|
|
50
|
+
@success == true && @code == VERIFY_NO_ERROR
|
|
51
|
+
end
|
|
52
|
+
|
|
47
53
|
def error_message
|
|
48
54
|
ERROR_MESSAGES.fetch(@code, 'error')
|
|
49
55
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: private_captcha
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Taras Kushnir
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-
|
|
11
|
+
date: 2025-12-23 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rack
|