Class: HTAuth::DigestFile

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

Constant Summary collapse

ENTRY_KLASS =
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

add an new record. raises an error if the entry exists.

Raises:



40
41
42
43
44
45
46
47
48
49
# File 'lib/htauth/digest_file.rb', line 40

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

add or update an entry as appropriate



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

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

remove an entry from the file



19
20
21
22
23
24
25
26
27
28
# File 'lib/htauth/digest_file.rb', line 19

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



69
70
71
# File 'lib/htauth/digest_file.rb', line 69

def entry_klass
  ENTRY_KLASS
end

#fetch(username, realm) ⇒ Object

fetches a copy of an entry from the file. Updateing the entry returned from fetch will NOT propogate back to the file.



63
64
65
66
67
# File 'lib/htauth/digest_file.rb', line 63

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

does the entry the the specified username and realm exist in the file

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/htauth/digest_file.rb', line 13

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

#update(username, realm, password) ⇒ Object

update an already existing entry with a new password. raises an error if the entry does not exist

Raises:



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

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