Class: Spree::Authentication::StrategyRegistry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
app/models/spree/authentication/strategy_registry.rb

Overview

Keyed registry of authentication strategy classes for the Store and Admin APIs.

Strategies are dispatched by the ‘provider` value the client sends to the auth endpoint, so the registry is a key → class map. The `:email` key is reserved for the built-in Spree::Authentication::Strategies::EmailPasswordStrategy; integrators can override it by adding a different class under the same key.

Examples:

Registering a custom provider

Spree.store_authentication_strategies.add(:auth0, MyApp::Auth::Auth0Strategy)

Removing a provider

Spree.store_authentication_strategies.remove(:email)

Reading a strategy class

Spree.store_authentication_strategies[:email]

Instance Method Summary collapse

Constructor Details

#initialize(strategies = {}) ⇒ StrategyRegistry

Returns a new instance of StrategyRegistry.



26
27
28
29
# File 'app/models/spree/authentication/strategy_registry.rb', line 26

def initialize(strategies = {})
  @strategies = {}
  strategies.each { |key, klass| add(key, klass) }
end

Instance Method Details

#[](key) ⇒ Class?

Look up a registered strategy class.

Parameters:

  • key (Symbol, String)

    provider identifier

Returns:

  • (Class, nil)

    the registered strategy class, or nil if no such key



54
55
56
# File 'app/models/spree/authentication/strategy_registry.rb', line 54

def [](key)
  @strategies[key.to_sym]
end

#add(key, strategy_class) ⇒ Class

Register a strategy class under the given provider key. Overwrites any existing entry for that key.

Parameters:

Returns:

  • (Class)

    the registered class



38
39
40
# File 'app/models/spree/authentication/strategy_registry.rb', line 38

def add(key, strategy_class)
  @strategies[key.to_sym] = strategy_class
end

#key?(key) ⇒ Boolean

Whether a strategy is registered under the given provider key.

Parameters:

  • key (Symbol, String)

Returns:

  • (Boolean)


62
63
64
# File 'app/models/spree/authentication/strategy_registry.rb', line 62

def key?(key)
  @strategies.key?(key.to_sym)
end

#remove(key) ⇒ Class?

Unregister a strategy. Idempotent — returns ‘nil` if the key is not present.

Parameters:

  • key (Symbol, String)

Returns:

  • (Class, nil)

    the removed class, or nil if no such key



46
47
48
# File 'app/models/spree/authentication/strategy_registry.rb', line 46

def remove(key)
  @strategies.delete(key.to_sym)
end

#to_hHash{Symbol => Class}

Returns a shallow copy of the underlying map.

Returns:

  • (Hash{Symbol => Class})

    a shallow copy of the underlying map



67
68
69
# File 'app/models/spree/authentication/strategy_registry.rb', line 67

def to_h
  @strategies.dup
end