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:



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

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!
  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.



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

def add_or_update(username, realm, password)
  if has_entry?(username, realm)
    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



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

def delete(username, realm)
  if has_entry?(username, realm)
    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



162
163
164
# File 'lib/htauth/digest_file.rb', line 162

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



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

def fetch(username, realm)
  return nil unless has_entry?(username, realm)

  ir = internal_record(username, realm)
  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)


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

def has_entry?(username, realm)
  test_entry = DigestEntry.new(username, realm)
  @entries.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:



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/htauth/digest_file.rb', line 126

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!
  nil
end