Documentation
¶
Overview ¶
Package sessionbinding binds sessions to the validated issuer and subject, plus the client and optional outermost actor for delegated identities. It never binds to a bearer token. Authentication must run before ownership enforcement.
Index ¶
- Constants
- Variables
- func AddOwnedSession(ctx context.Context, manager Store, sess session.Session) error
- func BindOwner(ctx context.Context, sess session.Session) error
- func Check(r *http.Request, id string, lookup Lookup) error
- func Format(iss, sub string) (string, error)
- func FromIdentity(identity *auth.Identity) (string, error)
- func NewMiddleware(extract func(*http.Request) (string, error), lookup Lookup, ...) (func(http.Handler) http.Handler, error)
- func Parse(value string) (iss, sub string, ok bool)
- func QueryKeys() []string
- func RequestID(r *http.Request, queryKey string) (string, error)
- func ResolveRequestID(r *http.Request) (string, error)
- func Validate(stored string, identity *auth.Identity) error
- func WriteOwnershipError(w http.ResponseWriter, requestID any, err error)
- type Lookup
- type Store
Constants ¶
const MetadataKey = session.MetadataKeyIdentityBinding
MetadataKey is shared with persisted vMCP sessions for compatibility.
const UnauthenticatedSentinel = "unauthenticated"
UnauthenticatedSentinel is reserved for an actually absent identity.
Variables ¶
var ( // ErrInvalidBinding denotes an identity that cannot be bound to a session. ErrInvalidBinding = errors.New("invalid identity binding") // ErrNotFound deliberately conceals missing, unowned and foreign sessions. ErrNotFound = session.ErrSessionNotFound )
Functions ¶
func AddOwnedSession ¶
AddOwnedSession binds an ordinary proxy session before atomic publication. sess must be newly constructed and unpublished; its metadata is modified. A collision may reuse only the same owner's record, never replace metadata. Shared storage does not provide cross-replica live-stream delivery.
func Check ¶
Check validates a session-bearing request using its authenticated context. Empty IDs are sessionless; callers enforce any protocol-required ID separately.
func FromIdentity ¶
FromIdentity accepts nil only for auth-disabled deployments. A synthetic anonymous/local identity with valid claims binds normally; malformed non-nil identities must never downgrade to the unauthenticated sentinel.
The binding depends on the admission route. Interactive and unspecified identities, including sessionless grants without the delegated-access marker, bind two fields: issuer and subject. A delegated identity binds four fields (issuer, subject, route, client_id), or six when it carries an actor (plus the outermost act issuer and subject). A delegated identity with a missing or invalid client_id, a malformed, incomplete or explicitly null act, or an unknown route returns ErrInvalidBinding. An absent act is not an error.
func NewMiddleware ¶
func NewMiddleware(extract func(*http.Request) (string, error), lookup Lookup, reject func(http.ResponseWriter, *http.Request, error)) (func(http.Handler) http.Handler, error)
NewMiddleware constructs ownership enforcement. extract must classify requests without consuming their bodies and return an empty ID only for truly sessionless requests. reject must conceal ErrNotFound (404) and map storage failures to 503. Install inside authentication and outside stateful handlers/restoration.
func Parse ¶
Parse rejects malformed bindings and the unauthenticated sentinel. For a delegated binding it returns only the issuer and subject prefix. Use Validate to compare full bindings; passing the parsed fields to Format loses ownership data.
func QueryKeys ¶ added in v0.75.0
func QueryKeys() []string
QueryKeys returns the query parameter names that carry an MCP session id, in a new slice the caller may keep.
func RequestID ¶
RequestID reads protocol session carriers, rejecting duplicate or conflicting values before a backend can interpret them differently. queryKey may be empty.
func ResolveRequestID ¶ added in v0.75.0
ResolveRequestID resolves a request's session id from the Mcp-Session-Id header or any query spelling in QueryKeys.
Two carriers with different values are refused with ErrNotFound rather than resolved to one of them: a proxy and the backend could pick differently, and a disagreement there is a session-confusion bug. A query key that matches a known spelling only when case is ignored (SessionId) is refused the same way, because a backend that reads query keys case-insensitively would take it as a carrier. An empty id is not an error; it means the request names no session, which callers handle separately.
func Validate ¶
Validate fails closed for legacy records without an owner. Refreshed tokens are accepted when the full binding matches. It never mutates session state.
func WriteOwnershipError ¶
func WriteOwnershipError(w http.ResponseWriter, requestID any, err error)
WriteOwnershipError uses the ordinary proxies' non-disclosing JSON-RPC 404.