session

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package session provides JWT-based session ID generation and validation

Index

Constants

View Source
const (
	// DefaultSessionDuration is the default duration for session JWTs
	DefaultSessionDuration = 24 * time.Hour
)

Variables

View Source
var ErrInvalidBackendInitToken = errors.New("invalid backend-init token")

ErrInvalidBackendInitToken is returned when a backend-init JWT cannot be validated

Functions

func DeriveEncryptionKey added in v0.7.0

func DeriveEncryptionKey(signingKey []byte) ([]byte, error)

DeriveEncryptionKey derives a 32-byte AES-256 key from the session signing key using HKDF.

func WithEncryptionKey added in v0.7.0

func WithEncryptionKey(key []byte) func(c *Cache)

WithEncryptionKey sets the AES-256 key for encrypting user tokens in Redis.

func WithRedisClient added in v0.6.0

func WithRedisClient(client *redis.Client) func(c *Cache)

WithRedisClient configures the cache to use an existing redis client

Types

type BackendInitClaims added in v0.7.0

type BackendInitClaims struct {
	jwt.RegisteredClaims
	Purpose string `json:"purpose"`
	Host    string `json:"host"`
}

BackendInitClaims represents the claims in a short-lived JWT used to authenticate the hairpin backend-init request the router makes when lazily initializing a session with an upstream MCP server.

type Cache

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

Cache implements a cache

func NewCache

func NewCache(opts ...func(*Cache)) (*Cache, error)

NewCache returns a new cache. Pass WithRedisClient to use an external redis store; otherwise an in-memory cache is returned.

func (*Cache) AddSession

func (c *Cache) AddSession(ctx context.Context, key, mcpServerID, mcpSession string, ttl time.Duration) (bool, error)

AddSession will add a session under the key. If the key exists it will append that session. ttl sets the expiry on the Redis hash key; pass 0 for no expiry (in-memory mode ignores ttl).

func (*Cache) DeleteSessions

func (c *Cache) DeleteSessions(ctx context.Context, key ...string) error

DeleteSessions deletes sessions and associated metadata from the cache

func (*Cache) DeleteUserToken added in v0.7.0

func (c *Cache) DeleteUserToken(ctx context.Context, sessionID, serverName string) error

DeleteUserToken removes a cached upstream token for the given session and server.

func (*Cache) GetClientElicitation added in v0.6.0

func (c *Cache) GetClientElicitation(ctx context.Context, gatewaySessionID string) (bool, error)

GetClientElicitation returns whether the client for this gateway session supports elicitation

func (*Cache) GetSession

func (c *Cache) GetSession(ctx context.Context, key string) (map[string]string, error)

GetSession returns a session from the cache

func (*Cache) GetUserToken added in v0.7.0

func (c *Cache) GetUserToken(ctx context.Context, sessionID, serverName string) (string, bool, error)

GetUserToken retrieves a cached upstream token. Returns ("", false, nil) on miss. JWT tokens are checked for expiry; expired tokens are deleted and treated as a miss.

func (*Cache) KeyExists

func (c *Cache) KeyExists(ctx context.Context, key string) (bool, error)

KeyExists checks if a key exists in the cache

func (*Cache) RemoveServerSession

func (c *Cache) RemoveServerSession(ctx context.Context, key, mcpServerID string) error

RemoveServerSession remove specific server session form cache

func (*Cache) SetClientElicitation added in v0.6.0

func (c *Cache) SetClientElicitation(ctx context.Context, gatewaySessionID string, ttl time.Duration) error

SetClientElicitation records that the client for this gateway session supports elicitation. ttl sets the key expiry in Redis; pass 0 for no expiry (in-memory mode ignores ttl).

func (*Cache) SetUserToken added in v0.7.0

func (c *Cache) SetUserToken(ctx context.Context, sessionID, serverName, token string) error

SetUserToken stores a per-user upstream token in the session hash.

type Claims

type Claims struct {
	jwt.RegisteredClaims
}

Claims represents the claims in a session JWT

type Deleter

type Deleter interface {
	DeleteSessions(ctx context.Context, key ...string) error
}

Deleter interface for providing session deletion

type JWTManager

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

JWTManager handles JWT generation and validation for session IDs

func NewJWTManager

func NewJWTManager(signingKey string, sessionLength int64, logger *slog.Logger, sessionHandler Deleter) (*JWTManager, error)

NewJWTManager creates a new JWT manager with the provided signing key

func (*JWTManager) Generate

func (m *JWTManager) Generate() string

Generate returns a session id JWT to fullfil SessionIdManager interface

func (*JWTManager) GenerateBackendInitToken added in v0.7.0

func (m *JWTManager) GenerateBackendInitToken(host string) (string, error)

GenerateBackendInitToken creates a short-lived JWT bound to a specific upstream host, used to authenticate the router's hairpin backend-init request. The token is signed with the same HMAC key used for client session JWTs but uses a distinct audience and purpose claim so it cannot be confused with a client session token.

func (*JWTManager) GetExpiresIn

func (m *JWTManager) GetExpiresIn(tokenValue string) (time.Time, error)

GetExpiresIn returns the time a token will expire

func (*JWTManager) Terminate

func (m *JWTManager) Terminate(sessionID string) (isNotAllowed bool, err error)

Terminate part of the SessionIDManager interface. Will remove the associated sessions from cache

func (*JWTManager) Validate

func (m *JWTManager) Validate(tokenValue string) (bool, error)

Validate validates a JWT token and fulfils SessionIdManager interface. returns IsInValid as a bool

func (*JWTManager) ValidateBackendInitToken added in v0.7.0

func (m *JWTManager) ValidateBackendInitToken(tokenValue, expectedHost string) error

ValidateBackendInitToken verifies a short-lived backend-init JWT. The token must be signed with the manager's HMAC key, have issuer=mcp-gateway, audience=mcp-router, purpose=backend-init and a host claim equal to the expected target host. Any mismatch or expiry returns ErrInvalidBackendInitToken.

Jump to

Keyboard shortcuts

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