Commit 486eb67850ce2feb8acaf5118ae2c0685202bc52
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
erge changes
Showing
3 changed files
with
38 additions
and
11 deletions
Show diff stats
app/models/environment.rb
... | ... | @@ -326,7 +326,7 @@ class Environment < ActiveRecord::Base |
326 | 326 | |
327 | 327 | settings_items :signup_welcome_screen_body, :type => String |
328 | 328 | |
329 | - #Captcha setings | |
329 | + #Captcha settings | |
330 | 330 | settings_items :api_captcha_settings, :type => ActiveSupport::HashWithIndifferentAccess, :default => {} |
331 | 331 | |
332 | 332 | def has_custom_welcome_screen? | ... | ... |
lib/noosfero/api/helpers.rb
... | ... | @@ -255,7 +255,6 @@ |
255 | 255 | def period(from_date, until_date) |
256 | 256 | begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date |
257 | 257 | end_period = until_date.nil? ? DateTime.now : until_date |
258 | - | |
259 | 258 | begin_period..end_period |
260 | 259 | end |
261 | 260 | |
... | ... | @@ -271,18 +270,20 @@ |
271 | 270 | if d[:provider] == 'google' |
272 | 271 | raise ArgumentError, "Environment api_captcha_settings private_key not defined" if d[:private_key].nil? |
273 | 272 | raise ArgumentError, "Environment api_captcha_settings version not defined" unless d[:version] == 1 || d[:version] == 2 |
274 | - raise ArgumentError, "Environment api_captcha_settings verify_uri not defined" if d[:verify_uri].nil? | |
275 | 273 | if d[:version] == 1 |
274 | + d[:verify_uri] ||= 'https://www.google.com/recaptcha/api/verify' | |
276 | 275 | return verify_recaptcha_v1(remote_ip, d[:private_key], d[:verify_uri], params[:recaptcha_challenge_field], params[:recaptcha_response_field]) |
277 | 276 | end |
278 | 277 | if d[:version] == 2 |
278 | + d[:verify_uri] ||= 'https://www.google.com/recaptcha/api/siteverify' | |
279 | 279 | return verify_recaptcha_v2(remote_ip, d[:private_key], d[:verify_uri], params[:g_recaptcha_response]) |
280 | 280 | end |
281 | 281 | end |
282 | - | |
283 | 282 | if d[:provider] == 'serpro' |
284 | - #TODO ADD SERPRO's CAPTCHA | |
283 | + d[:verify_uri] ||= 'http://captcha2.servicoscorporativos.serpro.gov.br/captchavalidar/1.0.0/validar' | |
284 | + return verify_serpro_captcha(d[:serpro_client_id], params[:txtToken_captcha_serpro_gov_br], params[:captcha_text], d[:verify_uri]) | |
285 | 285 | end |
286 | + raise ArgumentError, "Environment api_captcha_settings provider not defined" | |
286 | 287 | end |
287 | 288 | |
288 | 289 | def verify_recaptcha_v1(remote_ip, private_key, api_recaptcha_verify_uri, recaptcha_challenge_field, recaptcha_response_field) |
... | ... | @@ -306,7 +307,6 @@ |
306 | 307 | end |
307 | 308 | |
308 | 309 | def verify_recaptcha_v2(remote_ip, private_key, api_recaptcha_verify_uri, g_recaptcha_response) |
309 | - | |
310 | 310 | if g_recaptcha_response == nil |
311 | 311 | return _('Missing captcha data') |
312 | 312 | end |
... | ... | @@ -325,6 +325,19 @@ |
325 | 325 | captcha_result["success"] ? true : captcha_result |
326 | 326 | end |
327 | 327 | |
328 | + def verify_serpro_captcha(client_id, token, captcha_text, verify_uri) | |
329 | + if token == nil || captcha_text == nil | |
330 | + return _('Missing captcha data') | |
331 | + end | |
332 | + uri = URI(verify_uri) | |
333 | + http = Net::HTTP.new(uri.host, uri.port) | |
334 | + request = Net::HTTP::Post.new(uri.path) | |
335 | + verify_string = "#{client_id}&#{token}&#{captcha_text}" | |
336 | + request.body = verify_string | |
337 | + body = http.request(request).body | |
338 | + body == '1' ? true : body | |
339 | + end | |
340 | + | |
328 | 341 | end |
329 | 342 | end |
330 | 343 | end | ... | ... |
test/unit/api/helpers_test.rb
... | ... | @@ -164,7 +164,8 @@ class APIHelpersTest < ActiveSupport::TestCase |
164 | 164 | |
165 | 165 | should 'do not test captcha when there are no settings' do |
166 | 166 | environment = Environment.new |
167 | - assert test_captcha("127.0.0.1", {}, environment) | |
167 | + stubs(:environment).returns(environment) | |
168 | + assert test_captcha("127.0.0.1", {}) | |
168 | 169 | end |
169 | 170 | |
170 | 171 | should 'do not test captcha when captcha is disabled on settings' do |
... | ... | @@ -172,10 +173,10 @@ class APIHelpersTest < ActiveSupport::TestCase |
172 | 173 | environment.api_captcha_settings = { |
173 | 174 | enabled: false, |
174 | 175 | } |
175 | - assert test_captcha("127.0.0.1", {}, environment) | |
176 | + stubs(:environment).returns(environment) | |
177 | + assert test_captcha("127.0.0.1", {}) | |
176 | 178 | end |
177 | 179 | |
178 | - | |
179 | 180 | should 'fail display recaptcha v1' do |
180 | 181 | environment = Environment.new |
181 | 182 | environment.api_captcha_settings = { |
... | ... | @@ -186,7 +187,8 @@ class APIHelpersTest < ActiveSupport::TestCase |
186 | 187 | public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', |
187 | 188 | verify_uri: 'https://www.google.com/recaptcha/api/verify', |
188 | 189 | } |
189 | - assert_equal test_captcha("127.0.0.1", {}, environment), "Missing captcha data" | |
190 | + stubs(:environment).returns(environment) | |
191 | + assert_equal test_captcha("127.0.0.1", {}), "Missing captcha data" | |
190 | 192 | end |
191 | 193 | |
192 | 194 | should 'fail display recaptcha v2' do |
... | ... | @@ -199,7 +201,19 @@ class APIHelpersTest < ActiveSupport::TestCase |
199 | 201 | public_key: '6LdsWAcTAAAAAChTUUD6yu9fCDhdIZzNd7F53zf-', |
200 | 202 | verify_uri: 'https://www.google.com/recaptcha/api/siteverify', |
201 | 203 | } |
202 | - assert_equal test_captcha("127.0.0.1", {}, environment), "Missing captcha data" | |
204 | + stubs(:environment).returns(environment) | |
205 | + assert_equal test_captcha("127.0.0.1", {}), "Missing captcha data" | |
206 | + end | |
207 | + | |
208 | + should 'fail display Serpro captcha' do | |
209 | + environment = Environment.new | |
210 | + environment.api_captcha_settings = { | |
211 | + enabled: true, | |
212 | + provider: 'serpro', | |
213 | + serpro_client_id: '0000000000000000', | |
214 | + } | |
215 | + stubs(:environment).returns(environment) | |
216 | + assert_equal test_captcha("127.0.0.1", {}), "Missing captcha data" | |
203 | 217 | end |
204 | 218 | |
205 | 219 | should 'render not_found if endpoint is unavailable' do | ... | ... |