Class: HTAuth::DigestEntry

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

Overview

A single record in an htdigest file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DigestEntry.



47
48
49
50
51
# File 'lib/htauth/digest_entry.rb', line 47

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

Instance Attribute Details

#digestObject

Returns the value of attribute digest.



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

def digest
  @digest
end

#realmObject

Returns the value of attribute realm.



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

def realm
  @realm
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.from_line(line) ⇒ Object



16
17
18
19
20
21
# File 'lib/htauth/digest_entry.rb', line 16

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

test if a line is an entry, raise InvalidDigestEntry if it is not. an entry must be composed of 3 parts, username:realm:md5sum where username, and realm do not contain the ‘:’ character and the md5sum must be 32 characters long.

Raises:



27
28
29
30
31
32
33
34
# File 'lib/htauth/digest_entry.rb', line 27

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

test if a line is an entry and return true or false

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/htauth/digest_entry.rb', line 37

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

Instance Method Details

#authenticated?(check_password) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/htauth/digest_entry.rb', line 61

def authenticated?(check_password)
  hd = ::Digest::MD5.hexdigest("#{user}:#{realm}:#{check_password}")
  return hd == digest
end

#calc_digest(password) ⇒ Object



57
58
59
# File 'lib/htauth/digest_entry.rb', line 57

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

#keyObject



66
67
68
# File 'lib/htauth/digest_entry.rb', line 66

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

#password=(new_password) ⇒ Object



53
54
55
# File 'lib/htauth/digest_entry.rb', line 53

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

#to_sObject



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

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