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
-
.entry!(line) ⇒ Object
Internal: test if the given line is valid for this Entry class.
-
.entry?(line) ⇒ Boolean
Internal: Returns whether or not the line is a valid entry.
-
.from_line(line) ⇒ Object
Internal: Create an instance of this class from a line of text.
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
64 65 66 67 68 69 |
# File 'lib/htauth/digest_entry.rb', line 64 def initialize(user, realm, password = "") super() @user = user @realm = realm @digest = calc_digest(password) end |
Instance Attribute Details
#digest ⇒ Object
Internal: The passwod digest of this entry
16 17 18 |
# File 'lib/htauth/digest_entry.rb', line 16 def digest @digest end |
#realm ⇒ Object
Internal: The realm of this entry
14 15 16 |
# File 'lib/htauth/digest_entry.rb', line 14 def realm @realm end |
#user ⇒ Object
Internal: The user of this entry
12 13 14 |
# File 'lib/htauth/digest_entry.rb', line 12 def user @user end |
Class Method Details
.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
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/htauth/digest_entry.rb', line 41 def entry!(line) raise InvalidDigestEntry, "line commented out" if line.start_with?("#") 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" unless /\A[[:xdigit:]]{32}\Z/.match?(parts.last) parts end |
.entry?(line) ⇒ Boolean
Internal: Returns whether or not the line is a valid entry
Returns true or false
55 56 57 58 59 60 |
# File 'lib/htauth/digest_entry.rb', line 55 def entry?(line) entry!(line) true rescue InvalidDigestEntry false end |
.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
24 25 26 27 28 29 |
# File 'lib/htauth/digest_entry.rb', line 24 def from_line(line) parts = entry!(line) d = DigestEntry.new(parts[0], parts[1]) d.digest = parts[2] d end |
Instance Method Details
#authenticated?(check_password) ⇒ Boolean
Public: Check if the given password is the password of this entry.
82 83 84 85 |
# File 'lib/htauth/digest_entry.rb', line 82 def authenticated?(check_password) check = calc_digest(check_password) Algorithm.secure_compare(check, digest) end |
#calc_digest(password) ⇒ Object
Internal: calculate the new digest of the given password
77 78 79 |
# File 'lib/htauth/digest_entry.rb', line 77 def calc_digest(password) ::Digest::MD5.hexdigest("#{user}:#{realm}:#{password}") end |
#key ⇒ Object
Internal: Returns the key of this entry
88 89 90 |
# File 'lib/htauth/digest_entry.rb', line 88 def key "#{user}:#{realm}" end |
#password=(new_password) ⇒ Object
Internal: Update the password of the entry with its new value
72 73 74 |
# File 'lib/htauth/digest_entry.rb', line 72 def password=(new_password) @digest = calc_digest(new_password) end |
#to_s ⇒ Object
Internal: Returns the file line for this entry
93 94 95 |
# File 'lib/htauth/digest_entry.rb', line 93 def to_s "#{user}:#{realm}:#{digest}" end |