class HTAuth::DigestEntry

Object version of a single record from an htdigest file

Public

↑ top

Public Instance Methods

authenticated?(check_password) click to toggle source

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

# File lib/htauth/digest_entry.rb, line 79
def authenticated?(check_password)
  check = calc_digest(check_password)
  return Algorithm.secure_compare(check, digest)
end

Internal

↑ top

Attributes

digest[RW]

The passwod digest of this entry

realm[RW]

The realm of this entry

user[RW]

The user of this entry

Public Class Methods

from_line(line) click to toggle source

Create an instance of this class from a line of text

line

a line of text from a htdigest file

Returns

Returns an instance of DigestEntry

# File lib/htauth/digest_entry.rb, line 22
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) click to toggle source

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

Returns the individual parts of the line

Raises InvalidDigestEntry if it is not a a valid entry

# File lib/htauth/digest_entry.rb, line 39
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) click to toggle source

Returns

Returns whether or not the line is a valid entry

Returns true or false

# File lib/htauth/digest_entry.rb, line 51
def is_entry?(line)
  begin
    is_entry!(line)
    return true
  rescue InvalidDigestEntry
    return false
  end
end
new(user, realm, password = "") click to toggle source

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

# File lib/htauth/digest_entry.rb, line 62
def initialize(user, realm, password = "")
  @user     = user
  @realm    = realm
  @digest   = calc_digest(password)
end

Public Instance Methods

calc_digest(password) click to toggle source

calculate the new digest of the given password

# File lib/htauth/digest_entry.rb, line 74
def calc_digest(password)
  ::Digest::MD5.hexdigest("#{user}:#{realm}:#{password}")
end
key() click to toggle source

Returns

Returns the key of this entry

# File lib/htauth/digest_entry.rb, line 85
def key
  "#{user}:#{realm}"
end
password=(new_password) click to toggle source

Update the password of the entry with its new value

# File lib/htauth/digest_entry.rb, line 69
def password=(new_password)
  @digest = calc_digest(new_password)
end
to_s() click to toggle source

Returns

Returns the file line for this entry

# File lib/htauth/digest_entry.rb, line 90
def to_s
  "#{user}:#{realm}:#{digest}"
end