Skip to content

Commit 8bb674d

Browse files
committed
Refactor outgoing auth to separate registry from strategy
Rename OutgoingAuthenticator to OutgoingAuthRegistry to better reflect its responsibility as a strategy registry rather than an authenticator. The interface now focuses solely on strategy management (registration and retrieval), while authentication is performed directly by Strategy implementations. This separation improves performance by eliminating indirection in the hot path (per-request authentication) and clarifies the single responsibility of each component: the registry manages strategies, strategies perform authentication.
1 parent 799e80e commit 8bb674d

File tree

7 files changed

+404
-608
lines changed

7 files changed

+404
-608
lines changed

pkg/vmcp/auth/auth.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Package auth provides authentication for Virtual MCP Server.
22
//
33
// This package defines:
4-
// - OutgoingAuthenticator: Authenticates vMCP to backend servers
4+
// - OutgoingAuthRegistry: Registry for managing backend authentication strategies
55
// - Strategy: Pluggable authentication strategies for backends
66
//
77
// Incoming authentication uses pkg/auth middleware (OIDC, local, anonymous)
@@ -17,24 +17,39 @@ import (
1717
"github.com/stacklok/toolhive/pkg/auth"
1818
)
1919

20-
// OutgoingAuthenticator handles authentication to backend MCP servers.
21-
// This is responsible for obtaining and injecting appropriate credentials
22-
// for each backend based on its authentication strategy.
20+
// OutgoingAuthRegistry manages authentication strategies for outgoing requests to backend MCP servers.
21+
// This is a registry that stores and retrieves Strategy implementations.
2322
//
24-
// The specific authentication strategies and their behavior will be defined
25-
// during implementation based on the design decisions documented in the
26-
// Virtual MCP Server proposal.
27-
type OutgoingAuthenticator interface {
28-
// AuthenticateRequest adds authentication to an outgoing backend request.
29-
// The strategy and metadata are provided in the BackendTarget.
30-
AuthenticateRequest(ctx context.Context, req *http.Request, strategy string, metadata map[string]any) error
31-
32-
// GetStrategy returns the authentication strategy handler for a given strategy name.
33-
// This enables extensibility - new strategies can be registered.
23+
// The registry supports dynamic strategy registration, allowing custom authentication
24+
// strategies to be added at runtime. Once registered, strategies can be retrieved
25+
// by name and used to authenticate requests to backends.
26+
//
27+
// Responsibilities:
28+
// - Maintain registry of available strategies
29+
// - Retrieve strategies by name
30+
// - Register new strategies dynamically
31+
//
32+
// This registry does NOT perform authentication itself. Authentication is performed
33+
// by Strategy implementations retrieved from this registry.
34+
//
35+
// Usage Pattern:
36+
// 1. Register strategies during application initialization
37+
// 2. Resolve strategy once at client creation time (cold path)
38+
// 3. Call strategy.Authenticate() directly per-request (hot path)
39+
//
40+
// Thread-safety: Implementations must be safe for concurrent access.
41+
type OutgoingAuthRegistry interface {
42+
// GetStrategy retrieves an authentication strategy by name.
43+
// Returns an error if the strategy is not found.
3444
GetStrategy(name string) (Strategy, error)
3545

3646
// RegisterStrategy registers a new authentication strategy.
37-
// This allows custom auth strategies to be added at runtime.
47+
// The strategy name must match the name returned by strategy.Name().
48+
// Returns an error if:
49+
// - name is empty
50+
// - strategy is nil
51+
// - a strategy with the same name is already registered
52+
// - strategy.Name() does not match the registration name
3853
RegisterStrategy(name string, strategy Strategy) error
3954
}
4055

pkg/vmcp/auth/outgoing_authenticator.go

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)