Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package session provides server-side session management for OniWorks. The default store is Oni Memory (in-process, fast). A database store is also available.

Index

Constants

This section is empty.

Variables

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

ErrNotFound is returned by Store.Get when no session exists for the given ID (or it has expired). It lets the Manager distinguish "no session" — which should transparently create a fresh one — from a real store failure, which must be surfaced instead of silently logging users out.

Functions

This section is empty.

Types

type Config

type Config struct {
	CookieName string
	Domain     string
	Path       string
	Secure     bool
	HTTPOnly   bool
	SameSite   http.SameSite
	TTL        time.Duration
}

Config holds session configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns production-safe session defaults.

type Manager

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

Manager manages session lifecycle.

func NewManager

func NewManager(store Store, cfg ...Config) *Manager

NewManager creates a session Manager.

func (*Manager) Destroy

func (m *Manager) Destroy(ctx context.Context, w http.ResponseWriter, sess *Session) error

Destroy deletes the session and clears the cookie.

func (*Manager) Save

func (m *Manager) Save(ctx context.Context, w http.ResponseWriter, sess *Session) error

Save persists the session data to the store. Call this at the end of each request.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, r *http.Request, w http.ResponseWriter) (*Session, error)

Start reads or creates the session for the current request. A missing or expired session transparently creates a fresh one; a store failure (e.g. a transient backend outage) is returned as an error so callers don't silently log users out.

type Session

type Session struct {
	ID string
	// contains filtered or unexported fields
}

Session represents a single user session.

func (*Session) Delete

func (s *Session) Delete(key string)

func (*Session) Flash

func (s *Session) Flash(key string, value any)

func (*Session) Get

func (s *Session) Get(key string) (any, bool)

func (*Session) GetFlash

func (s *Session) GetFlash(key string) (any, bool)

func (*Session) Has

func (s *Session) Has(key string) bool

func (*Session) IsNew

func (s *Session) IsNew() bool

func (*Session) Regenerate

func (s *Session) Regenerate(ctx context.Context) error

Regenerate rotates the session ID while preserving the session data. Call it on any privilege change — especially after login — to prevent session fixation, where an attacker fixes a victim's pre-auth session ID and reuses it once authenticated. The new ID is written to the cookie on the next Save.

func (*Session) Set

func (s *Session) Set(key string, value any)

type Store

type Store interface {
	// Get returns the session data for id. When no session exists (or it has
	// expired) it should return ErrNotFound; returning (nil, nil) is also
	// accepted for backward compatibility. Any other error is treated as a
	// store failure and propagated to the caller.
	Get(ctx context.Context, id string) (map[string]any, error)
	Set(ctx context.Context, id string, data map[string]any, ttl time.Duration) error
	Delete(ctx context.Context, id string) error
	Regenerate(ctx context.Context, oldID string) (string, error)
}

Store is the interface all session stores must implement.

Directories

Path Synopsis
Package drivers provides session store implementations.
Package drivers provides session store implementations.

Jump to

Keyboard shortcuts

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