Class: HTAuth::PasswdEntry

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

Overview

A single record in an htdigest 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

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

#algorithmObject

Returns the value of attribute algorithm.



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

def algorithm
  @algorithm
end

#digestObject

Returns the value of attribute digest.



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

def digest
  @digest
end

#userObject

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

Raises:



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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

#keyObject



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_sObject



94
95
96
# File 'lib/htauth/passwd_entry.rb', line 94

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