Module: Passlib::Configuration::Context
- Included in:
- Passlib, Passlib::Context
- Defined in:
- lib/passlib/configuration/context.rb
Overview
Mixin that provides configuration, hash-loading, and hash-creation helpers.
Extended into the Passlib module (so all methods are available as Passlib.load, Passlib.create, etc.)
Instance Method Summary collapse
-
#configuration ⇒ Configuration
(also: #config)
Returns the active Passlib::Configuration for this object.
-
#configuration=(value) ⇒ void
(also: #config=)
Replaces the current configuration options.
-
#configure {|config| ... } ⇒ void
(also: #setup)
Yields the current configuration to a block for modification.
-
#create(secret) ⇒ Password
Creates a new password hash for the given secret using the current configuration.
-
#load(payload) ⇒ Password
Parses a stored hash string and returns a Password instance.
-
#upgrade(secret, hash, verify: true) ⇒ Password?
Re-hashes a password if the stored hash is outdated.
-
#upgrade?(hash) ⇒ Boolean
Returns whether a stored hash should be re-hashed.
-
#verify(secret, hash) ⇒ Boolean
(also: #match?, #valid_secret?, #valid_password?)
Verifies a plaintext secret against a stored password hash.
Instance Method Details
#configuration ⇒ Configuration Also known as: config
Returns the active Passlib::Configuration for this object.
The first call initializes the configuration, inheriting from Passlib.configuration unless this object is the Passlib module.
14 |
# File 'lib/passlib/configuration/context.rb', line 14 def configuration = @configuration ||= Passlib::Configuration.new(base_config) |
#configuration=(value) ⇒ void Also known as: config=
This method returns an undefined value.
Replaces the current configuration options.
21 22 23 |
# File 'lib/passlib/configuration/context.rb', line 21 def configuration=(value) configuration.set(value) end |
#configure {|config| ... } ⇒ void Also known as: setup
This method returns an undefined value.
Yields the current configuration to a block for modification.
31 |
# File 'lib/passlib/configuration/context.rb', line 31 def configure = yield(config) |
#create(secret) ⇒ Password
Creates a new password hash for the given secret using the current configuration.
Delegates to Password.create with the current configuration applied. A preferred algorithm must be configured or a concrete subclass used.
51 |
# File 'lib/passlib/configuration/context.rb', line 51 def create(secret, **) = Passlib::Password.create(secret, config, **) |
#load(payload) ⇒ Password
Parses a stored hash string and returns a Password instance.
Delegates to Password.load with the current configuration applied.
42 |
# File 'lib/passlib/configuration/context.rb', line 42 def load(payload, **) = Passlib::Password.load(payload, config, **) |
#upgrade(secret, hash, verify: true) ⇒ Password?
Re-hashes a password if the stored hash is outdated.
First verifies secret against hash (unless verify: false). Returns nil if verification fails or no upgrade is needed. Otherwise creates and returns a new hash using the current configuration (algorithm and cost parameters).
The argument order may be swapped: if the first argument is a Password instance and the second is a String, they are treated as (hash, secret).
110 111 112 113 114 115 116 |
# File 'lib/passlib/configuration/context.rb', line 110 def upgrade(secret, hash, verify: true) secret, hash = hash, secret if secret.is_a? Passlib::Password and not hash.is_a? Passlib::Password hash = load(hash) unless hash.is_a? Passlib::Password return if verify and not verify(secret, hash) return unless upgrade?(hash) create(secret) end |
#upgrade?(hash) ⇒ Boolean
Returns whether a stored hash should be re-hashed.
Returns false immediately when no preferred scheme is configured. Returns true when the hash uses a different algorithm than the preferred scheme. When the algorithm already matches, delegates to Password#upgrade? to check whether the cost parameters are weaker than those in the current configuration.
87 88 89 90 91 92 |
# File 'lib/passlib/configuration/context.rb', line 87 def upgrade?(hash) return false unless target = config.preferred_scheme hash = load(hash) unless hash.is_a? Passlib::Password return true unless hash.is_a? Passlib[target] hash.upgrade? end |
#verify(secret, hash) ⇒ Boolean Also known as: match?, valid_secret?, valid_password?
Verifies a plaintext secret against a stored password hash.
The argument order may be swapped: if the first argument is a Password instance and the second is a String, they are treated as (hash, secret).
Equivalent to Passlib.load(hash).verify(secret).
67 68 69 70 71 |
# File 'lib/passlib/configuration/context.rb', line 67 def verify(secret, hash) secret, hash = hash, secret if secret.is_a? Passlib::Password and not hash.is_a? Passlib::Password hash = load(hash) unless hash.is_a? Passlib::Password hash.verify(secret) end |