session

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package session implements Session\Manager and the Session\Storage\* family: an HTTP-only cookie carrying an opaque, randomly generated session ID, and the storage the data behind that ID lives in. Memory storage lasts as long as the process; disk storage is a file per session under a directory the host chooses, and prunes by modification time.

A manager wraps whichever storage it is given for tracing, so a request's trace shows which backend it paid for and whether the session was there. The session ID is never recorded: it is the credential in the cookie.

It is wired in by importing it; stdlib/imports.go does that.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(rt *runner.Runtime)

Register installs the session storage and manager classes.

Types

type Manager

type Manager struct {
	// SessionCookieName is the mutable name used to read and write the session
	// cookie. PHP may change it with `$session->SessionCookieName = "sid"`.
	SessionCookieName string
	// contains filtered or unexported fields
}

Manager associates an HTTP-only cookie with data in Storage. The cookie contains only an opaque, randomly generated session ID.

func NewManager

func NewManager(storage Storage) (*Manager, error)

NewManager creates a manager backed by storage.

func (*Manager) Get

func (s *Manager) Get(ctx context.Context) (string, error)

Get returns the user ID stored for the current session cookie.

func (*Manager) Start

func (s *Manager) Start(ctx context.Context, userID any) error

Start creates a new session, stores userID, and stages its HTTP-only cookie.

func (*Manager) Valid

func (s *Manager) Valid(ctx context.Context) (bool, error)

Valid reports whether the request has a well-formed cookie whose session is present in storage. Missing and malformed cookies are invalid, not errors.

type Storage

type Storage interface {
	Load(ctx context.Context, id string) ([]byte, error)
	Save(ctx context.Context, id string, data []byte) error
	Delete(ctx context.Context, id string) error
	Prune(ctx context.Context, maxAge time.Duration) error
}

Storage persists opaque session data by ID.

type StorageDisk

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

StorageDisk stores sessions in a local folder.

func NewStorageDisk

func NewStorageDisk(storagePaths ...string) (*StorageDisk, error)

NewStorageDisk creates the storage folder and verifies that it is writable. With no path, it uses the operating system's temporary directory.

func (*StorageDisk) Delete

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

Delete removes a session from disk.

func (*StorageDisk) Load

func (s *StorageDisk) Load(ctx context.Context, id string) ([]byte, error)

Load retrieves a session from disk.

func (*StorageDisk) Prune

func (s *StorageDisk) Prune(ctx context.Context, maxAge time.Duration) error

Prune removes sessions that have not been saved within maxAge.

func (*StorageDisk) Save

func (s *StorageDisk) Save(ctx context.Context, id string, data []byte) error

Save atomically writes a session to disk.

type StorageMemory

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

StorageMemory stores sessions in memory. Its contents are not durable.

func NewStorageMemory

func NewStorageMemory() *StorageMemory

NewStorageMemory creates empty in-memory session storage.

func (*StorageMemory) Delete

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

Delete removes a session from memory.

func (*StorageMemory) Load

func (s *StorageMemory) Load(ctx context.Context, id string) ([]byte, error)

Load retrieves a copy of a session's data.

func (*StorageMemory) Prune

func (s *StorageMemory) Prune(ctx context.Context, maxAge time.Duration) error

Prune removes sessions that have not been saved within maxAge.

func (*StorageMemory) Save

func (s *StorageMemory) Save(ctx context.Context, id string, data []byte) error

Save stores a copy of the session data.

Jump to

Keyboard shortcuts

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