Class: HTAuth::PasswdEntry
Overview
Internal: Object version of a single entry from a htpasswd file
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
Internal: the algorithm used to create the digest of this entry.
-
#algorithm_args ⇒ Object
Internal: the algorithm arguments used to create the digest of this entry.
-
#digest ⇒ Object
Internal: the password digest 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, password = nil, alg = Algorithm::DEFAULT, alg_params = {}) ⇒ PasswdEntry
constructor
Internal: Create a new Entry with the given user, password, and algorithm.
-
#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.
Methods inherited from Entry
Constructor Details
#initialize(user, password = nil, alg = Algorithm::DEFAULT, alg_params = {}) ⇒ PasswdEntry
Internal: Create a new Entry with the given user, password, and algorithm
63 64 65 66 67 68 |
# File 'lib/htauth/passwd_entry.rb', line 63 def initialize(user, password = nil, alg = Algorithm::DEFAULT, alg_params = {} ) @user = user alg = Algorithm::DEFAULT if alg == Algorithm::EXISTING @algorithm = Algorithm.algorithm_from_name(alg, alg_params) @digest = calc_digest(password) end |
Instance Attribute Details
#algorithm ⇒ Object
Internal: the algorithm used to create the digest of this entry
14 15 16 |
# File 'lib/htauth/passwd_entry.rb', line 14 def algorithm @algorithm end |
#algorithm_args ⇒ Object
Internal: the algorithm arguments used to create the digest of this entry
16 17 18 |
# File 'lib/htauth/passwd_entry.rb', line 16 def algorithm_args @algorithm_args end |
#digest ⇒ Object
Internal: the password digest of this entry
12 13 14 |
# File 'lib/htauth/passwd_entry.rb', line 12 def digest @digest end |
#user ⇒ Object
Internal: the user of this entry
10 11 12 |
# File 'lib/htauth/passwd_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 htpasswd file
Returns an instance of PasswdEntry
24 25 26 27 28 29 30 |
# File 'lib/htauth/passwd_entry.rb', line 24 def from_line(line) parts = is_entry!(line) d = PasswdEntry.new(parts[0]) d.digest = parts[1] d.algorithm = Algorithm.algorithm_from_field(parts[1]) return d end |
.is_entry!(line) ⇒ Object
Internal: test if the given line is valid for this Entry class
A valid entry is a single line composed of two parts; a username and a password separated by a ‘:’ character. Neither the username nor the password may contain a ‘:’ character
line - a line of text from a file
Returns the individual parts of the line Raises InvalidPasswdEntry if it is not an valid entry
42 43 44 45 46 47 |
# File 'lib/htauth/passwd_entry.rb', line 42 def is_entry!(line) raise InvalidPasswdEntry, "line commented out" if line =~ /\A#/ parts = line.strip.split(":") raise InvalidPasswdEntry, "line must be of the format username:password" if parts.size != 2 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/passwd_entry.rb', line 52 def is_entry?(line) begin is_entry!(line) return true rescue InvalidPasswdEntry return false end end |
Instance Method Details
#authenticated?(check_password) ⇒ Boolean
Public: Check if the given password is the password of this entry
110 111 112 |
# File 'lib/htauth/passwd_entry.rb', line 110 def authenticated?(check_password) algorithm.verify_password?(check_password, @digest) end |
#calc_digest(password) ⇒ Object
Internal: calculate the new digest of the given password
103 104 105 106 |
# File 'lib/htauth/passwd_entry.rb', line 103 def calc_digest(password) return nil unless password algorithm.encode(password) end |
#key ⇒ Object
Internal: Returns the key of this entry
115 116 117 |
# File 'lib/htauth/passwd_entry.rb', line 115 def key "#{user}" end |
#password=(new_password) ⇒ Object
Internal: Update the password of the entry with its new value
If we have an array of algorithms, then we set it to CRYPT
95 96 97 98 99 100 |
# File 'lib/htauth/passwd_entry.rb', line 95 def password=(new_password) if algorithm.kind_of?(HTAuth::Plaintext) then @algorithm = Algorithm.algorithm_from_name(Algorithm::CRYPT) end @digest = calc_digest(new_password) end |
#to_s ⇒ Object
Internal: Returns the file line for this entry
120 121 122 |
# File 'lib/htauth/passwd_entry.rb', line 120 def to_s "#{user}:#{digest}" end |