Sha256: 42b7537bdccd0bd9d2b05c9077f3e15b8605b7382a1eee287b53315059b62554

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Resourceful

  class SSBEAuthenticator
    require 'httpauth'
    require 'addressable/uri'


    attr_reader :username, :password, :realm, :domain, :challenge

    def initialize(username, password)
      @username, @password = username, password
      @realm = 'SystemShepherd'
      @domain = nil
    end

    def update_credentials(challenge_response)
      @domain = Addressable::URI.parse(challenge_response.uri).host
      @challenge = HTTPAuth::Digest::Challenge.from_header(challenge_response.header['WWW-Authenticate'].first)
    end

    def valid_for?(challenge_response)
      return false unless challenge_header = challenge_response.header['WWW-Authenticate']
      begin
        challenge = HTTPAuth::Digest::Challenge.from_header(challenge_header.first)
      rescue HTTPAuth::UnwellformedHeader
        return false
      end
      challenge.realm == @realm
    end

    def can_handle?(request)
      Addressable::URI.parse(request.uri).host == @domain
    end

    def add_credentials_to(request)
      request.header['Authorization'] = credentials_for(request)
    end

    def credentials_for(request)
      HTTPAuth::Digest::Credentials.from_challenge(@challenge, 
                                                   :username => @username,
                                                   :password => @password,
                                                   :method   => request.method.to_s.upcase,
                                                   :uri      => Addressable::URI.parse(request.uri).path).to_header
    end

  end

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
absperf-collectd_server-0.1.0 lib/collectd_server/ssbe_authenticator.rb
absperf-collectd_server-0.1.1 lib/collectd_server/ssbe_authenticator.rb
absperf-dm-ssbe-adapter-0.10.0 lib/dm-ssbe-adapter/ssbe_authenticator.rb