Class: Otto::Security::Authentication::RouteAuthWrapperComponents::StrategyResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/otto/security/authentication/route_auth_wrapper/strategy_resolver.rb

Overview

Resolves authentication strategy names to strategy instances

Handles strategy lookup with caching and pattern matching:

  • Exact match: ‘authenticated’ → looks up auth_config[‘authenticated’]

  • Prefix match: ‘custom:value’ → looks up ‘custom’ strategy

Results are cached to avoid repeated lookups for the same requirement.

Examples:

resolver = StrategyResolver.new(auth_config)
strategy, name = resolver.resolve('session')
strategy, name = resolver.resolve('oauth:google')  # prefix match

Instance Method Summary collapse

Constructor Details

#initialize(auth_config) ⇒ StrategyResolver

Returns a new instance of StrategyResolver.

Parameters:

  • auth_config (Hash)

    Auth configuration with :auth_strategies key



22
23
24
25
# File 'lib/otto/security/authentication/route_auth_wrapper/strategy_resolver.rb', line 22

def initialize(auth_config)
  @auth_config = auth_config
  @cache = {}
end

Instance Method Details

#clear_cacheObject

Clear the strategy cache



43
44
45
# File 'lib/otto/security/authentication/route_auth_wrapper/strategy_resolver.rb', line 43

def clear_cache
  @cache.clear
end

#resolve(requirement) ⇒ Array<AuthStrategy, String>, Array<nil, nil>

Resolve a requirement string to a strategy instance

Parameters:

  • requirement (String)

    Auth requirement from route (e.g., ‘session’, ‘oauth:google’)

Returns:

  • (Array<AuthStrategy, String>, Array<nil, nil>)

    Tuple of [strategy, name] or [nil, nil]



31
32
33
34
35
36
37
38
39
40
# File 'lib/otto/security/authentication/route_auth_wrapper/strategy_resolver.rb', line 31

def resolve(requirement)
  return [nil, nil] unless @auth_config && @auth_config[:auth_strategies]

  # Check cache first
  return @cache[requirement] if @cache.key?(requirement)

  result = find_strategy(requirement)
  @cache[requirement] = result
  result
end