Class: HTAuth::DigestFile

Inherits:
File
  • Object
show all
Defined in:
lib/htauth/digest_file.rb

Overview

Public: An API for managing an ‘htdigest’ file

Examples

::HTAuth::DigestFile.open("my.digest") do |df|
  df.has_entry?('myuser', 'myrealm')
  df.add_or_update('someuser', 'myrealm', 'a password')
  df.delete('someolduser', 'myotherrealm')
end

Constant Summary collapse

ENTRY_KLASS =

Private: The class implementing a single entry in the DigestFile

HTAuth::DigestEntry

Constants inherited from File

File::ALTER, File::CREATE, File::STDOUT_FLAG

Instance Attribute Summary

Attributes inherited from File

#file, #filename

Instance Method Summary collapse

Methods inherited from File

#contents, #dirty!, #dirty?, #initialize, #load_entries, open, #save!

Constructor Details

This class inherits a constructor from HTAuth::File

Instance Method Details

#add(username, realm, password) ⇒ Object

Public: Add a new record to the file.

username - the username of the entry realm - the realm of the entry password - the password of the entry

Examples

digest_file.add("newuser", "realm", "password")
digest_file.save!

Returns nothing. Raises DigestFileError if the give username / realm already exists.

Raises:



99
100
101
102
103
104
105
106
107
108
# File 'lib/htauth/digest_file.rb', line 99

def add(username, realm, password)
  raise DigestFileError, "Unable to add already existing user #{username} in realm #{realm}" if has_entry?(username, realm)

  new_entry = DigestEntry.new(username, realm, password)
  new_index = @lines.size
  @lines << new_entry.to_s
  @entries[new_entry.key] = { 'entry' => new_entry, 'line_index' => new_index }
  dirty!
  return nil
end

#add_or_update(username, realm, password) ⇒ Object

Public: Add or update username / realm entry with the new password. This will add a new entry if the username / realm combination does not exist in the file. If the entry does exist in the file, then the password of the entry is updated to the new password.

The file is not written to disk until #save! is called.

username - the username of the entry realm - the realm of the entry password - the password of the entry

Examples

digest_file.add_or_update("newuser", "realm", "password")
digest_file.save!

Returns nothing.



78
79
80
81
82
83
84
# File 'lib/htauth/digest_file.rb', line 78

def add_or_update(username, realm, password)
  if has_entry?(username, realm) then
    update(username, realm, password)
  else
    add(username, realm, password)
  end
end

#delete(username, realm) ⇒ Object

Public: remove the given username / realm from the file. The file is not written to disk until #save! is called.

username - the username to remove realm - the realm to remove

Examples

digest_file.delete("myuser", "myrealm")
digest_file.save!

Returns nothing



50
51
52
53
54
55
56
57
58
59
# File 'lib/htauth/digest_file.rb', line 50

def delete(username, realm)
  if has_entry?(username, realm) then
    ir = internal_record(username, realm)
    line_index = ir['line_index']
    @entries.delete(ir['entry'].key)
    @lines[line_index] = nil
    dirty!
  end
  nil
end

#entry_klassObject

Internal: returns the class used for each entry

Returns a Class



155
156
157
# File 'lib/htauth/digest_file.rb', line 155

def entry_klass
  ENTRY_KLASS
end

#fetch(username, realm) ⇒ Object

Public: Returns the given DigestEntry from the file.

Updating the DigestEntry instance returned by this method will NOT update the file. To update the file, use #update and #save!

username - the username of the entry realm - the realm of the entry

Examples

entry = digest_file.fetch("myuser", "myrealm")

Returns a DigestEntry if the entry is found Returns nil if the entry is not found



146
147
148
149
150
# File 'lib/htauth/digest_file.rb', line 146

def fetch(username, realm)
  return nil unless has_entry?(username, realm)
  ir = internal_record(username, realm)
  return ir['entry'].dup
end

#has_entry?(username, realm) ⇒ Boolean

Public: Checks if the given username / realm combination exists

username - the username to check realm - the realm to check

Examples

digest_file.has_entry?("myuser", "myrealm")
# => true

Returns true or false if the username/realm combination is found.

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/htauth/digest_file.rb', line 33

def has_entry?(username, realm)
  test_entry = DigestEntry.new(username, realm)
  @entries.has_key?(test_entry.key)
end

#update(username, realm, password) ⇒ Object

Public: Updates an existing username / relam entry with a new password

username - the username of the entry realm - the realm of the entry password - the password of the entry

Examples

digest_file.update("existinguser", "realm", "newpassword")
digest_file.save!

Returns nothing Raises DigestfileError if the username / realm is not found in the file

Raises:



123
124
125
126
127
128
129
130
# File 'lib/htauth/digest_file.rb', line 123

def update(username, realm, password)
  raise DigestFileError, "Unable to update non-existent user #{username} in realm #{realm}" unless has_entry?(username, realm)
  ir = internal_record(username, realm)
  ir['entry'].password = password
  @lines[ir['line_index']] = ir['entry'].to_s
  dirty!
  return nil
end