tokenexchange

package
v0.67.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package tokenexchange adapts reusable OAuth token exchange to ToolHive middleware.

Index

Constants

View Source
const (
	// HeaderStrategyReplace replaces the Authorization header with the exchanged token
	HeaderStrategyReplace = "replace"
	// HeaderStrategyCustom adds the exchanged token to a custom header
	HeaderStrategyCustom = "custom"
)

Header injection strategy constants

View Source
const (
	// EnvClientSecret is the environment variable name for the OAuth client secret
	// This corresponds to the "client_secret" field in the token exchange configuration
	//nolint:gosec // G101: this is an environment variable name, not a credential value
	EnvClientSecret = "TOOLHIVE_TOKEN_EXCHANGE_CLIENT_SECRET"
)

Environment variable names

View Source
const (
	MiddlewareType = "tokenexchange"
)

Middleware type constant

Variables

This section is empty.

Functions

func CreateMiddleware

func CreateMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error

CreateMiddleware factory function for token exchange middleware

func CreateMiddlewareFromHeader

func CreateMiddlewareFromHeader(config Config) (types.MiddlewareFunction, error)

CreateMiddlewareFromHeader creates token exchange middleware that extracts the subject token from the incoming request's Authorization header. This is the recommended approach when the proxy receives authenticated requests and needs to exchange those tokens for backend access.

For external authentication flows (OAuth/OIDC), use CreateMiddlewareFromTokenSource instead.

func CreateMiddlewareFromTokenSource

func CreateMiddlewareFromTokenSource(
	config Config,
	tokenSource oauth2.TokenSource,
) (types.MiddlewareFunction, error)

CreateMiddlewareFromTokenSource creates token exchange middleware using an oauth2.TokenSource. This is the recommended approach for external authentication flows (OAuth/OIDC).

The middleware will automatically select the appropriate token based on config.SubjectTokenType:

  • oauthproto.TokenTypeAccessToken: Uses token.AccessToken
  • oauthproto.TokenTypeIDToken or oauthproto.TokenTypeJWT: Uses token.Extra("id_token")

This moves the token selection logic into the middleware where it belongs, keeping the command layer focused on configuration.

Types

type Config

type Config struct {
	// TokenURL is the OAuth 2.0 token endpoint URL
	TokenURL string `json:"token_url"`

	// ClientID is the OAuth 2.0 client identifier
	ClientID string `json:"client_id"`

	// ClientSecret is the OAuth 2.0 client secret
	ClientSecret string `json:"client_secret"`

	// Audience is the target audience for the exchanged token
	Audience string `json:"audience"`

	// Scopes is the list of scopes to request for the exchanged token
	Scopes []string `json:"scopes,omitempty"`

	// SubjectTokenType specifies the type of the subject token being exchanged.
	// Common values: oauthproto.TokenTypeAccessToken (default), oauthproto.TokenTypeIDToken, oauthproto.TokenTypeJWT.
	// If empty, defaults to oauthproto.TokenTypeAccessToken.
	SubjectTokenType string `json:"subject_token_type,omitempty"`

	// HeaderStrategy determines how to inject the token
	// Valid values: HeaderStrategyReplace (default), HeaderStrategyCustom
	HeaderStrategy string `json:"header_strategy,omitempty"`

	// ExternalTokenHeaderName is the name of the custom header to use when HeaderStrategy is "custom"
	ExternalTokenHeaderName string `json:"external_token_header_name,omitempty"`
}

Config holds configuration for token exchange middleware

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

Middleware wraps token exchange middleware functionality

func (*Middleware) Close

func (*Middleware) Close() error

Close cleans up any resources used by the middleware.

func (*Middleware) Handler

func (m *Middleware) Handler() types.MiddlewareFunction

Handler returns the middleware function used by the proxy.

type MiddlewareParams

type MiddlewareParams struct {
	TokenExchangeConfig *Config `json:"token_exchange_config,omitempty"`
}

MiddlewareParams represents the parameters for token exchange middleware

type SubjectTokenProvider

type SubjectTokenProvider func() (string, error)

SubjectTokenProvider is a function that provides the subject token for exchange. This is used when the token comes from an external source (e.g., OAuth flow) rather than from incoming request headers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL