Class: Resourceful::SSBEAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/collectd_server/ssbe_authenticator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ SSBEAuthenticator

Returns a new instance of SSBEAuthenticator.



10
11
12
13
14
# File 'lib/collectd_server/ssbe_authenticator.rb', line 10

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

Instance Attribute Details

#challengeObject (readonly)

Returns the value of attribute challenge.



8
9
10
# File 'lib/collectd_server/ssbe_authenticator.rb', line 8

def challenge
  @challenge
end

#domainObject (readonly)

Returns the value of attribute domain.



8
9
10
# File 'lib/collectd_server/ssbe_authenticator.rb', line 8

def domain
  @domain
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/collectd_server/ssbe_authenticator.rb', line 8

def password
  @password
end

#realmObject (readonly)

Returns the value of attribute realm.



8
9
10
# File 'lib/collectd_server/ssbe_authenticator.rb', line 8

def realm
  @realm
end

#usernameObject (readonly)

Returns the value of attribute username.



8
9
10
# File 'lib/collectd_server/ssbe_authenticator.rb', line 8

def username
  @username
end

Instance Method Details

#add_credentials_to(request) ⇒ Object



35
36
37
# File 'lib/collectd_server/ssbe_authenticator.rb', line 35

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

#can_handle?(request) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/collectd_server/ssbe_authenticator.rb', line 31

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

#credentials_for(request) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/collectd_server/ssbe_authenticator.rb', line 39

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

#update_credentials(challenge_response) ⇒ Object



16
17
18
19
# File 'lib/collectd_server/ssbe_authenticator.rb', line 16

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

#valid_for?(challenge_response) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/collectd_server/ssbe_authenticator.rb', line 21

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