Class: Resourceful::DigestAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/resourceful/authentication_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(realm, username, password) ⇒ DigestAuthenticator

Returns a new instance of DigestAuthenticator.



66
67
68
69
70
# File 'lib/resourceful/authentication_manager.rb', line 66

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

Instance Attribute Details

#challengeObject (readonly)

Returns the value of attribute challenge.



64
65
66
# File 'lib/resourceful/authentication_manager.rb', line 64

def challenge
  @challenge
end

#domainObject (readonly)

Returns the value of attribute domain.



64
65
66
# File 'lib/resourceful/authentication_manager.rb', line 64

def domain
  @domain
end

#passwordObject (readonly)

Returns the value of attribute password.



64
65
66
# File 'lib/resourceful/authentication_manager.rb', line 64

def password
  @password
end

#realmObject (readonly)

Returns the value of attribute realm.



64
65
66
# File 'lib/resourceful/authentication_manager.rb', line 64

def realm
  @realm
end

#usernameObject (readonly)

Returns the value of attribute username.



64
65
66
# File 'lib/resourceful/authentication_manager.rb', line 64

def username
  @username
end

Instance Method Details

#add_credentials_to(request) ⇒ Object



91
92
93
# File 'lib/resourceful/authentication_manager.rb', line 91

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

#can_handle?(request) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/resourceful/authentication_manager.rb', line 87

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

#credentials_for(request) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/resourceful/authentication_manager.rb', line 95

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



72
73
74
75
# File 'lib/resourceful/authentication_manager.rb', line 72

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)


77
78
79
80
81
82
83
84
85
# File 'lib/resourceful/authentication_manager.rb', line 77

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