Class: MultiPassword

Inherits:
Object
  • Object
show all
Extended by:
Dry::Configurable
Defined in:
lib/multi_password.rb,
lib/multi_password/errors.rb,
lib/multi_password/version.rb,
lib/multi_password/strategy.rb,
lib/multi_password/strategies/argon2.rb,
lib/multi_password/strategies/bcrypt.rb,
lib/multi_password/strategies/scrypt.rb

Defined Under Namespace

Modules: Strategies, Strategy Classes: AlgorithmNotRegistered, Error, InvalidOptions, MethodNotImplemented

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm: config.default_algorithm, options: config.default_options) ⇒ MultiPassword

Returns a new instance of MultiPassword.



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

def initialize(algorithm: config.default_algorithm, options: config.default_options)
  @strategy = registers.fetch(algorithm).new
  @options = @strategy.validate_options(options)
rescue KeyError
  raise AlgorithmNotRegistered.new(algorithm)
end

Class Method Details

.configure(&block) ⇒ Object



34
35
36
37
38
# File 'lib/multi_password.rb', line 34

def self.configure(&block)
  super.tap do
    new(algorithm: config.default_algorithm, options: config.default_options)
  end
end

.register(algorithm, klass) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/multi_password.rb', line 19

def self.register(algorithm, klass)
  if registers[algorithm]
    Warning.warn <<-MSG
[MultiPassword] #{algorithm} is already registered by #{registers[algorithm]} but is overwritten by #{klass} in:
    #{caller.first}
    MSG
  end

  registers[algorithm] = klass
end

.registersObject



15
16
17
# File 'lib/multi_password.rb', line 15

def self.registers
  @registers
end

.unregister(algorithm) ⇒ Object



30
31
32
# File 'lib/multi_password.rb', line 30

def self.unregister(algorithm)
  registers.delete(algorithm)
end

Instance Method Details

#create(password) ⇒ Object



47
48
49
# File 'lib/multi_password.rb', line 47

def create(password)
  strategy.create(password, options)
end

#verify(password, encrypted_password) ⇒ Object



51
52
53
# File 'lib/multi_password.rb', line 51

def verify(password, encrypted_password)
  strategy.verify(password, encrypted_password)
end