Class: Passlib::Context

Inherits:
Object
  • Object
show all
Includes:
Passlib::Configuration::Context
Defined in:
lib/passlib/context.rb

Overview

An isolated configuration context for creating and verifying password hashes.

A Context bundles a Configuration with the full Passlib::Configuration::Context interface (load, create, verify, etc.), letting different parts of an application use different algorithms or settings independently without touching the global Passlib configuration.

By default a new context inherits the global Passlib configuration as its parent, so any option not explicitly overridden falls back to the global setting. Pass another context or configuration as the first argument to inherit from that instead.

Examples:

Creating a context with a fixed algorithm

context = Passlib::Context.new(preferred_scheme: :bcrypt)
hash = context.create("hunter2")
hash.class # => Passlib::BCrypt
context.verify("hunter2", hash) # => true

Inheriting from a parent context

parent  = Passlib::Context.new(preferred_scheme: :bcrypt)
context = Passlib::Context.new(parent)
context.create("hunter2").class  # => Passlib::BCrypt

Reconfiguring after creation

context = Passlib::Context.new
context.configure { |c| c.preferred_scheme = :bcrypt }
context.create("hunter2").class  # => Passlib::BCrypt

Using as a drop-in for a subset of Passlib’s interface

context = Passlib::Context.new(preferred_scheme: :bcrypt)
context.load("$2a$12$...").verify("hunter2")  # => true

See Also:

Instance Method Summary collapse

Methods included from Passlib::Configuration::Context

#configuration, #configuration=, #configure, #create, #load, #upgrade, #upgrade?, #verify

Constructor Details

#initializeContext #initialize(parent) ⇒ Context #initialize(**options) ⇒ Context #initialize(parent, **options) ⇒ Context

Creates a new context.

Overloads:

  • #initializeContext

    Creates a context inheriting defaults from Passlib.configuration.

  • #initialize(parent) ⇒ Context

    Creates a context inheriting defaults from another context or configuration.

    Parameters:

  • #initialize(**options) ⇒ Context

    Creates a context with the given options applied on top of the global defaults.

    Parameters:

    • options (Hash)

      option key/value pairs (e.g. preferred_scheme: :bcrypt)

  • #initialize(parent, **options) ⇒ Context

    Creates a context inheriting from a parent with additional option overrides.

    Parameters:

    • parent (Context, Configuration)

      parent to inherit defaults from

    • options (Hash)

      option overrides applied on top of the parent



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/passlib/context.rb', line 57

def initialize(input = nil, **options)
  @base_config = nil

  case input
  when Passlib::Configuration::Context then @base_config   = input.config
  when Passlib::Configuration          then @base_config   = input
  when Hash                            then @configuration = Passlib::Configuration.new(base_config, input)
  when nil
  else raise ArgumentError, "invalid context input: #{input.inspect}"
  end

  config.apply(options)
end