Sha256: b055ecddd5bdf0f5712129b0941fbf37f3a3eabf0cb1d5256d00edfe415a386b

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

require 'rest-client'
require 'omniauth/basic'

module OmniAuth
  module Strategies
    class HttpBasic
      include OmniAuth::Strategy

      def initialize(app, name, endpoint = nil, headers = {}, &block)
        super
        @endpoint = endpoint
        @request_headers = headers
      end

      attr_reader :endpoint, :request_headers

      def request_phase
        if env['REQUEST_METHOD'] == 'GET'
          get_credentials
        else
          perform
        end
      end

      def title
        name.split('_').map{|s| s.capitalize}.join(' ')
      end

      def get_credentials
        OmniAuth::Form.build(:title => title) do
          text_field 'Username', 'username'
          password_field 'Password', 'password'
        end.to_response
      end

      def perform
        @response = perform_authentication(endpoint)
        @env['omniauth.auth'] = auth_hash
        @env['REQUEST_METHOD'] = 'GET'
        @env['PATH_INFO'] = "#{OmniAuth.config.path_prefix}/#{name}/callback"

        call_app!
      rescue RestClient::Request::Unauthorized => e
        fail!(:invalid_credentials, e)
      end

      def perform_authentication(uri, headers = request_headers)
        RestClient.get(uri, headers)
      end

      def callback_phase
        fail!(:invalid_credentials)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
oa-basic-0.3.2 lib/omniauth/strategies/http_basic.rb
oa-basic-0.3.0 lib/omniauth/strategies/http_basic.rb
oa-basic-0.3.0.rc3 lib/omniauth/strategies/http_basic.rb
omniauth-0.2.6 oa-basic/lib/omniauth/strategies/http_basic.rb
oa-basic-0.2.6 lib/omniauth/strategies/http_basic.rb
omniauth-0.2.5 oa-basic/lib/omniauth/strategies/http_basic.rb
oa-basic-0.2.5 lib/omniauth/strategies/http_basic.rb