Class: ROM::Auth::PasswordVerifiers::PasswordVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/auth/password_verifiers/password_verifier.rb

Direct Known Subclasses

PBKDF2Verifier

Constant Summary collapse

DEFAULT_OPTIONS =
{ :iterations => 15000, :hash_function => :sha256 }.freeze
DEFAULT_SALT_LENGTH =
16
SEPARATOR =
","
VERIFIERS =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#digestObject (readonly)

Returns the value of attribute digest.



11
12
13
# File 'lib/rom/auth/password_verifiers/password_verifier.rb', line 11

def digest
  @digest
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/rom/auth/password_verifiers/password_verifier.rb', line 11

def options
  @options
end

#saltObject (readonly)

Returns the value of attribute salt.



11
12
13
# File 'lib/rom/auth/password_verifiers/password_verifier.rb', line 11

def salt
  @salt
end

Class Method Details

.for_password(plaintext_password, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
# File 'lib/rom/auth/password_verifiers/password_verifier.rb', line 31

def self.for_password(plaintext_password, options={})
  raise ArgumentError unless plaintext_password.is_a?(String)
  new(options.merge(:password => plaintext_password))
end

.from_s(string) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
# File 'lib/rom/auth/password_verifiers/password_verifier.rb', line 36

def self.from_s(string)
  kind, salt, digest, iterations = string.split(SEPARATOR)

  raise(ArgumentError, "Invalid password verifier kind: #{kind.inspect}") unless VERIFIERS.keys.map(&:to_s).include?(kind)

  VERIFIERS[kind.to_sym].new(DEFAULT_OPTIONS.merge(:digest => digest, :salt => salt, :iterations => iterations.to_i))
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rom/auth/password_verifiers/password_verifier.rb', line 23

def ==(other)
  case other
    when PasswordVerifier then to_s == other.to_s
    when String then to_s == other
    else false
  end
end

#to_sObject



19
20
21
# File 'lib/rom/auth/password_verifiers/password_verifier.rb', line 19

def to_s
  [type, salt, digest.to_s, options[:iterations]].join(SEPARATOR)
end

#verifies?(plaintext_password) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/rom/auth/password_verifiers/password_verifier.rb', line 13

def verifies?(plaintext_password)
  tested_digest = compute_digest(plaintext_password, salt, options)

  digest == tested_digest
end