class HTAuth::PasswdEntry
Object version of a single entry from a htpasswd file
Attributes
the algorithm used to create the digest of this entry
the algorithm arguments used to create the digest of this entry
Private Instance Methods
set the algorithm for the entry
# File lib/htauth/passwd_entry.rb, line 71 def algorithm=(alg) return @algorithm if Algorithm::EXISTING == alg case alg when String @algorithm = Algorithm.algorithm_from_name(alg) when ::HTAuth::Algorithm @algorithm = alg else raise InvalidAlgorithmError, "Unable to assign #{alg} to algorithm" end return @algorithm end
set fields on the algorithm
# File lib/htauth/passwd_entry.rb, line 85 def algorithm_args=(args) args.each do |key, value| method = "#{key}=" @algorithm.send(method, value) if @algorithm.respond_to?(method) end end
Public
↑ topPublic Instance Methods
Check if the given password is the password of this entry check the password and make sure it works, in the case that the algorithm is unknown it tries all of the ones that it thinks it could be, and marks the algorithm if it matches when looking for a matche, we always compare all of them, no short circuiting
# File lib/htauth/passwd_entry.rb, line 113 def authenticated?(check_password) authed = false if algorithm.kind_of?(Bcrypt) then bc = ::BCrypt::Password.new(digest) authed = bc.is_password?(check_password) else encoded = algorithm.encode(check_password) authed = Algorithm.secure_compare(encoded, digest) end return authed end
Internal
↑ topAttributes
the password digest of this entry
the user of this entry
Public Class Methods
Create an instance of this class from a line of text
- line
-
a line of text from a htpasswd file
Returns¶ ↑
Returns an instance of PasswdEntry
# 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
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¶ ↑
Returns the individual parts of the line
Raises InvalidPasswdEntry
if it is not an valid entry
# 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
Create a new Entry
with the given user, password, and algorithm
# 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
Public Instance Methods
calculate the new digest of the given password
# File lib/htauth/passwd_entry.rb, line 103 def calc_digest(password) return nil unless password algorithm.encode(password) end
Update the password of the entry with its new value
If we have an array of algorithms, then we set it to CRYPT
# 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