Class: HTAuth::DigestEntry

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

Overview

Internal: Object version of a single record from an htdigest file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, realm, password = "") ⇒ DigestEntry

Internal: Create a new Entry with the given user, realm and password



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

def initialize(user, realm, password = "")
  @user     = user
  @realm    = realm
  @digest   = calc_digest(password)
end

Instance Attribute Details

#digestObject

Internal: The passwod digest of this entry



15
16
17
# File 'lib/htauth/digest_entry.rb', line 15

def digest
  @digest
end

#realmObject

Internal: The realm of this entry



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

def realm
  @realm
end

#userObject

Internal: The user of this entry



11
12
13
# File 'lib/htauth/digest_entry.rb', line 11

def user
  @user
end

Class Method Details

.from_line(line) ⇒ Object

Internal: Create an instance of this class from a line of text

line - a line of text from a htdigest file

Returns an instance of DigestEntry



23
24
25
26
27
28
# File 'lib/htauth/digest_entry.rb', line 23

def from_line(line)
  parts = is_entry!(line)
  d = DigestEntry.new(parts[0], parts[1])
  d.digest = parts[2]
  return d
end

.is_entry!(line) ⇒ Object

Internal: test if the given line is valid for this Entry class

A valid entry must be composed of 3 parts, username:realm:md5sum where username, and realm do not contain the ‘:’ character; and md5sum must be 32 characters long

line - a line of text from a file

Returns the individual parts of the line Raises InvalidDigestEntry if it is not a a valid entry

Raises:



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

def is_entry!(line)
  raise InvalidDigestEntry, "line commented out" if line =~ /\A#/
  parts = line.strip.split(":")
  raise InvalidDigestEntry, "line must be of the format username:realm:md5checksum" if parts.size != 3
  raise InvalidDigestEntry, "md5 checksum is not 32 characters long" if parts.last.size  != 32
  raise InvalidDigestEntry, "md5 checksum has invalid characters" if parts.last !~ /\A[[:xdigit:]]{32}\Z/
  return parts
end

.is_entry?(line) ⇒ Boolean

Internal: Returns whether or not the line is a valid entry

Returns true or false

Returns:

  • (Boolean)


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

def is_entry?(line)
  begin
    is_entry!(line)
    return true
  rescue InvalidDigestEntry
    return false
  end
end

Instance Method Details

#authenticated?(check_password) ⇒ Boolean

Public: Check if the given password is the password of this entry.

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/htauth/digest_entry.rb', line 80

def authenticated?(check_password)
  check = calc_digest(check_password)
  return Algorithm.secure_compare(check, digest)
end

#calc_digest(password) ⇒ Object

Internal: calculate the new digest of the given password



75
76
77
# File 'lib/htauth/digest_entry.rb', line 75

def calc_digest(password)
  ::Digest::MD5.hexdigest("#{user}:#{realm}:#{password}")
end

#keyObject

Internal: Returns the key of this entry



86
87
88
# File 'lib/htauth/digest_entry.rb', line 86

def key
  "#{user}:#{realm}"
end

#password=(new_password) ⇒ Object

Internal: Update the password of the entry with its new value



70
71
72
# File 'lib/htauth/digest_entry.rb', line 70

def password=(new_password)
  @digest = calc_digest(new_password)
end

#to_sObject

Internal: Returns the file line for this entry



91
92
93
# File 'lib/htauth/digest_entry.rb', line 91

def to_s
  "#{user}:#{realm}:#{digest}"
end