{"modulePath":"github.com/stacklok/toolhive","version":"v0.72.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package session\n\nPackage session provides OAuth session management for the authorization server. Sessions link issued access tokens to upstream identity provider tokens, enabling token exchange and refresh operations.\n\n## Constants\n\n```go\nconst ClientIDClaimKey = \"client_id\"\n```\nClientIDClaimKey is the JWT claim key for the OAuth client ID. This identifies which client was issued the token.\n\n```go\nconst EmailClaimKey = \"email\"\n```\nEmailClaimKey is the JWT claim key for the user's email address. Per OIDC Core Section 5.1.\n\n```go\nconst NameClaimKey = \"name\"\n```\nNameClaimKey is the JWT claim key for the user's display name. Per OIDC Core Section 5.1.\n\n```go\nconst UpstreamSubjectClaimKey = claimnames.UpstreamSubjectKey\n```\nUpstreamSubjectClaimKey is the claim name of the upstream subject claim.\n\nDeprecated: use claimnames.UpstreamSubjectKey.\n\n## Types\n\n```go\ntype Factory func(subject, idpSessionID, clientID string) fosite.Session\n```\nFactory is a function that creates a new session given subject, IDP session ID, and client ID. This allows storage implementations to create sessions without importing this package directly.\n\nThe factory is primarily used during deserialization where the clientID may be empty because the client\\_id claim is preserved in the JWT claims Extra map from the original serialized session data.\n\n```go\ntype Session struct {\n\t*oauth2.JWTSession\n\n\t// UpstreamSessionID links this session to stored upstream IDP tokens.\n\t// This ID is used to retrieve the IDP tokens from storage.\n\tUpstreamSessionID string\n\n\t// VerifiedUpstream is preserved server-side through code/refresh storage. It is\n\t// not recorded at new logins when SubjectIdentifierAudiences is empty, and\n\t// becomes the sub_id and upstream subject claims only for listed audiences.\n\tVerifiedUpstream UpstreamSubject `json:\"verified_upstream,omitzero\"`\n}\n```\nSession extends fosite's JWT session with an IDP session reference. This allows the authorization server to link issued tokens to upstream IDP tokens stored separately.\n\nMost methods are provided by the embedded \\*oauth2.JWTSession. This type adds IDP session tracking and verified upstream identity provenance; Clone() preserves those fields along with the embedded JWT session.\n\nConcurrency: Sessions are designed to be request-scoped and are not safe for concurrent access from multiple goroutines. This follows Fosite's design pattern where sessions are created per-request, populated by handlers, and then persisted to storage. The storage layer is responsible for thread-safe access to stored sessions.\n\n```go\nfunc New(subject, idpSessionID, clientID string, claims UserClaims) *Session\n```\nNew creates a new Session with the given subject, IDP session ID, client ID, and optional user profile claims.\n\nParameters:\n\n  - subject: The OAuth subject (user identifier). May be empty for placeholder sessions.\n  - idpSessionID: Links to upstream IDP tokens in storage. If provided, it will be included in the JWT claims as \"tsid\" to allow proxy middleware to look up upstream IDP tokens.\n  - clientID: The OAuth client ID. When provided, it will be included in the JWT claims as \"client\\_id\" for binding verification per RFC 9068.\n  - claims: Optional user profile claims (name, email) from the upstream IDP. Included in the JWT per OIDC Core Section 5.1.\n\nClientID handling:\n\n  - For token issuance (authorize handler): Pass the client ID to ensure RFC 9068 compliance. The client\\_id claim will be embedded in the JWT.\n  - For token exchange (token handler): May be empty - Fosite copies claims from the stored authorize session, preserving the original client\\_id.\n  - For deserialization: May be empty - the client\\_id claim is preserved in the JWT claims Extra map from the serialized session data.\n\nNote: The remaining RFC 9068 required claims (iss, aud, exp, iat, jti) are populated by Fosite during token generation. This session only initializes custom claims that are not part of Fosite's standard JWT handling.\n\n```go\nfunc (s *Session) ClearSubjectIdentifier()\n```\nClearSubjectIdentifier removes any persisted or source-token sub\\_id and upstream subject claim before the current grant decides whether it is eligible to issue them. It mutates the receiver's claims in place; a caller holding a session loaded from storage must Clone it first.\n\n```go\nfunc (s *Session) Clone() fosite.Session\n```\nClone creates a deep copy of the session. This overrides the embedded JWTSession.Clone() to also copy UpstreamSessionID and the verified upstream identity.\n\n```go\nfunc (s *Session) GetIDPSessionID() string\n```\nGetIDPSessionID returns the IDP session ID.\n\n```go\nfunc (s *Session) SetIDPSessionID(id string)\n```\nSetIDPSessionID sets the IDP session ID.\n\n```go\nfunc (s *Session) SetSubjectIdentifier(upstream UpstreamSubject)\n```\nSetSubjectIdentifier sets the RFC 9493 sub\\_id claim and the upstream subject claim from the verified upstream identity. It does nothing unless the identity is complete.\n\nThe upstream identity is the one the first upstream verified, and that upstream is the only one that creates users. The local user is found by the link key (upstream name, subject\\_claim value). The sub\\_id is the raw (iss, sub) and is not used to find users, so the two can drift apart: a changed link key with the same (iss, sub) gives one person two users with the same sub\\_id, and a same link key with a different issuer gives one user two sub\\_ids. See docs/arch/11-auth-server-storage.md, \"One upstream identity, one user\".\n\nIt mutates the receiver's claims in place; a caller holding a session loaded from storage must Clone it first.\n\n```go\nfunc (s *Session) UnmarshalJSON(data []byte) error\n```\nUnmarshalJSON decodes a session as encoding/json would, except that the authorization\\_details claim keeps its encoded bytes as a json.RawMessage instead of being decoded into \\[]any, which would round an integer above 2^53. A session read back from storage then holds the claim in the same form as the session that was stored.\n\n```go\ntype UpstreamSession interface {\n\toauth2.JWTSessionContainer\n\tGetIDPSessionID() string\n}\n```\nUpstreamSession is an interface for sessions that support IDP linking and JWT claims. It embeds oauth2.JWTSessionContainer (which includes fosite.Session, GetJWTClaims, and GetJWTHeader) and adds IDP session tracking. This interface is used by the storage layer for serialization/deserialization.\n\n```go\ntype UpstreamSubject struct {\n\tIssuer      string `json:\"iss,omitempty\"`\n\tSubject     string `json:\"sub,omitempty\"`\n\tStableClaim string `json:\"stable_claim,omitempty\"`\n\tStableValue string `json:\"stable_value,omitempty\"`\n}\n```\nUpstreamSubject is the verified identity of the upstream OIDC login that established the local user. Issuer and Subject are the exact iss and sub of the validated upstream ID token. StableClaim names the claim the upstream is configured to key users on (its subject\\_claim, or \"sub\"), and StableValue is that claim's value from the same ID token. All four are set together or not at all.\n\nupstream.Identity carries the same four values as its Verified\\* fields, because the upstream package cannot import this one. verifiedUpstreamIdentity in handlers/callback.go converts between them; a member added here must be added there and to upstream.Identity too, or IsComplete never holds and the claims are silently dropped.\n\n```go\nfunc (u UpstreamSubject) IsComplete() bool\n```\nIsComplete reports whether every member is set. Only a complete value is stored or disclosed.\n\n```go\ntype UserClaims struct {\n\t// Name is the user's display name (OIDC \"name\" claim).\n\tName string\n\t// Email is the user's email address (OIDC \"email\" claim).\n\tEmail string\n}\n```\nUserClaims holds optional user profile claims from the upstream IDP. Using a struct avoids parameter-ordering mistakes in function signatures that accept multiple string parameters.\n\n","path":"github.com/stacklok/toolhive/pkg/authserver/server/session","name":"session","synopsis":"Package session provides OAuth session management for the authorization server.","isRedistributable":true}
