Class | HTAuth::DigestEntry |
In: |
lib/htauth/digest_entry.rb
|
Parent: | Object |
A single record in an htdigest file.
digest | [RW] | |
realm | [RW] | |
user | [RW] |
# File lib/htauth/digest_entry.rb, line 15 15: def from_line(line) 16: parts = is_entry!(line) 17: d = DigestEntry.new(parts[0], parts[1]) 18: d.digest = parts[2] 19: return d 20: end
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.
# File lib/htauth/digest_entry.rb, line 26 26: def is_entry!(line) 27: raise InvalidDigestEntry, "line commented out" if line =~ /\A#/ 28: parts = line.strip.split(":") 29: raise InvalidDigestEntry, "line must be of the format username:realm:md5checksum" if parts.size != 3 30: raise InvalidDigestEntry, "md5 checksum is not 32 characters long" if parts.last.size != 32 31: raise InvalidDigestEntry, "md5 checksum has invalid characters" if parts.last !~ /\A[[:xdigit:]]{32}\Z/ 32: return parts 33: end
test if a line is an entry and return true or false
# File lib/htauth/digest_entry.rb, line 36 36: def is_entry?(line) 37: begin 38: is_entry!(line) 39: return true 40: rescue InvalidDigestEntry 41: return false 42: end 43: end
# File lib/htauth/digest_entry.rb, line 46 46: def initialize(user, realm, password = "") 47: @user = user 48: @realm = realm 49: @digest = calc_digest(password) 50: end
# File lib/htauth/digest_entry.rb, line 60 60: def authenticated?(check_password) 61: hd = ::Digest::MD5.hexdigest("#{user}:#{realm}:#{check_password}") 62: return hd == digest 63: end
# File lib/htauth/digest_entry.rb, line 56 56: def calc_digest(password) 57: ::Digest::MD5.hexdigest("#{user}:#{realm}:#{password}") 58: end