session

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package session provides signed-ID, server-side HTTP sessions. Session data is never placed in the cookie, and applications can replace the backing Store.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("session not found")
	ErrExists   = errors.New("session already exists")
)

Functions

This section is empty.

Types

type Cookie struct {
	Name                  string
	Path                  string
	Domain                string
	UnsafeAllowHTTP       bool
	UnsafeAllowJavaScript bool
	SameSite              http.SameSite
}

type Manager

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

func NewManager

func NewManager(store Store, secret []byte, cookie Cookie, lifetime time.Duration) (*Manager, error)

NewManager retains the original sliding-lifetime behavior. Applications that need a hard session deadline should use NewManagerWithPolicy.

func NewManagerWithPolicy added in v0.8.0

func NewManagerWithPolicy(store Store, secret []byte, cookie Cookie, policy Policy) (*Manager, error)

NewManagerWithPolicy constructs a manager with explicit idle and absolute lifetimes. Session issuance is persisted in the server-side payload; changing or rotating the signed cookie cannot extend the absolute deadline.

func (*Manager) Destroy

func (manager *Manager) Destroy(ctx context.Context, response http.ResponseWriter, session *Session) error

func (*Manager) Invalidate added in v0.8.0

func (manager *Manager) Invalidate(ctx context.Context, response http.ResponseWriter, session *Session) error

Invalidate removes a server-side session without writing a cookie. It is intended for stale authenticated requests: a late deletion cookie from one response must not overwrite a newly rotated cookie from a concurrent winning response in the same browser. Explicit logout should continue to use Destroy.

func (*Manager) Load

func (manager *Manager) Load(ctx context.Context, request *http.Request) (*Session, error)

func (*Manager) Regenerate

func (manager *Manager) Regenerate(_ context.Context, session *Session) error

Regenerate stages a new ID while retaining values. Save atomically replaces a persisted old ID and sends the new cookie, typically after authentication. The context parameter is retained for API compatibility; persistence happens only in Save so a failed replacement cannot destroy the current session.

func (*Manager) RegenerateOrCreate added in v0.8.0

func (manager *Manager) RegenerateOrCreate(_ context.Context, session *Session) error

RegenerateOrCreate stages a new ID whose save may create the new row if a concurrent request removed the old one. Use this only after an independent, durable authorization change has already succeeded (for example a password compare-and-swap); ordinary rotations must use Regenerate and fail closed.

func (*Manager) Save

func (manager *Manager) Save(ctx context.Context, response http.ResponseWriter, session *Session) error

Save persists changes and refreshes the cookie. Call it before writing a response; writers exposing Written report attempts to save too late.

type MemoryStore

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

MemoryStore is an in-process store intended for development and tests. Entries are removed on expired reads; restarting the process loses sessions.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

func (*MemoryStore) Create

func (store *MemoryStore) Create(_ context.Context, id string, value []byte, expiresAt time.Time) error

func (*MemoryStore) Delete

func (store *MemoryStore) Delete(_ context.Context, id string) error

func (*MemoryStore) Get

func (store *MemoryStore) Get(_ context.Context, id string) ([]byte, error)

func (*MemoryStore) Rotate

func (store *MemoryStore) Rotate(_ context.Context, oldID, newID string, value []byte, expiresAt time.Time) error

func (*MemoryStore) Update

func (store *MemoryStore) Update(_ context.Context, id string, value []byte, expiresAt time.Time) error

type Policy added in v0.8.0

type Policy struct {
	IdleLifetime     time.Duration
	AbsoluteLifetime time.Duration
}

Policy controls how long a session may remain idle and how long it may exist in total. AbsoluteLifetime zero disables the absolute deadline. When enabled, it must be at least IdleLifetime so the two expiration rules remain legible.

type Session

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

Session holds one request's mutable state and is not safe for concurrent use. Obtain sessions through Manager.Load and persist changes with Manager.Save.

func (*Session) Flash

func (session *Session) Flash(key string, value any) error

func (*Session) Forget

func (session *Session) Forget(key string)

func (*Session) Get

func (session *Session) Get(key string, target any) (bool, error)

func (*Session) ID

func (session *Session) ID() string

func (*Session) Put

func (session *Session) Put(key string, value any) error

type Store

type Store interface {
	Get(context.Context, string) ([]byte, error)
	Create(context.Context, string, []byte, time.Time) error
	Update(context.Context, string, []byte, time.Time) error
	Rotate(context.Context, string, string, []byte, time.Time) error
	Delete(context.Context, string) error
}

Store persists sessions until their expiry. Create must return ErrExists when an ID is already present. Get and Update must return ErrNotFound for missing or expired IDs. Rotate must atomically replace oldID with newID, returning ErrNotFound when oldID is no longer current and ErrExists when newID already exists. Implementations must be safe for concurrent use.

Jump to

Keyboard shortcuts

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