Class: HTAuth::DigestEntry
- Inherits:
-
Object
- Object
- HTAuth::DigestEntry
- Defined in:
- lib/htauth/digest_entry.rb
Overview
Internal: Object version of a single record from an htdigest file
Instance Attribute Summary collapse
-
#digest ⇒ Object
Internal: The passwod digest of this entry.
-
#realm ⇒ Object
Internal: The realm of this entry.
-
#user ⇒ Object
Internal: The user of this entry.
Class Method Summary collapse
-
.from_line(line) ⇒ Object
Internal: Create an instance of this class from a line of text.
-
.is_entry!(line) ⇒ Object
Internal: test if the given line is valid for this Entry class.
-
.is_entry?(line) ⇒ Boolean
Internal: Returns whether or not the line is a valid entry.
Instance Method Summary collapse
-
#authenticated?(check_password) ⇒ Boolean
Public: Check if the given password is the password of this entry.
-
#calc_digest(password) ⇒ Object
Internal: calculate the new digest of the given password.
-
#initialize(user, realm, password = "") ⇒ DigestEntry
constructor
Internal: Create a new Entry with the given user, realm and password.
-
#key ⇒ Object
Internal: Returns the key of this entry.
-
#password=(new_password) ⇒ Object
Internal: Update the password of the entry with its new value.
-
#to_s ⇒ Object
Internal: Returns the file line for this entry.
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
#digest ⇒ Object
Internal: The passwod digest of this entry
15 16 17 |
# File 'lib/htauth/digest_entry.rb', line 15 def digest @digest end |
#realm ⇒ Object
Internal: The realm of this entry
13 14 15 |
# File 'lib/htauth/digest_entry.rb', line 13 def realm @realm end |
#user ⇒ Object
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
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
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.
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 |
#key ⇒ Object
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_s ⇒ Object
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 |