class HTAuth::DigestFile
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
Constants
- ENTRY_KLASS
Object version of a single record from an htdigest file
Private Instance Methods
# File lib/htauth/digest_file.rb, line 161 def internal_record(username, realm) e = DigestEntry.new(username, realm) @entries[e.key] end
Public
↑ topPublic Instance Methods
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¶ ↑
Returns nothing.
Raises DigestFileError
if the give username / realm already exists.
# 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 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¶ ↑
Returns nothing.
# 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
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¶ ↑
Returns nothing
# 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
Returns¶ ↑
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
# 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
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¶ ↑
Returns true or false if the username/realm combination is found.
# 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
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¶ ↑
Returns nothing
Raises DigestfileError if the username / realm is not found in the file
# 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