Module: Merb::RecaptchaMixin
- Defined in:
- lib/merb-recaptcha/controller_mixin.rb
Instance Method Summary collapse
-
#recaptcha_valid? ⇒ Boolean
This method checks challenge and response data via Recaptcha API server.
Instance Method Details
#recaptcha_valid? ⇒ Boolean
This method checks challenge and response data via Recaptcha API server.
-
It will return true, if filled captcha response is correct.
-
It will return false, if captcha is incorrect.
-
It will raise exception, if Recaptcha API server returns error.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/merb-recaptcha/controller_mixin.rb', line 11 def recaptcha_valid? return true if (Merb.testing? && !Merb::RecaptchaMixin.const_defined?(:DO_NOT_IGNORE_RECAPTCHA_IN_TESTING_ENV)) return true if Merb::Plugins.config[:merb_recaptcha][:allow_disable_by_cookie] && [:recaptcha_disabled]=="true" begin response = Net::HTTP.post_form(URI.parse("#{Merb::Recaptcha::API_VERIFY_SERVER}/verify"), { :privatekey => Merb::Plugins.config[:merb_recaptcha][:private_key], :remoteip => request.remote_ip, :challenge => params[:recaptcha_challenge_field], :response => params[:recaptcha_response_field] }) rescue Exception => e if Merb.env?(:production) || ENV['OFFLINE'] Merb.logger.error("when trying to connect to recaptcha, got #{e.}") return true; else raise end end answer, error = response.body.split.map { |s| s.chomp } if answer == "true" true else Merb.logger.fatal("recaptcha error: #{error} for #{request.remote_ip}") case error when "incorrect-captcha-sol" then false when "invalid-site-public-key" then raise Merb::Recaptcha::InvalidSitePublicKey when "invalid-site-private-key" then raise Merb::Recaptcha::InvalidSitePrivateKey when "invalid-request-cookie" then raise Merb::Recaptcha::InvalidRequestCookie when "verify-params-incorrect" then raise Merb::Recaptcha::VerifyParamsIncorrect when "invalid-referrer" then raise Merb::Recaptcha::InvalidReferrer else raise Merb::Recaptcha::GenericError end end end |