Class: HTAuth::PasswdEntry
Overview
A single record in an htdigest file.
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
Returns the value of attribute algorithm.
-
#digest ⇒ Object
Returns the value of attribute digest.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
- .from_line(line) ⇒ Object
-
.is_entry!(line) ⇒ Object
test if a line is an entry, raise InvalidPasswdEntry if it is not.
-
.is_entry?(line) ⇒ Boolean
test if a line is an entry and return true or false.
Instance Method Summary collapse
-
#authenticated?(check_password) ⇒ Boolean
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.
-
#initialize(user, password = nil, alg = Algorithm::DEFAULT, alg_params = {}) ⇒ PasswdEntry
constructor
A new instance of PasswdEntry.
- #key ⇒ Object
- #password=(new_password) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Entry
Constructor Details
#initialize(user, password = nil, alg = Algorithm::DEFAULT, alg_params = {}) ⇒ PasswdEntry
Returns a new instance of PasswdEntry.
45 46 47 48 49 50 |
# File 'lib/htauth/passwd_entry.rb', line 45 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 = algorithm.encode(password) if password end |
Instance Attribute Details
#algorithm ⇒ Object
Returns the value of attribute algorithm.
13 14 15 |
# File 'lib/htauth/passwd_entry.rb', line 13 def algorithm @algorithm end |
#digest ⇒ Object
Returns the value of attribute digest.
12 13 14 |
# File 'lib/htauth/passwd_entry.rb', line 12 def digest @digest end |
#user ⇒ Object
Returns the value of attribute user.
11 12 13 |
# File 'lib/htauth/passwd_entry.rb', line 11 def user @user end |
Class Method Details
.from_line(line) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/htauth/passwd_entry.rb', line 16 def from_line(line) parts = is_entry!(line) d = PasswdEntry.new(parts[0]) d.digest = parts[1] d.algorithm = Algorithm.algorithms_from_field(parts[1]) return d end |
.is_entry!(line) ⇒ Object
test if a line is an entry, raise InvalidPasswdEntry if it is not. an entry must be composed of 2 parts, username:encrypted_password where username, and password do not contain the ‘:’ character
27 28 29 30 31 32 |
# File 'lib/htauth/passwd_entry.rb', line 27 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:pssword" if parts.size != 2 return parts end |
.is_entry?(line) ⇒ Boolean
test if a line is an entry and return true or false
35 36 37 38 39 40 41 42 |
# File 'lib/htauth/passwd_entry.rb', line 35 def is_entry?(line) begin is_entry!(line) return true rescue InvalidPasswdEntry return false end end |
Instance Method Details
#authenticated?(check_password) ⇒ Boolean
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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/htauth/passwd_entry.rb', line 74 def authenticated?(check_password) authed = false if algorithm.kind_of?(Array) then algorithm.each do |alg| if alg.encode(check_password) == digest then @algorithm = alg authed = true break end end else authed = digest == algorithm.encode(check_password) end return authed end |
#key ⇒ Object
90 91 92 |
# File 'lib/htauth/passwd_entry.rb', line 90 def key return "#{user}" end |
#password=(new_password) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/htauth/passwd_entry.rb', line 65 def password=(new_password) if algorithm.kind_of?(Array) then @algorithm = Algorithm.algorithm_from_name("crypt") end @digest = algorithm.encode(new_password) end |
#to_s ⇒ Object
94 95 96 |
# File 'lib/htauth/passwd_entry.rb', line 94 def to_s "#{user}:#{digest}" end |