Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
rack-iamnotarobot / lib / rack / iamnotarobot.rb
Size: Mime:
require 'json'
require 'net/http'
require File.expand_path '../iamnotarobot/helpers', __FILE__

module Rack
  class IAmNotARobot
    API_URL         = 'https://www.google.com/recaptcha/api/siteverify'
    RESPONSE_FIELD  = 'g-recaptcha-response'

    class << self
      attr_accessor :secret_key, :site_key, :test_mode

      def test_mode!(options = {})
        value = options[:return]
        self.test_mode = value.nil? ? true : options[:return]
      end

      def test_mode?
        !test_mode.nil?
      end
    end

    # Initialize the Rack Middleware. Some of the available options are:
    #   :site_key  -- your ReCaptcha API site key *(required)*
    #   :secret_key -- your ReCaptcha API secret key *(required)*
    #
    def initialize(app, options = {})
      @app = app
      self.class.secret_key = options[:secret_key]
      self.class.site_key = options[:site_key]
    end

    def call(env)
      dup._call(env)
    end

    def _call(env)
      request = Request.new(env)
      if request.params[RESPONSE_FIELD]
        if Rack::IAmNotARobot.test_mode?
          result = { 'success' => Rack::IAmNotARobot.test_mode, 'error-codes' => ['Good Work!'] }
        else
          result = verify(request.ip, request.params[RESPONSE_FIELD])
        end
        env.merge!('recaptcha.valid' => result['success'], 'recaptcha.msg' => result['error-codes'])
      end
      @app.call(env)
    end

    def verify(ip, response)
      params = {
        'secret' => Rack::IAmNotARobot.secret_key,
        'remoteip'   => ip,
        'response'   => response
      }

      uri  = URI.parse(Rack::IAmNotARobot::API_URL)

      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http.ssl_version = :SSLv3
      http.start do |client|
        request = Net::HTTP::Post.new(uri)
        request.set_form_data(params)

        response = client.request(request)
      end

      JSON.parse(response.body)
    end

  end
end