diff --git a/lib/recaptcha_verification.rb b/lib/recaptcha_verification.rb index 524b522..bfc4cf1 100644 --- a/lib/recaptcha_verification.rb +++ b/lib/recaptcha_verification.rb @@ -10,7 +10,6 @@ class RecaptchaVerification if recaptcha_challenge_field == nil || recaptcha_response_field == nil return hash_error(_('Captcha validation error'), 500, nil, _('Missing captcha data')) end - verify_hash = { "privatekey" => private_key, "remoteip" => remote_ip, @@ -22,15 +21,12 @@ class RecaptchaVerification https.use_ssl = true request = Net::HTTP::Post.new(uri.path) request.set_form_data(verify_hash) - # begin - result = https.request(request).body.split("\n") - # rescue Exception => e - # return hash_error(_('Internal captcha validation error'), 500, nil, "Error validating Googles' recaptcha version 1: #{e.message}") - # end - return true if result[0] == "true" - return hash_error(_("Wrong captcha text, please try again"), 403, nil, "Error validating Googles' recaptcha version 1: #{result[1]}") if result[1] == "incorrect-captcha-sol" + body = https.request(request).body + captcha_result = JSON.parse(body) + return true if captcha_result["success"] + return hash_error(_("Wrong captcha text, please try again"), 403, nil, "Error validating Googles' recaptcha version 1: #{captcha_result["error-codes"]}") if captcha_result["error-codes"] == "incorrect-captcha-sol" #Catches all errors at the end - return hash_error(_("Internal recaptcha validation error"), 500, nil, "Error validating Googles' recaptcha version 1: #{result[1]}") + return hash_error(_("Internal recaptcha validation error"), 500, nil, "Error validating Googles' recaptcha version 1: #{captcha_result["error-codes"]}") end # return true or a hash with the error @@ -47,40 +43,10 @@ class RecaptchaVerification https.use_ssl = true request = Net::HTTP::Post.new(uri.path) request.set_form_data(verify_hash) - # begin - body = https.request(request).body - # rescue Exception => e - # return hash_error(_('Internal captcha validation error'), 500, nil, "recaptcha error: #{e.message}") - # end + body = https.request(request).body captcha_result = JSON.parse(body) return true if captcha_result["success"] return hash_error(_("Wrong captcha text, please try again"), 403, body, captcha_result["error-codes"]) end - # return true or a hash with the error - # :user_message, :status, :log_message, :javascript_console_message - def verify_serpro_captcha(client_id, token, captcha_text, verify_uri) - msg_icve = _('Internal captcha validation error') - msg_esca = 'Environment recaptcha_plugin_attributes' - return hash_error(msg_icve, 500, nil, "#{msg_esca} verify_uri not defined") if verify_uri.nil? - return hash_error(msg_icve, 500, nil, "#{msg_esca} client_id not defined") if client_id.nil? - return hash_error(_("Error processing token validation"), 500, nil, _("Missing Serpro's Captcha token")) unless token - return hash_error(_('Captcha text has not been filled'), 403) unless captcha_text - uri = URI(verify_uri) - http = Net::HTTP.new(uri.host, uri.port) - request = Net::HTTP::Post.new(uri.path) - verify_string = "#{client_id}&#{token}&#{captcha_text}" - request.body = verify_string - body = http.request(request).body - return true if body == '1' - return hash_error(_("Internal captcha validation error"), 500, body, "Unable to reach Serpro's Captcha validation service") if body == "Activity timed out" - return hash_error(_("Wrong captcha text, please try again"), 403) if body == '0' - return hash_error(_("Serpro's captcha token not found"), 500) if body == '2' - return hash_error(_("No data sent to validation server or other serious problem"), 500) if body == -1 - #Catches all errors at the end - return hash_error(_("Internal captcha validation error"), 500, nil, "Error validating Serpro's captcha service returned: #{body}") - end - - - end diff --git a/test/test_helper.rb b/test/test_helper.rb index 00a9642..a4b616f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -30,9 +30,7 @@ class ActiveSupport::TestCase remoteip: "127.0.0.1"} end - return_body = "{ - \"success\": #{pass} - }" + return_body = "{\"success\": #{pass} }" stub_request(:post, @verify_uri). with(:body => body, diff --git a/test/unit/recaptcha_verification_test.rb b/test/unit/recaptcha_verification_test.rb index 6df831e..c2069bd 100644 --- a/test/unit/recaptcha_verification_test.rb +++ b/test/unit/recaptcha_verification_test.rb @@ -17,17 +17,16 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase @params = {} @params[:remoteip] = @remoteip if version.to_i == 1 - #wont go to google thanks to webmock + # won't go to google thanks to webmock @verify_uri = 'https://www.google.com/recaptcha/api/verify' @params[:privatekey] = @environment.recaptcha_private_key @params[:challenge] = "challenge" @params[:response] = "response" - @params[:recaptcha_challenge_field] = @params[:challenge] @params[:recaptcha_response_field] = @params[:response] end if version.to_i == 2 - #wont go to google thanks to webmock + # won't go to google thanks to webmock @verify_uri = 'https://www.google.com/recaptcha/api/siteverify' @params[:secret] = @environment.recaptcha_private_key @params[:response] = "response" @@ -55,7 +54,8 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase setup_captcha(version) validate_captcha(version) r = RecaptchaPlugin.new.test_captcha(@remoteip, @params, @environment) - assert r + assert_not_kind_of Hash, r + assert_equal true, r end should 'fail recaptcha version 1' do @@ -63,16 +63,16 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase setup_captcha(version) validate_captcha(version, false) r = RecaptchaPlugin.new.test_captcha(@remoteip, @params, @environment) - assert r + assert_kind_of Hash, r end should 'pass recaptcha version 2' do version = 2 setup_captcha(version) validate_captcha(version) - rp = RecaptchaPlugin.new - r = rp.test_captcha(@remoteip, @params, @environment) - assert r + r = RecaptchaPlugin.new.test_captcha(@remoteip, @params, @environment) + assert_not_kind_of Hash, r + assert_equal true, r end should 'fail recaptcha version 2' do @@ -80,6 +80,7 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase setup_captcha(version) validate_captcha(version, false) r = RecaptchaPlugin.new.test_captcha(@remoteip, @params, @environment) + assert_kind_of Hash, r assert_equal r[:user_message], _("Wrong captcha text, please try again") end @@ -109,7 +110,6 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase assert_equal json["message"], _("Wrong captcha text, please try again") end - should 'fail captcha if user has not filled recaptcha_verify_uri v1 text' do version = 1 setup_captcha(version) @@ -124,17 +124,31 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase article = create_article('Article 1') params = {} params[:value] = 1 + post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 401, last_response.status + end + should 'not perform a vote if recaptcha 1 fails' do + version = 1 + setup_captcha(version) + validate_captcha(version, false) + post "/api/v1/login-captcha?#{@params.to_query}" + json = JSON.parse(last_response.body) + article = create_article('Article 1') + params = {} + params[:private_token] = json['private_token'] + params[:value] = 1 post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 401, last_response.status end - should 'perform a vote on an article identified by id' do - version = 2 + should 'perform a vote on an article identified by id using recaptcha 1' do + version = 1 setup_captcha(version) validate_captcha(version) - post "/api/v1/login-captcha?#{params.to_query}" + post "/api/v1/login-captcha?#{@params.to_query}" json = JSON.parse(last_response.body) article = create_article('Article 1') params = {} @@ -146,10 +160,10 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase assert_equal true, json['vote'] end - should 'not perform a vote if recaptcha 2 fails' do + should 'perform a vote on an article identified by id using recaptcha 2' do version = 2 setup_captcha(version) - validate_captcha(version, false) + validate_captcha(version) post "/api/v1/login-captcha?#{@params.to_query}" json = JSON.parse(last_response.body) article = create_article('Article 1') @@ -158,11 +172,12 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase params[:value] = 1 post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" json = JSON.parse(last_response.body) - assert_equal 401, last_response.status + assert_not_equal 401, last_response.status + assert_equal true, json['vote'] end - should 'not perform a vote if recaptcha 1 fails' do - version = 1 + should 'not perform a vote if recaptcha 2 fails' do + version = 2 setup_captcha(version) validate_captcha(version, false) post "/api/v1/login-captcha?#{@params.to_query}" @@ -176,5 +191,4 @@ class RecaptchaVerificationTest < ActiveSupport::TestCase assert_equal 401, last_response.status end - end -- libgit2 0.21.2