Commit 4de7f161bba93e0f636f12e6929cb92eadeb10cd

Authored by Evandro Junior
1 parent 19deee80

Added hotspot for Serpro api captcha

Showing 1 changed file with 9 additions and 24 deletions   Show diff stats
lib/noosfero/api/helpers.rb
@@ -29,7 +29,7 @@ require 'grape' @@ -29,7 +29,7 @@ require 'grape'
29 29
30 def logout_tmp_user 30 def logout_tmp_user
31 @current_tmp_user = nil 31 @current_tmp_user = nil
32 - end 32 + end
33 33
34 def current_user 34 def current_user
35 private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token']).to_s 35 private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token']).to_s
@@ -274,7 +274,7 @@ require 'grape' @@ -274,7 +274,7 @@ require 'grape'
274 unauthorized! unless current_user 274 unauthorized! unless current_user
275 end 275 end
276 276
277 - # Allows the anonymous captcha user authentication 277 + # Allows the anonymous captcha user authentication
278 # to pass the check. Used by the articles/vote to allow 278 # to pass the check. Used by the articles/vote to allow
279 # the vote without login 279 # the vote without login
280 def authenticate_allow_captcha! 280 def authenticate_allow_captcha!
@@ -412,29 +412,14 @@ require 'grape' @@ -412,29 +412,14 @@ require 'grape'
412 ########################################## 412 ##########################################
413 413
414 def test_captcha(remote_ip, params, environment) 414 def test_captcha(remote_ip, params, environment)
415 - d = environment.api_captcha_settings  
416 - return true unless d[:enabled] == true  
417 - msg_icve = _('Internal captcha validation error')  
418 - msg_eacs = 'Environment api_captcha_settings'  
419 - s = 500  
420 -  
421 - if d[:provider] == 'google'  
422 - return render_api_error!(msg_icve, s, nil, "#{msg_eacs} private_key not defined") if d[:private_key].nil?  
423 - return render_api_error!(msg_icve, s, nil, "#{msg_eacs} version not defined") unless d[:version] == 1 || d[:version] == 2  
424 - if d[:version] == 1  
425 - d[:verify_uri] ||= 'https://www.google.com/recaptcha/api/verify'  
426 - return verify_recaptcha_v1(remote_ip, d[:private_key], d[:verify_uri], params[:recaptcha_challenge_field], params[:recaptcha_response_field])  
427 - end  
428 - if d[:version] == 2  
429 - d[:verify_uri] ||= 'https://www.google.com/recaptcha/api/siteverify'  
430 - return verify_recaptcha_v2(remote_ip, d[:private_key], d[:verify_uri], params[:g_recaptcha_response])  
431 - end  
432 - end  
433 - if d[:provider] == 'serpro'  
434 - return render_api_error!(msg_icve, s, nil, "#{msg_eacs} verify_uri not defined") if d[:verify_uri].nil?  
435 - return verify_serpro_captcha(d[:serpro_client_id], params[:txtToken_captcha_serpro_gov_br], params[:captcha_text], d[:verify_uri]) 415 + captcha_plugin_enabled = @plugins.dispatch(:test_captcha, remote_ip, params, environment)
  416 + return true if captcha_plugin_enabled.size == 0
  417 + if captcha_plugin_enabled.size > 1
  418 + return render_api_error!(_("Error processing Captcha"), 500, nil, "More than one captcha plugin enabled")
436 end 419 end
437 - return render_api_error!(msg_icve, s, nil, "#{msg_eacs} provider not defined") 420 + test_result = captcha_plugin_enabled[0]
  421 + return true if test_result === true
  422 + render_api_error!(test_result[:user_message], test_result[:status], test_result[:log_message], test_result[:javascript_console_message])
438 end 423 end
439 424
440 def verify_recaptcha_v1(remote_ip, private_key, api_recaptcha_verify_uri, recaptcha_challenge_field, recaptcha_response_field) 425 def verify_recaptcha_v1(remote_ip, private_key, api_recaptcha_verify_uri, recaptcha_challenge_field, recaptcha_response_field)