class HTAuth::PasswdEntry

Object version of a single entry from a htpasswd file

Attributes

algorithm[R]

the algorithm used to create the digest of this entry

algorithm_args[R]

the algorithm arguments used to create the digest of this entry

Private Instance Methods

algorithm=(alg) click to toggle source

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
algorithm_args=(args) click to toggle source

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

↑ top

Public Instance Methods

authenticated?(check_password) click to toggle source

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

↑ top

Attributes

digest[RW]

the password digest of this entry

user[RW]

the user of this entry

Public Class Methods

from_line(line) click to toggle source

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
is_entry!(line) click to toggle source

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
is_entry?(line) click to toggle source

Returns

Returns whether or not the line is a valid entry

Returns true or false

# File lib/htauth/passwd_entry.rb, line 52
def is_entry?(line)
  begin
    is_entry!(line)
    return true
  rescue InvalidPasswdEntry
    return false
  end
end
new(user, password = nil, alg = Algorithm::DEFAULT, alg_params = {} ) click to toggle source

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

calc_digest(password) click to toggle source

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
key() click to toggle source

Returns

Returns the key of this entry

# File lib/htauth/passwd_entry.rb, line 126
def key
  return "#{user}"
end
password=(new_password) click to toggle source

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
to_s() click to toggle source

Returns

Returns the file line for this entry

# File lib/htauth/passwd_entry.rb, line 131
def to_s
  "#{user}:#{digest}"
end