|
| 1 | +package tokenexchange |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/golang-jwt/jwt/v5" |
| 11 | + |
| 12 | + "github.com/stacklok/toolhive/pkg/auth" |
| 13 | + "github.com/stacklok/toolhive/pkg/logger" |
| 14 | + "github.com/stacklok/toolhive/pkg/transport/types" |
| 15 | +) |
| 16 | + |
| 17 | +// Middleware type constant |
| 18 | +const ( |
| 19 | + MiddlewareType = "tokenexchange" |
| 20 | +) |
| 21 | + |
| 22 | +// Header injection strategy constants |
| 23 | +const ( |
| 24 | + // HeaderStrategyReplace replaces the Authorization header with the exchanged token |
| 25 | + HeaderStrategyReplace = "replace" |
| 26 | + // HeaderStrategyCustom adds the exchanged token to a custom header |
| 27 | + HeaderStrategyCustom = "custom" |
| 28 | +) |
| 29 | + |
| 30 | +var errUnknownStrategy = errors.New("unknown token injection strategy") |
| 31 | + |
| 32 | +// MiddlewareParams represents the parameters for token exchange middleware |
| 33 | +type MiddlewareParams struct { |
| 34 | + TokenExchangeConfig *Config `json:"token_exchange_config,omitempty"` |
| 35 | +} |
| 36 | + |
| 37 | +// Config holds configuration for token exchange middleware |
| 38 | +type Config struct { |
| 39 | + // TokenURL is the OAuth 2.0 token endpoint URL |
| 40 | + TokenURL string `json:"token_url"` |
| 41 | + |
| 42 | + // ClientID is the OAuth 2.0 client identifier |
| 43 | + ClientID string `json:"client_id"` |
| 44 | + |
| 45 | + // ClientSecret is the OAuth 2.0 client secret |
| 46 | + ClientSecret string `json:"client_secret"` |
| 47 | + |
| 48 | + // Audience is the target audience for the exchanged token |
| 49 | + Audience string `json:"audience"` |
| 50 | + |
| 51 | + // Scopes is the list of scopes to request for the exchanged token |
| 52 | + Scopes []string `json:"scopes,omitempty"` |
| 53 | + |
| 54 | + // HeaderStrategy determines how to inject the token |
| 55 | + // Valid values: HeaderStrategyReplace (default), HeaderStrategyCustom |
| 56 | + HeaderStrategy string `json:"header_strategy,omitempty"` |
| 57 | + |
| 58 | + // ExternalTokenHeaderName is the name of the custom header to use when HeaderStrategy is "custom" |
| 59 | + ExternalTokenHeaderName string `json:"external_token_header_name,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +// Middleware wraps token exchange middleware functionality |
| 63 | +type Middleware struct { |
| 64 | + middleware types.MiddlewareFunction |
| 65 | +} |
| 66 | + |
| 67 | +// Handler returns the middleware function used by the proxy. |
| 68 | +func (m *Middleware) Handler() types.MiddlewareFunction { |
| 69 | + return m.middleware |
| 70 | +} |
| 71 | + |
| 72 | +// Close cleans up any resources used by the middleware. |
| 73 | +func (*Middleware) Close() error { |
| 74 | + // Token exchange middleware doesn't need cleanup |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// CreateMiddleware factory function for token exchange middleware |
| 79 | +func CreateMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error { |
| 80 | + var params MiddlewareParams |
| 81 | + if err := json.Unmarshal(config.Parameters, ¶ms); err != nil { |
| 82 | + return fmt.Errorf("failed to unmarshal token exchange middleware parameters: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + // Token exchange config is required when this middleware type is specified |
| 86 | + if params.TokenExchangeConfig == nil { |
| 87 | + return fmt.Errorf("token exchange configuration is required but not provided") |
| 88 | + } |
| 89 | + |
| 90 | + // Validate configuration |
| 91 | + if err := validateTokenExchangeConfig(params.TokenExchangeConfig); err != nil { |
| 92 | + return fmt.Errorf("invalid token exchange configuration: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + middleware, err := CreateTokenExchangeMiddlewareFromClaims(*params.TokenExchangeConfig) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("invalid token exchange middleware config: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + tokenExchangeMw := &Middleware{ |
| 101 | + middleware: middleware, |
| 102 | + } |
| 103 | + |
| 104 | + // Add middleware to runner |
| 105 | + runner.AddMiddleware(tokenExchangeMw) |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// validateTokenExchangeConfig validates the token exchange configuration |
| 111 | +func validateTokenExchangeConfig(config *Config) error { |
| 112 | + if config.HeaderStrategy == HeaderStrategyCustom && config.ExternalTokenHeaderName == "" { |
| 113 | + return fmt.Errorf("external_token_header_name must be specified when header_strategy is '%s'", HeaderStrategyCustom) |
| 114 | + } |
| 115 | + |
| 116 | + if config.HeaderStrategy != "" && |
| 117 | + config.HeaderStrategy != HeaderStrategyReplace && |
| 118 | + config.HeaderStrategy != HeaderStrategyCustom { |
| 119 | + return fmt.Errorf("invalid header_strategy: %s (valid values: '%s', '%s')", |
| 120 | + config.HeaderStrategy, HeaderStrategyReplace, HeaderStrategyCustom) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +// injectionFunc is a function that injects a token into an HTTP request |
| 127 | +type injectionFunc func(*http.Request, string) error |
| 128 | + |
| 129 | +// createReplaceInjector creates an injection function that replaces the Authorization header |
| 130 | +func createReplaceInjector() injectionFunc { |
| 131 | + return func(r *http.Request, token string) error { |
| 132 | + logger.Debugf("Token exchange successful, replacing Authorization header") |
| 133 | + r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 134 | + return nil |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// createCustomInjector creates an injection function that adds the token to a custom header |
| 139 | +func createCustomInjector(headerName string) injectionFunc { |
| 140 | + // Validate header name at creation time |
| 141 | + if headerName == "" { |
| 142 | + return func(_ *http.Request, _ string) error { |
| 143 | + return fmt.Errorf("external_token_header_name must be specified when header_strategy is '%s'", HeaderStrategyCustom) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return func(r *http.Request, token string) error { |
| 148 | + logger.Debugf("Token exchange successful, adding token to custom header: %s", headerName) |
| 149 | + r.Header.Set(headerName, fmt.Sprintf("Bearer %s", token)) |
| 150 | + return nil |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// CreateTokenExchangeMiddlewareFromClaims creates a middleware that uses token claims |
| 155 | +// from the auth middleware to perform token exchange. |
| 156 | +// This is a public function for direct usage in proxy commands. |
| 157 | +func CreateTokenExchangeMiddlewareFromClaims(config Config) (types.MiddlewareFunction, error) { |
| 158 | + // Determine injection strategy at startup time |
| 159 | + strategy := config.HeaderStrategy |
| 160 | + if strategy == "" { |
| 161 | + strategy = HeaderStrategyReplace // Default to replace for backwards compatibility |
| 162 | + } |
| 163 | + |
| 164 | + var injectToken injectionFunc |
| 165 | + switch strategy { |
| 166 | + case HeaderStrategyReplace: |
| 167 | + injectToken = createReplaceInjector() |
| 168 | + case HeaderStrategyCustom: |
| 169 | + injectToken = createCustomInjector(config.ExternalTokenHeaderName) |
| 170 | + default: |
| 171 | + return nil, fmt.Errorf("%w: invalid header injection strategy %s", errUnknownStrategy, strategy) |
| 172 | + } |
| 173 | + |
| 174 | + // Create base exchange config at startup time with all static fields |
| 175 | + baseExchangeConfig := ExchangeConfig{ |
| 176 | + TokenURL: config.TokenURL, |
| 177 | + ClientID: config.ClientID, |
| 178 | + ClientSecret: config.ClientSecret, |
| 179 | + Audience: config.Audience, |
| 180 | + Scopes: config.Scopes, |
| 181 | + // SubjectTokenProvider will be set per request |
| 182 | + } |
| 183 | + |
| 184 | + return func(next http.Handler) http.Handler { |
| 185 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 186 | + // Get claims from the auth middleware |
| 187 | + claims, ok := r.Context().Value(auth.ClaimsContextKey{}).(jwt.MapClaims) |
| 188 | + if !ok { |
| 189 | + logger.Debug("No claims found in context, proceeding without token exchange") |
| 190 | + next.ServeHTTP(w, r) |
| 191 | + return |
| 192 | + } |
| 193 | + |
| 194 | + // Extract the original token from the Authorization header |
| 195 | + authHeader := r.Header.Get("Authorization") |
| 196 | + if authHeader == "" || !strings.HasPrefix(authHeader, "Bearer ") { |
| 197 | + logger.Debug("No valid Bearer token found, proceeding without token exchange") |
| 198 | + next.ServeHTTP(w, r) |
| 199 | + return |
| 200 | + } |
| 201 | + |
| 202 | + subjectToken := strings.TrimPrefix(authHeader, "Bearer ") |
| 203 | + if subjectToken == "" { |
| 204 | + logger.Debug("Empty Bearer token, proceeding without token exchange") |
| 205 | + next.ServeHTTP(w, r) |
| 206 | + return |
| 207 | + } |
| 208 | + |
| 209 | + // Log some claim information for debugging |
| 210 | + if sub, exists := claims["sub"]; exists { |
| 211 | + logger.Debugf("Performing token exchange for subject: %v", sub) |
| 212 | + } |
| 213 | + |
| 214 | + // Create a copy of the base config with the request-specific subject token |
| 215 | + exchangeConfig := baseExchangeConfig |
| 216 | + exchangeConfig.SubjectTokenProvider = func() (string, error) { |
| 217 | + return subjectToken, nil |
| 218 | + } |
| 219 | + |
| 220 | + // Get token from token source |
| 221 | + tokenSource := exchangeConfig.TokenSource(r.Context()) |
| 222 | + exchangedToken, err := tokenSource.Token() |
| 223 | + if err != nil { |
| 224 | + logger.Warnf("Token exchange failed: %v", err) |
| 225 | + http.Error(w, "Token exchange failed", http.StatusUnauthorized) |
| 226 | + return |
| 227 | + } |
| 228 | + |
| 229 | + // Inject the exchanged token into the request using the pre-selected strategy |
| 230 | + if err := injectToken(r, exchangedToken.AccessToken); err != nil { |
| 231 | + logger.Warnf("Failed to inject token: %v", err) |
| 232 | + http.Error(w, "Token injection failed", http.StatusInternalServerError) |
| 233 | + return |
| 234 | + } |
| 235 | + |
| 236 | + next.ServeHTTP(w, r) |
| 237 | + }) |
| 238 | + }, nil |
| 239 | +} |
0 commit comments