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

- (DigestEntry) initialize(user, realm, password = "")

A new instance of DigestEntry



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

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

Instance Attribute Details

- (Object) digest

Returns the value of attribute digest



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

def digest
  @digest
end

- (Object) realm

Returns the value of attribute realm



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

def realm
  @realm
end

- (Object) user

Returns the value of attribute user



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

def user
  @user
end

Class Method Details

+ (Object) from_line(line)



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

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

+ (Object) is_entry!(line)

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:



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

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

+ (Boolean) is_entry?(line)

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

Returns:

  • (Boolean)


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

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

Instance Method Details

- (Boolean) authenticated?(check_password)

Returns:

  • (Boolean)


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

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

- (Object) calc_digest(password)



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

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

- (Object) key



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

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

- (Object) password=(new_password)



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

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

- (Object) to_s



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

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