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



64
65
66
67
68
69
70
# File 'lib/htauth/passwd_entry.rb', line 64

def initialize(user, password = nil, alg = Algorithm::DEFAULT, alg_params = {})
  super()
  @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



15
16
17
# File 'lib/htauth/passwd_entry.rb', line 15

def algorithm
  @algorithm
end

#algorithm_argsObject

Internal: the algorithm arguments used to create the digest of this entry



17
18
19
# File 'lib/htauth/passwd_entry.rb', line 17

def algorithm_args
  @algorithm_args
end

#digestObject

Internal: the password digest of this entry



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

def digest
  @digest
end

#userObject

Internal: the user of this entry



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

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 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:



43
44
45
46
47
48
49
50
# File 'lib/htauth/passwd_entry.rb', line 43

def entry!(line)
  raise InvalidPasswdEntry, "line commented out" if line.start_with?("#")

  parts = line.strip.split(":")
  raise InvalidPasswdEntry, "line must be of the format username:password" if parts.size != 2

  parts
end

.entry?(line) ⇒ Boolean

Internal: Returns whether or not the line is a valid entry

Returns true or false

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/htauth/passwd_entry.rb', line 55

def entry?(line)
  entry!(line)
  true
rescue InvalidPasswdEntry
  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 htpasswd file

Returns an instance of PasswdEntry



25
26
27
28
29
30
31
# File 'lib/htauth/passwd_entry.rb', line 25

def from_line(line)
  parts = entry!(line)
  d = PasswdEntry.new(parts[0])
  d.digest = parts[1]
  d.algorithm = Algorithm.algorithm_from_field(parts[1])
  d
end

Instance Method Details

#authenticated?(check_password) ⇒ Boolean

Public: Check if the given password is the password of this entry

Returns:

  • (Boolean)


112
113
114
# File 'lib/htauth/passwd_entry.rb', line 112

def authenticated?(check_password)
  algorithm.verify_password?(check_password, @digest)
end

#calc_digest(password) ⇒ Object

Internal: calculate the new digest of the given password



104
105
106
107
108
# File 'lib/htauth/passwd_entry.rb', line 104

def calc_digest(password)
  return nil unless password

  algorithm.encode(password)
end

#keyObject

Internal: Returns the key of this entry



117
118
119
# File 'lib/htauth/passwd_entry.rb', line 117

def key
  user.to_s
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



98
99
100
101
# File 'lib/htauth/passwd_entry.rb', line 98

def password=(new_password)
  @algorithm = Algorithm.algorithm_from_name(Algorithm::CRYPT) if algorithm.is_a?(HTAuth::Plaintext)
  @digest = calc_digest(new_password)
end

#to_sObject

Internal: Returns the file line for this entry



122
123
124
# File 'lib/htauth/passwd_entry.rb', line 122

def to_s
  "#{user}:#{digest}"
end