session

package
v0.18.5 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package session implements server-side opaque sessions (plan §5.3): a 256-bit random id handed to the browser, only its SHA-256 hash stored; rotated on login; idle + absolute timeouts that cannot be reopened by a backward wall clock step — even across a process restart (plan §5.9; review #1).

Clock model: within a process, m.now() is monotonic-anchored to boot. Across restarts that anchor resets to the (possibly stepped-back) wall clock, so we additionally clamp to a persisted "high-water mark" of the greatest wall second ever observed. effectiveNow() = max(monotonic-anchored now, high-water) — it never regresses, so an expired session stays expired and GC still purges it after a backward step. Create and Load/GC all use the SAME effective clock.

Index

Constants

This section is empty.

Variables

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

ErrNotFound means no live session matched (missing, expired, or revoked).

Functions

This section is empty.

Types

type Manager

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

Manager owns session lifecycle against the DB.

func New

func New(db *store.DB, idle, absolute time.Duration) *Manager

New returns a session Manager, seeding the clock high-water mark from the DB.

func (*Manager) Create

func (m *Manager) Create(ctx context.Context, username, peerIP, userAgent string) (rawID string, err error)

Create mints a new session for username, returns the raw id for the cookie.

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, rawID string) error

Delete revokes a session by its raw id (logout).

func (*Manager) DeleteAll

func (m *Manager) DeleteAll(ctx context.Context) (int64, error)

DeleteAll revokes EVERY session (used when the auth config changes — password or TOTP — so no session predating the change survives, regardless of which username it was issued under). Returns rows affected.

func (*Manager) DeleteAllForUser

func (m *Manager) DeleteAllForUser(ctx context.Context, username string) (int64, error)

DeleteAllForUser revokes every session for a user (privilege change / forced logout). Returns rows affected.

func (*Manager) GC

func (m *Manager) GC(ctx context.Context) error

GC removes expired sessions; called opportunistically. Uses the same effective clock as Load so a backward step does not strand expired rows.

func (*Manager) Load

func (m *Manager) Load(ctx context.Context, rawID string) (*Session, error)

Load returns the session for a raw cookie id, enforcing idle + absolute timeouts against the non-regressing effective clock, and ADVANCES last_seen — active use keeps the session alive (this runs on every authed request via the session middleware).

func (*Manager) Peek

func (m *Manager) Peek(ctx context.Context, rawID string) (*Session, error)

Peek is Load WITHOUT advancing last_seen — a read-only liveness check enforcing the SAME idle + absolute expiry. It must not keep an idle session alive, so it's used by the dashboard's focus-loss watchdog status probe (an unfocused tab polling "am I still logged in?" must observe the idle-out, not postpone it).

type Session

type Session struct {
	Username   string
	CreatedAt  time.Time
	LastSeenAt time.Time
}

Session is a loaded, still-valid session.

Jump to

Keyboard shortcuts

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