session

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package session manages user sessions across MCP tool calls.

Index

Constants

View Source
const DefaultTimeout = 30 * time.Minute

DefaultTimeout is the default session timeout.

Variables

View Source
var ErrSessionNotFound = errors.New("session not found")

ErrSessionNotFound is returned when a session doesn't exist or is expired.

Functions

func GenerateSessionID

func GenerateSessionID() (string, error)

GenerateSessionID creates a cryptographically random session ID. Uses crypto/rand for unpredictability (SESS-05 requirement). Returns 64 hex characters (32 bytes).

Types

type Config

type Config struct {
	// Timeout is the session expiration duration. Default: 30 minutes.
	Timeout time.Duration
}

Config holds session service configuration.

type Session

type Session struct {
	// ID is a cryptographically random identifier, 32 bytes hex-encoded.
	ID string
	// IdentityID references the auth.Identity this session belongs to.
	IdentityID string
	// IdentityName is the human-readable name of the identity.
	IdentityName string
	// Roles are cached from the Identity for fast RBAC lookup.
	Roles []auth.Role
	// CreatedAt is when the session was created (UTC).
	CreatedAt time.Time
	// ExpiresAt is when the session will expire (UTC).
	ExpiresAt time.Time
	// LastAccess is the last time the session was used (UTC).
	LastAccess time.Time
}

Session tracks an authenticated user's context across tool calls.

func (*Session) IsExpired

func (s *Session) IsExpired() bool

IsExpired checks if the session has exceeded its timeout.

func (*Session) Refresh

func (s *Session) Refresh(timeout time.Duration)

Refresh updates LastAccess and extends ExpiresAt by the given duration.

type SessionService

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

SessionService manages session lifecycle.

func NewSessionService

func NewSessionService(store SessionStore, cfg Config) *SessionService

NewSessionService creates a new SessionService with the given store and config.

func (*SessionService) Create

func (s *SessionService) Create(ctx context.Context, identity *auth.Identity) (*Session, error)

Create generates a new session for an identity.

func (*SessionService) Delete

func (s *SessionService) Delete(ctx context.Context, id string) error

Delete terminates a session.

func (*SessionService) Get

func (s *SessionService) Get(ctx context.Context, id string) (*Session, error)

Get retrieves a session by ID. Returns ErrSessionNotFound if the session doesn't exist.

func (*SessionService) Refresh

func (s *SessionService) Refresh(ctx context.Context, id string) error

Refresh extends session expiration and updates last access time.

type SessionStore

type SessionStore interface {
	// Create stores a new session.
	Create(ctx context.Context, session *Session) error

	// Get retrieves a session by ID.
	// Returns ErrSessionNotFound if session doesn't exist or is expired.
	Get(ctx context.Context, id string) (*Session, error)

	// Update saves changes to an existing session.
	Update(ctx context.Context, session *Session) error

	// Delete removes a session.
	Delete(ctx context.Context, id string) error
}

SessionStore provides session persistence. This interface is defined in the domain to avoid circular imports. Implementations: Redis (prod), in-memory (test).

Jump to

Keyboard shortcuts

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