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.

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.

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]

Registering a configured factory

Spree.admin_authentication_strategies.add(
  :entra,
  Spree::Authentication::Strategies::OidcStrategy.configure(
    issuer: 'https://login.microsoftonline.com/<tenant>/v2.0',
    client_id: ENV['ENTRA_CLIENT_ID'],
    client_secret: ENV['ENTRA_CLIENT_SECRET'],
    label: 'Microsoft Entra ID'
  )
)

Instance Method Summary collapse

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.

Parameters:

  • key (Symbol, String) —

    provider identifier

Returns:

  • (Class, nil) —

    the registered strategy class, or nil if no such key



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.

Parameters:

Returns:

  • (Class) —

    the registered class



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.

Parameters:

  • key (Symbol, String) —

    provider identifier

Returns:

  • (Object, nil) —

    a strategy instance, or nil if no such key



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.

Yield Parameters:

  • key (Symbol) —

    provider key, for minting a per-provider CSRF state

Returns:

  • (Array<Hash>) —

    one entry per registered provider

  • (Array<Hash>) —

    one entry per registered provider



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] = authorization_url_for(entry, state: state)
    end

    descriptor.compact
  end
end

#key?(key) ⇒ Boolean

Whether a strategy is registered under the given provider key.

Parameters:

  • key (Symbol, String)

Returns:

  • (Boolean)


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.

Parameters:

  • key (Symbol, String)

Returns:

  • (Class, nil) —

    the removed class, or nil if no such key



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.

Returns:

  • (Hash{Symbol => Class}) —

    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