Documentation
¶
Overview ¶
Package auth provides authentication for Virtual MCP Server.
This package defines:
- OutgoingAuthRegistry: Registry for managing backend authentication strategies
- Strategy: Pluggable authentication strategies for backends
Incoming authentication uses pkg/auth middleware (OIDC, local, anonymous) which directly creates pkg/auth.Identity in context.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Authorizer ¶
type Authorizer interface {
// Authorize checks if an identity is authorized to perform an action on a resource.
Authorize(ctx context.Context, identity *auth.Identity, action string, resource string) error
// AuthorizeToolCall checks if an identity can call a specific tool.
AuthorizeToolCall(ctx context.Context, identity *auth.Identity, toolName string) error
// AuthorizeResourceAccess checks if an identity can access a specific resource.
AuthorizeResourceAccess(ctx context.Context, identity *auth.Identity, resourceURI string) error
}
Authorizer handles authorization decisions. This integrates with ToolHive's existing Cedar-based authorization.
type DefaultOutgoingAuthRegistry ¶ added in v0.6.0
type DefaultOutgoingAuthRegistry struct {
// contains filtered or unexported fields
}
DefaultOutgoingAuthRegistry is a thread-safe implementation of OutgoingAuthRegistry that maintains a registry of authentication strategies.
Thread-safety: Safe for concurrent calls to RegisterStrategy and GetStrategy. Strategy implementations must be thread-safe as they are called concurrently. It uses sync.RWMutex for thread-safety as HTTP servers are inherently concurrent.
This registry supports dynamic registration of strategies and retrieval by name. It does not perform authentication itself - that is done by the Strategy implementations.
Example usage:
registry := NewDefaultOutgoingAuthRegistry()
registry.RegisterStrategy("header_injection", NewHeaderInjectionStrategy())
strategy, err := registry.GetStrategy("header_injection")
if err == nil {
err = strategy.Authenticate(ctx, req, metadata)
}
func NewDefaultOutgoingAuthRegistry ¶ added in v0.6.0
func NewDefaultOutgoingAuthRegistry() *DefaultOutgoingAuthRegistry
NewDefaultOutgoingAuthRegistry creates a new DefaultOutgoingAuthRegistry with an empty strategy registry.
Strategies must be registered using RegisterStrategy before they can be used for authentication.
func (*DefaultOutgoingAuthRegistry) GetStrategy ¶ added in v0.6.0
func (r *DefaultOutgoingAuthRegistry) GetStrategy(name string) (Strategy, error)
GetStrategy retrieves an authentication strategy by name.
This method is thread-safe for concurrent reads. It returns the strategy if found, or an error if no strategy is registered with the given name.
Parameters:
- name: The identifier of the strategy to retrieve
Returns:
- Strategy: The registered strategy
- error: An error if the strategy is not found
func (*DefaultOutgoingAuthRegistry) RegisterStrategy ¶ added in v0.6.0
func (r *DefaultOutgoingAuthRegistry) RegisterStrategy(name string, strategy Strategy) error
RegisterStrategy registers a new authentication strategy.
This method is thread-safe and validates that:
- name is not empty
- strategy is not nil
- strategy.Name() matches the registration name
- no strategy is already registered with the same name
Parameters:
- name: The unique identifier for this strategy
- strategy: The Strategy implementation to register
Returns an error if validation fails or a strategy with the same name already exists.
type OutgoingAuthRegistry ¶ added in v0.6.0
type OutgoingAuthRegistry interface {
// GetStrategy retrieves an authentication strategy by name.
// Returns an error if the strategy is not found.
GetStrategy(name string) (Strategy, error)
// RegisterStrategy registers a new authentication strategy.
// The strategy name must match the name returned by strategy.Name().
// Returns an error if:
// - name is empty
// - strategy is nil
// - a strategy with the same name is already registered
// - strategy.Name() does not match the registration name
RegisterStrategy(name string, strategy Strategy) error
}
OutgoingAuthRegistry manages authentication strategies for outgoing requests to backend MCP servers. This is a registry that stores and retrieves Strategy implementations.
The registry supports dynamic strategy registration, allowing custom authentication strategies to be added at runtime. Once registered, strategies can be retrieved by name and used to authenticate requests to backends.
Responsibilities:
- Maintain registry of available strategies
- Retrieve strategies by name
- Register new strategies dynamically
This registry does NOT perform authentication itself. Authentication is performed by Strategy implementations retrieved from this registry.
Usage Pattern:
- Register strategies during application initialization
- Resolve strategy once at client creation time (cold path)
- Call strategy.Authenticate() directly per-request (hot path)
Thread-safety: Implementations must be safe for concurrent access.
type Strategy ¶
type Strategy interface {
// Name returns the strategy identifier.
Name() string
// Authenticate performs authentication and modifies the request.
// The metadata contains strategy-specific configuration.
Authenticate(ctx context.Context, req *http.Request, metadata map[string]any) error
// Validate checks if the strategy configuration is valid.
Validate(metadata map[string]any) error
}
Strategy defines how to authenticate to a backend. This interface enables pluggable authentication strategies.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package converters provides functions to convert external authentication configurations to vMCP auth strategy metadata.
|
Package converters provides functions to convert external authentication configurations to vMCP auth strategy metadata. |
|
Package factory provides factory functions for creating vMCP authentication components.
|
Package factory provides factory functions for creating vMCP authentication components. |
|
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |
|
Package strategies provides authentication strategy implementations for Virtual MCP Server.
|
Package strategies provides authentication strategy implementations for Virtual MCP Server. |