Module: Sequel::Plugins::Password::ClassMethods

Defined in:
lib/sequel_password.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



24
25
26
# File 'lib/sequel_password.rb', line 24

def column
  @column
end

#hashersObject (readonly)

Returns the value of attribute hashers.



24
25
26
# File 'lib/sequel_password.rb', line 24

def hashers
  @hashers
end

Instance Method Details

#check_password(password, encoded, setter: nil, algorithm: :default) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sequel_password.rb', line 47

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) unless must_update

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

  correct
end

#hasher(algorithm = :default) ⇒ Object



36
37
38
# File 'lib/sequel_password.rb', line 36

def hasher(algorithm = :default)
  @hashers.fetch(algorithm.to_sym, @hashers.values.first)
end

#make_password(password, salt: nil, algorithm: :default) ⇒ Object



29
30
31
32
33
34
# File 'lib/sequel_password.rb', line 29

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) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/sequel_password.rb', line 40

def usable_password?(encoded)
  return false if encoded.nil? || encoded.start_with?("!")

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