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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa810b760940cf717035ca4440a7fdc6fa770cde651ff6b987c0e10d0a471718
4
- data.tar.gz: 41af27ec27a704388b0bc6e946fbf3ee595a261df8b90355720a9f6cb9742131
3
+ metadata.gz: 2c8de28bc957a91e857b5c7705c61ffdd50d38e3fb73211c36b3b2422ecf9fed
4
+ data.tar.gz: 955a648bfa588942fd8a349e32625c1860d8043a5b40e05f12af3bedf69cb05d
5
5
  SHA512:
6
- metadata.gz: d378417f2010faed2b3bea508a54d17ddf66ca38766ba5d8c9ec3224a04742000c56a2121a1ec07217c86bfa59f2e36a7d84b680617cac2bb700093c0da39930
7
- data.tar.gz: 9a3dad7d9fb2236905e94a970e499935b52f2d9281e56a9612c3439ccc229aa545afc43fb8a3c0992de4c7faa0551a0047b89064be9fd9dfd8b1ee74f937bd73
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
- **NewGoogleRecaptcha.human?(token, model)** in contoller
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
- **<%= include_recaptcha_js %>** in layout (by using yield)
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
- **<%= recaptcha_action(action_name) %>** in view
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
 
@@ -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 = NewGoogleRecaptcha::Validator.valid?(token, action, minimum_score)
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
- def self.valid?(token, action, minimum_score)
6
- uri = URI("https://www.google.com/recaptcha/api/siteverify?secret=#{NewGoogleRecaptcha.secret_key}&response=#{token}")
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)
@@ -1,3 +1,3 @@
1
1
  module NewGoogleRecaptcha
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
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.0.0
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-01-03 00:00:00.000000000 Z
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.1
164
+ rubygems_version: 3.0.4
165
165
  signing_key:
166
166
  specification_version: 4
167
167
  summary: Google reCaptcha v3 + Rails