Class: Spree::Authentication::StrategyRegistry
- Inherits:
-
Object
- Object
- Spree::Authentication::StrategyRegistry
- 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.
An entry may be either a strategy class or a configured factory —
any object responding to #build(params:, request_env:, user_class:).
Factories let one class serve several registrations with different settings
(an OIDC strategy pointed at two different issuers, say), which a bare class
cannot express.
Instance Method Summary collapse
-
#[](key) ⇒ Class?
Look up a registered strategy class.
-
#add(key, strategy_class) ⇒ Class
Register a strategy class under the given provider key.
-
#build(key, params:, request_env:, user_class: nil) ⇒ Object?
Instantiates the strategy registered under
key, accepting either a bare class or a configured factory. -
#describe {|key| ... } ⇒ Array<Hash>
Self-description of every registered strategy, for the provider-discovery endpoint that drives the dashboard login page.
-
#initialize(strategies = {}) ⇒ StrategyRegistry
constructor
A new instance of StrategyRegistry.
-
#key?(key) ⇒ Boolean
Whether a strategy is registered under the given provider key.
-
#remove(key) ⇒ Class?
Unregister a strategy.
-
#to_h ⇒ Hash{Symbol => Class}
A shallow copy of the underlying map.
Constructor Details
#initialize(strategies = {}) ⇒ StrategyRegistry
Returns a new instance of StrategyRegistry.
43 44 45 46 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 43 def initialize(strategies = {}) @strategies = {} strategies.each { |key, klass| add(key, klass) } end |
Instance Method Details
#[](key) ⇒ Class?
Look up a registered strategy class.
71 72 73 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 71 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.
55 56 57 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 55 def add(key, strategy_class) @strategies[key.to_sym] = strategy_class end |
#build(key, params:, request_env:, user_class: nil) ⇒ Object?
Instantiates the strategy registered under key, accepting either a bare
class or a configured factory.
93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 93 def build(key, params:, request_env:, user_class: nil) entry = self[key] return nil if entry.nil? if entry.respond_to?(:build) entry.build(params: params, request_env: request_env, user_class: user_class) else entry.new(params: params, request_env: request_env, user_class: user_class) end end |
#describe {|key| ... } ⇒ Array<Hash>
Self-description of every registered strategy, for the provider-discovery endpoint that drives the dashboard login page.
Exposes only what a login screen needs — never client secrets, never strategy class names.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 113 def describe @strategies.map do |key, entry| kind = kind_of(entry) descriptor = { key: key.to_s, kind: kind.to_s } descriptor[:label] = label_of(entry) if kind == :redirect state = block_given? ? yield(key) : nil descriptor[:authorization_url] = (entry, state: state) end descriptor.compact end end |
#key?(key) ⇒ Boolean
Whether a strategy is registered under the given provider key.
79 80 81 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 79 def key?(key) @strategies.key?(key.to_sym) end |
#remove(key) ⇒ Class?
Unregister a strategy. Idempotent — returns nil if the key is not present.
63 64 65 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 63 def remove(key) @strategies.delete(key.to_sym) end |
#to_h ⇒ Hash{Symbol => Class}
Returns a shallow copy of the underlying map.
84 85 86 |
# File 'app/models/spree/authentication/strategy_registry.rb', line 84 def to_h @strategies.dup end |