Class: Otto::Security::Authentication::RouteAuthWrapper

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

Overview

Wraps route handlers with authentication and authorization

This is the main orchestrator that:

  • Sets anonymous StrategyResult for unauthenticated routes

  • Enforces authentication for protected routes

  • Supports multi-strategy with OR logic (first success wins)

  • Performs Layer 1 (route-level) role authorization

Examples:

Basic usage

wrapper = RouteAuthWrapper.new(handler, route_def, auth_config)
response = wrapper.call(env)

See Also:

  • for strategy lookup
  • for error responses
  • for role checking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrapped_handler, route_definition, auth_config, security_config = nil) ⇒ RouteAuthWrapper

Returns a new instance of RouteAuthWrapper.



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

def initialize(wrapped_handler, route_definition, auth_config, security_config = nil)
  @wrapped_handler  = wrapped_handler
  @route_definition = route_definition
  @auth_config      = auth_config
  @security_config  = security_config

  # Initialize extracted components
  @strategy_resolver = RouteAuthWrapperComponents::StrategyResolver.new(auth_config)
  @response_builder  = RouteAuthWrapperComponents::ResponseBuilder.new(route_definition, auth_config, security_config)
  @role_authorizer   = RouteAuthWrapperComponents::RoleAuthorization.new(route_definition)
end

Instance Attribute Details

#auth_configObject (readonly)

Returns the value of attribute auth_config.



27
28
29
# File 'lib/otto/security/authentication/route_auth_wrapper.rb', line 27

def auth_config
  @auth_config
end

#route_definitionObject (readonly)

Returns the value of attribute route_definition.



27
28
29
# File 'lib/otto/security/authentication/route_auth_wrapper.rb', line 27

def route_definition
  @route_definition
end

#security_configObject (readonly)

Returns the value of attribute security_config.



27
28
29
# File 'lib/otto/security/authentication/route_auth_wrapper.rb', line 27

def security_config
  @security_config
end

#wrapped_handlerObject (readonly)

Returns the value of attribute wrapped_handler.



27
28
29
# File 'lib/otto/security/authentication/route_auth_wrapper.rb', line 27

def wrapped_handler
  @wrapped_handler
end

Instance Method Details

#call(env, extra_params = {}) ⇒ Array

Execute authentication then call wrapped handler

Parameters:

  • env (Hash)

    Rack environment

  • extra_params (Hash) (defaults to: {})

    Additional parameters

Returns:

  • (Array)

    Rack response array



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/otto/security/authentication/route_auth_wrapper.rb', line 46

def call(env, extra_params = {})
  auth_requirements = route_definition.auth_requirements

  # Routes without auth requirement get anonymous StrategyResult
  return handle_anonymous_route(env, extra_params) if auth_requirements.empty?

  # Validate all strategies exist before executing any (fail-fast)
  validation_error = validate_strategies(auth_requirements, env)
  return validation_error if validation_error

  # Try each strategy in order (first success wins)
  authenticate_and_authorize(env, extra_params, auth_requirements)
end