module Sequel::Plugins::Password::ClassMethods

@!attribute [r] column

@return [Symbol] name of the column where password is stored

@!attribute [r] hashers

@return [Hash] hash of the algorithms and their corresponding Hasher

Attributes

column[R]
hashers[R]

Public Instance Methods

check_password(password, encoded, setter: nil, algorithm: :default) click to toggle source

Check if password match, and upgrade to newest hashing algorithm if needed.

@param [String] password in plain text @param [String] encoded password for comparision @param [Proc] setter accepting an encoded password @param [Symbol] algorithm to be used for hashing @return [Boolean] if password match encoded password

# File lib/sequel_password.rb, line 64
def check_password(password, encoded, setter: nil, algorithm: :default)
  return false if password.nil? || !usable_password?(encoded)

  preferred = hasher(algorithm)
  hasher = hasher(encoded.split('$').first)

  must_update = hasher.algorithm != preferred.algorithm
  must_update ||= preferred.must_update(encoded)

  correct = hasher.verify(password, encoded)
  setter.call(password) if !setter.nil? && correct && must_update

  correct
end
make_password(password, salt: nil, algorithm: :default) click to toggle source

Returns the given password hash. It will return an unusable hash if given password is nil.

@param [String, nil] password to be hashed @param [String, nil] salt to be used during hashing @param [Symbol] algorithm to be used for hashing @return [String] the given password hashed

# File lib/sequel_password.rb, line 38
def make_password(password, salt: nil, algorithm: :default)
  return "!#{SecureRandom.hex(20)}" if password.nil?

  salt = hasher(algorithm).salt if salt.nil?
  hasher(algorithm).encode(password, salt)
end
usable_password?(encoded) click to toggle source

Returns if encoded hash is a usable password.

@param [String] encoded hash @return [Boolean] if password is usable

# File lib/sequel_password.rb, line 49
def usable_password?(encoded)
  return false if encoded.nil? || encoded.start_with?('!')

  algorithm = encoded.split('$').first
  !hasher(algorithm).nil?
end

Private Instance Methods

hasher(algorithm = :default) click to toggle source
# File lib/sequel_password.rb, line 81
def hasher(algorithm = :default)
  @hashers.fetch(algorithm.to_sym, @hashers.values.first)
end