Sha256: 4915bab16073061ec5383f4ff5fce88dd2983a4e33645fa9e226a33f24b1c171

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

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

module OmniAuth
  module Strategies
    class HttpBasic
      include OmniAuth::Strategy
      
      def initialize(app, name, endpoint, headers = {})
        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) 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

4 entries across 4 versions & 1 rubygems

Version Path
oa-basic-0.1.6 lib/omniauth/strategies/http_basic.rb
oa-basic-0.1.5 lib/omniauth/strategies/http_basic.rb
oa-basic-0.1.4 lib/omniauth/strategies/http_basic.rb
oa-basic-0.1.3 lib/omniauth/strategies/http_basic.rb