Class: VectorMCP::Security::AuthManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_mcp/security/auth_manager.rb

Overview

Manages authentication strategies for VectorMCP servers Provides opt-in authentication with zero configuration by default

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthManager

Returns a new instance of AuthManager.



10
11
12
13
14
# File 'lib/vector_mcp/security/auth_manager.rb', line 10

def initialize
  @strategies = {}
  @enabled = false
  @default_strategy = nil
end

Instance Attribute Details

#default_strategyObject (readonly)

Returns the value of attribute default_strategy.



8
9
10
# File 'lib/vector_mcp/security/auth_manager.rb', line 8

def default_strategy
  @default_strategy
end

#enabledObject (readonly)

Returns the value of attribute enabled.



8
9
10
# File 'lib/vector_mcp/security/auth_manager.rb', line 8

def enabled
  @enabled
end

#strategiesObject (readonly)

Returns the value of attribute strategies.



8
9
10
# File 'lib/vector_mcp/security/auth_manager.rb', line 8

def strategies
  @strategies
end

Instance Method Details

#add_strategy(name, strategy) ⇒ Object

Add an authentication strategy

Parameters:

  • name (Symbol)

    the strategy name

  • strategy (Object)

    the strategy instance



32
33
34
# File 'lib/vector_mcp/security/auth_manager.rb', line 32

def add_strategy(name, strategy)
  @strategies[name] = strategy
end

#authenticate(request, strategy: nil) ⇒ Object, false

Authenticate a request using the specified or default strategy

Parameters:

  • request (Hash)

    the request object containing headers, params, etc.

  • strategy (Symbol) (defaults to: nil)

    optional strategy override

Returns:

  • (Object, false)

    authentication result or false if failed



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vector_mcp/security/auth_manager.rb', line 46

def authenticate(request, strategy: nil)
  return { authenticated: true, user: nil } unless @enabled

  strategy_name = strategy || @default_strategy
  auth_strategy = @strategies[strategy_name]

  return { authenticated: false, error: "Unknown strategy: #{strategy_name}" } unless auth_strategy

  begin
    result = auth_strategy.authenticate(request)
    if result
      { authenticated: true, user: result }
    else
      { authenticated: false, error: "Authentication failed" }
    end
  rescue StandardError => e
    { authenticated: false, error: "Authentication error: #{e.message}" }
  end
end

#available_strategiesArray<Symbol>

Get list of available strategies

Returns:

  • (Array<Symbol>)

    array of strategy names



74
75
76
# File 'lib/vector_mcp/security/auth_manager.rb', line 74

def available_strategies
  @strategies.keys
end

#disable!Object

Disable authentication (return to pass-through mode)



24
25
26
27
# File 'lib/vector_mcp/security/auth_manager.rb', line 24

def disable!
  @enabled = false
  @default_strategy = nil
end

#enable!(default_strategy: :api_key) ⇒ Object

Enable authentication with optional default strategy

Parameters:

  • default_strategy (Symbol) (defaults to: :api_key)

    the default authentication strategy to use



18
19
20
21
# File 'lib/vector_mcp/security/auth_manager.rb', line 18

def enable!(default_strategy: :api_key)
  @enabled = true
  @default_strategy = default_strategy
end

#remove_strategy(name) ⇒ Object

Remove an authentication strategy

Parameters:

  • name (Symbol)

    the strategy name to remove



38
39
40
# File 'lib/vector_mcp/security/auth_manager.rb', line 38

def remove_strategy(name)
  @strategies.delete(name)
end

#required?Boolean

Check if authentication is required

Returns:

  • (Boolean)

    true if authentication is enabled



68
69
70
# File 'lib/vector_mcp/security/auth_manager.rb', line 68

def required?
  @enabled
end