new_google_recaptcha 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +90 -5
- data/lib/new_google_recaptcha.rb +32 -2
- data/lib/new_google_recaptcha/validator.rb +14 -2
- data/lib/new_google_recaptcha/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c8de28bc957a91e857b5c7705c61ffdd50d38e3fb73211c36b3b2422ecf9fed
|
4
|
+
data.tar.gz: 955a648bfa588942fd8a349e32625c1860d8043a5b40e05f12af3bedf69cb05d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7d045939f0d14bd2c6c1b455191a35687eb09595488a852fcd2ace7cca04edfc37d1de3350de975fafce041c02e7679c4fd6a1a29cbab677228cf49406d4441
|
7
|
+
data.tar.gz: 2a4b26180887e1289a5ef605393dbffe9f213f2ca6196768a131c63b1fb6a18c36174f03a7ad8a1d1eb3a1ebd95b3e4bc3490f2b11e61545bf2315c8ceb7f020
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Google Recaptcha v3 + Rails
|
2
2
|
|
3
|
-
Integrate Google Recaptcha v3 with Rails app.
|
3
|
+
Integrate Google Recaptcha v3 with Rails app.
|
4
4
|
|
5
5
|
Google Recaptcha console: https://www.google.com/recaptcha/admin#list
|
6
6
|
|
@@ -46,6 +46,32 @@ Recaptcha v3 documentation: https://developers.google.com/recaptcha/docs/v3
|
|
46
46
|
end
|
47
47
|
```
|
48
48
|
|
49
|
+
# or
|
50
|
+
# if you need to capture a humanity `score` from Google
|
51
|
+
# before you need to add a column for example `humanity_score` (type: float) where this score will be saved.
|
52
|
+
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
def create
|
56
|
+
@post = Post.new(post_params)
|
57
|
+
humanity_details =
|
58
|
+
NewGoogleRecaptcha.get_humanity_detailed(
|
59
|
+
params[:new_google_recaptcha_token],
|
60
|
+
"checkout",
|
61
|
+
NewGoogleRecaptcha.minimum_score,
|
62
|
+
@post
|
63
|
+
)
|
64
|
+
|
65
|
+
@post.humanity_score = humanity_details[:score]
|
66
|
+
|
67
|
+
if humanity_details[:is_human] && @post.save
|
68
|
+
redirect_to @post, notice: 'Post was successfully created.'
|
69
|
+
else
|
70
|
+
render :new
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
49
75
|
There are two mandatory arguments for `human?` method:
|
50
76
|
|
51
77
|
- `token` - token valid for your site
|
@@ -67,6 +93,14 @@ like this:
|
|
67
93
|
NewGoogleRecaptcha.human?(params[:new_google_recaptcha_token], "checkout")
|
68
94
|
```
|
69
95
|
|
96
|
+
`get_humanity_detailed` method acts like `human?` method, the only difference is that it returns following hash with three key-value pairs:
|
97
|
+
|
98
|
+
- `is_human` - whether actor is a human or not (same as result of `human?` method)
|
99
|
+
- `score` - actual humanity score from recaptcha response
|
100
|
+
- `model` - model which you trying to save
|
101
|
+
|
102
|
+
It could be handy if you want to store score in db or put it into logs or smth else.
|
103
|
+
|
70
104
|
Add to your navigation links `data-turbolinks="false"` to make it works with `turbolinks`.
|
71
105
|
|
72
106
|
## Installation
|
@@ -90,20 +124,71 @@ And edit new_google_recaptcha.rb and enter your site_key and secret_key.
|
|
90
124
|
|
91
125
|
## API
|
92
126
|
|
93
|
-
|
127
|
+
`NewGoogleRecaptcha.human?(token, model)` or `NewGoogleRecaptcha.get_humanity_detailed(token, model)` in contoller
|
94
128
|
|
95
129
|
- token is received from google, must be sent to backend
|
96
130
|
- model optional parameter. if you want to add error to model.
|
97
131
|
|
98
|
-
|
132
|
+
`<%= include_recaptcha_js %>` in layout (by using yield)
|
99
133
|
|
100
134
|
Include Google Recaptcha v3 JS into your Rails app. In head, right before `</head>`.
|
101
135
|
|
102
|
-
|
136
|
+
`<%= recaptcha_action(action_name) %>` in view
|
103
137
|
|
104
138
|
Action where recaptcha action was executed. Actions could be viewed in Admin console. More docs: https://developers.google.com/recaptcha/docs/v3. Action name could be "comments", "checkout", etc. Put any name and check scores in console.
|
105
139
|
|
140
|
+
## How to add to the Devise
|
141
|
+
|
142
|
+
Generate Devise controllers and views, and edit "create" method.
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
class Users::RegistrationsController < Devise::RegistrationsController
|
146
|
+
...
|
147
|
+
def create
|
148
|
+
build_resource(sign_up_params)
|
149
|
+
|
150
|
+
NewGoogleRecaptcha.human?(
|
151
|
+
params[:new_google_recaptcha_token],
|
152
|
+
"user",
|
153
|
+
NewGoogleRecaptcha.minimum_score,
|
154
|
+
resource) && resource.save
|
155
|
+
|
156
|
+
yield resource if block_given?
|
157
|
+
if resource.persisted?
|
158
|
+
if resource.active_for_authentication?
|
159
|
+
set_flash_message! :notice, :signed_up
|
160
|
+
sign_up(resource_name, resource)
|
161
|
+
respond_with resource, location: after_sign_up_path_for(resource)
|
162
|
+
else
|
163
|
+
set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
|
164
|
+
expire_data_after_sign_in!
|
165
|
+
respond_with resource, location: after_inactive_sign_up_path_for(resource)
|
166
|
+
end
|
167
|
+
else
|
168
|
+
clean_up_passwords resource
|
169
|
+
set_minimum_password_length
|
170
|
+
respond_with resource
|
171
|
+
end
|
172
|
+
end
|
173
|
+
```
|
174
|
+
|
175
|
+
## How to use in test or specs
|
176
|
+
|
177
|
+
At the end of the spec/rails_helper.rb put:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
module NewGoogleRecaptcha
|
181
|
+
def self.human?(*attrs)
|
182
|
+
true
|
183
|
+
end
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
Tests are located in `specs` folder and `test/dummy/tests` folder.
|
188
|
+
|
189
|
+
|
106
190
|
## I18n support
|
191
|
+
|
107
192
|
reCAPTCHA passes one types of error explanation to a linked model. It will use the I18n gem
|
108
193
|
to translate the default error message if I18n is available. To customize the messages to your locale,
|
109
194
|
add these keys to your I18n backend:
|
@@ -123,7 +208,6 @@ en:
|
|
123
208
|
|
124
209
|
- check everything works with turbolinks
|
125
210
|
- allow custom ID for input
|
126
|
-
- return score ?
|
127
211
|
- more tests
|
128
212
|
- handle exceptions with timeouts, json is not parsed
|
129
213
|
- add support for non-Rails apps
|
@@ -137,6 +221,7 @@ You are welcome to contribute.
|
|
137
221
|
* [gilcierweb](https://github.com/gilcierweb)
|
138
222
|
* [RoRElessar](https://github.com/RoRElessar)
|
139
223
|
* [rubyconvict](https://github.com/rubyconvict)
|
224
|
+
* [adelnabiullin](https://github.com/adelnabiullin)
|
140
225
|
|
141
226
|
## License
|
142
227
|
|
data/lib/new_google_recaptcha.rb
CHANGED
@@ -10,13 +10,37 @@ module NewGoogleRecaptcha
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.human?(token, action, minimum_score = self.minimum_score, model = nil)
|
13
|
-
is_valid =
|
13
|
+
is_valid =
|
14
|
+
NewGoogleRecaptcha::Validator.new(
|
15
|
+
token: token,
|
16
|
+
action: action,
|
17
|
+
minimum_score: minimum_score
|
18
|
+
).call
|
19
|
+
|
14
20
|
if model && !is_valid
|
15
21
|
model.errors.add(:base, self.i18n("new_google_recaptcha.errors.verification_human", "Looks like you are not a human"))
|
16
22
|
end
|
23
|
+
|
17
24
|
is_valid
|
18
25
|
end
|
19
26
|
|
27
|
+
def self.get_humanity_detailed(token, action, minimum_score = self.minimum_score, model = nil)
|
28
|
+
validator =
|
29
|
+
NewGoogleRecaptcha::Validator.new(
|
30
|
+
token: token,
|
31
|
+
action: action,
|
32
|
+
minimum_score: minimum_score
|
33
|
+
)
|
34
|
+
|
35
|
+
is_valid = validator.call
|
36
|
+
|
37
|
+
if model && !is_valid
|
38
|
+
model.errors.add(:base, self.i18n("new_google_recaptcha.errors.verification_human", "Looks like you are not a human"))
|
39
|
+
end
|
40
|
+
|
41
|
+
{ is_human: is_valid, score: validator.score, model: model }
|
42
|
+
end
|
43
|
+
|
20
44
|
def self.i18n(key, default)
|
21
45
|
if defined?(I18n)
|
22
46
|
I18n.translate(key, default: default)
|
@@ -25,7 +49,13 @@ module NewGoogleRecaptcha
|
|
25
49
|
end
|
26
50
|
end
|
27
51
|
|
52
|
+
def self.compose_uri(token)
|
53
|
+
URI(
|
54
|
+
"https://www.google.com/recaptcha/api/siteverify?"\
|
55
|
+
"secret=#{self.secret_key}&response=#{token}"
|
56
|
+
)
|
57
|
+
end
|
28
58
|
end
|
29
59
|
|
30
60
|
require_relative "new_google_recaptcha/view_ext"
|
31
|
-
require_relative "new_google_recaptcha/validator"
|
61
|
+
require_relative "new_google_recaptcha/validator"
|
@@ -2,9 +2,21 @@ require 'net/http'
|
|
2
2
|
|
3
3
|
module NewGoogleRecaptcha
|
4
4
|
class Validator
|
5
|
-
|
6
|
-
|
5
|
+
attr_reader :score
|
6
|
+
attr_reader :token, :action, :minimum_score
|
7
|
+
|
8
|
+
def initialize(token:, action:, minimum_score:)
|
9
|
+
@token = token
|
10
|
+
@action = action
|
11
|
+
@minimum_score = minimum_score
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
uri = NewGoogleRecaptcha.compose_uri(token)
|
7
16
|
result = JSON.parse(Net::HTTP.get(uri))
|
17
|
+
|
18
|
+
@score = result['score'].to_f
|
19
|
+
|
8
20
|
conditions = []
|
9
21
|
conditions << !!result['success']
|
10
22
|
conditions << (result['score'].to_f >= minimum_score)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: new_google_recaptcha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-08-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -161,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
161
161
|
- !ruby/object:Gem::Version
|
162
162
|
version: '0'
|
163
163
|
requirements: []
|
164
|
-
rubygems_version: 3.0.
|
164
|
+
rubygems_version: 3.0.4
|
165
165
|
signing_key:
|
166
166
|
specification_version: 4
|
167
167
|
summary: Google reCaptcha v3 + Rails
|