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
62 63 64 65 66 |
# File 'lib/htauth/digest_entry.rb', line 62 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
14 15 16 |
# File 'lib/htauth/digest_entry.rb', line 14 def digest @digest end |
#realm ⇒ Object
Internal: The realm of this entry
12 13 14 |
# File 'lib/htauth/digest_entry.rb', line 12 def realm @realm end |
#user ⇒ Object
Internal: The user of this entry
10 11 12 |
# File 'lib/htauth/digest_entry.rb', line 10 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
22 23 24 25 26 27 |
# 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) ⇒ 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
39 40 41 42 43 44 45 46 |
# 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) ⇒ Boolean
Internal: Returns whether or not the line is a valid entry
Returns true or false
51 52 53 54 55 56 57 58 |
# File 'lib/htauth/digest_entry.rb', line 51 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.
79 80 81 82 |
# File 'lib/htauth/digest_entry.rb', line 79 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
74 75 76 |
# File 'lib/htauth/digest_entry.rb', line 74 def calc_digest(password) ::Digest::MD5.hexdigest("#{user}:#{realm}:#{password}") end |
#key ⇒ Object
Internal: Returns the key of this entry
85 86 87 |
# File 'lib/htauth/digest_entry.rb', line 85 def key "#{user}:#{realm}" end |
#password=(new_password) ⇒ Object
Internal: Update the password of the entry with its new value
69 70 71 |
# File 'lib/htauth/digest_entry.rb', line 69 def password=(new_password) @digest = calc_digest(new_password) end |
#to_s ⇒ Object
Internal: Returns the file line for this entry
90 91 92 |
# File 'lib/htauth/digest_entry.rb', line 90 def to_s "#{user}:#{realm}:#{digest}" end |