Class: HTAuth::PasswdEntry

Inherits:
Entry
  • Object
show all
Defined in:
lib/htauth/passwd_entry.rb

Overview

Internal: Object version of a single entry from a htpasswd file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entry

#dup

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

#algorithmObject

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_argsObject

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

#digestObject

Internal: the password digest of this entry



12
13
14
# File 'lib/htauth/passwd_entry.rb', line 12

def digest
  @digest
end

#userObject

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

Raises:



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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

#keyObject

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_sObject

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