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.



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

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.



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

def challenge
  @challenge
end

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#realmObject (readonly)

Returns the value of attribute realm.



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

def realm
  @realm
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#add_credentials_to(request) ⇒ Object



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

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

#can_handle?(request) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#credentials_for(request) ⇒ Object



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

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



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

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)


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

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