session

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package session is Cairn's minimal server-side web session store (SPEC-0001, ADR-0004). A browser authenticates once through the login surface and is issued an opaque, high-entropy session token carried in a secure httpOnly cookie; this package holds the server-side record that token resolves to — the actor identity and the session's bound CSRF token — so nothing sensitive lives in the cookie itself. It is the seam the future OAuth 2.1 authorization server (ADR-0004, #22) reuses for its own token-backed sessions: a different credential adapter, the same server-side session record.

Tokens are stored hashed (SHA-256), never in the clear, so a database leak does not yield usable session cookies (the same treatment a password hash receives). Lookups hash the presented cookie and compare, and expired records resolve as absent.

Governing: SPEC-0001 (Authentication & Authorization), ADR-0004 (auth seam).

Index

Constants

This section is empty.

Variables

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

ErrNotFound reports that a session token does not resolve to a live session — it is unknown, expired, or already revoked. The three are deliberately indistinguishable so a stale cookie leaks no signal (the same uniform-absence discipline as an unknown artifact id, ADR-0007).

Functions

func NewToken

func NewToken() (string, error)

NewToken mints a standalone high-entropy, URL-safe token. It backs the login form's pre-session CSRF token — the same entropy source as a session token, so the no-JS login double-submit is as unguessable as a live session's.

Types

type MemoryStore

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

MemoryStore is an in-process session Store for tests and single-node runs without a database. It keeps the same hash-at-rest and expiry semantics as the Postgres store so a test exercising the memory store proves the same behavior.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore builds an empty in-memory session store.

func (*MemoryStore) Create

func (s *MemoryStore) Create(_ context.Context, issuer, subject, actorID, userID, operatorGroup string, ttl time.Duration) (*Session, error)

Create mints and stores a session, returning it with its raw secrets.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(_ context.Context, token string) error

Delete revokes a session by its raw token; deleting an absent token is a no-op.

func (*MemoryStore) Get

func (s *MemoryStore) Get(_ context.Context, token string) (*Session, error)

Get resolves a raw token to a live session, treating an expired record as absent (and evicting it).

type PostgresStore

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

PostgresStore is the production session store: session records live in the same Postgres the artifact core uses, so the single binary carries their schema and a horizontally-scaled deployment shares one session table (ADR-0012). Only the token hash is persisted (see hashToken).

func NewPostgresStore

func NewPostgresStore(pool *pgxpool.Pool) *PostgresStore

NewPostgresStore builds a Postgres-backed session Store over the shared pool.

func (*PostgresStore) Create

func (s *PostgresStore) Create(ctx context.Context, issuer, subject, actorID, userID, operatorGroup string, ttl time.Duration) (*Session, error)

Create mints and persists a session, returning it with its raw secrets. The row stores only the token's hash; the raw token is the cookie value the caller sets and never sees again from the store.

The row records only the user: the actor a session acts as is rendered from that user on every Get (SPEC-0023 REQ "Migration to Explicit Ownership"), so actorID only fills the returned value.

func (*PostgresStore) Delete

func (s *PostgresStore) Delete(ctx context.Context, token string) error

Delete revokes a session by its raw token. Deleting an absent token is a no-op.

func (*PostgresStore) Get

func (s *PostgresStore) Get(ctx context.Context, token string) (*Session, error)

Get resolves a raw token to its live session. An expired row, and the row of a suspended user, are filtered in the query so they resolve as ErrNotFound, indistinguishable from an unknown or revoked token. Suspension deletes a user's sessions as well; the filter is what makes a session that raced that delete fail too (SPEC-0023 "Suspending a user offboards them").

type Session

type Session struct {
	Token   string
	ActorID string
	// UserID is the users row the session acts for (SPEC-0023 REQ "Users and
	// Identities"); ActorID is that user as rendered on the wire. The Postgres
	// store requires a user and renders ActorID from it on every Get.
	UserID  string
	Issuer  string
	Subject string
	// OperatorGroup is the configured CAIRN_OPERATOR_GROUP the OIDC groups
	// claim carried when this session was minted, or "" (SPEC-0023 REQ
	// "Operator and User Profiles"). The claim exists only at sign-in, so the
	// session records the one fact the operator check needs from it.
	OperatorGroup string
	CSRFToken     string
	CreatedAt     time.Time
	ExpiresAt     time.Time
}

Session is one authenticated browser session. Token is the raw opaque value placed in the cookie and returned only at creation (the store persists only its hash); CSRFToken is the double-submit secret bound to this session and echoed in the readable cairn_csrf cookie so a same-origin script can prove the request came from Cairn's own origin. Issuer/Subject record the auth provenance (SPEC-0012): which provider established the session and the subject that provider asserted. Both are empty for the dev-password login.

type Store

type Store interface {
	// Create mints a new session living for ttl, returning it with its raw
	// Token and CSRFToken populated (both are shown to the caller exactly once,
	// then only their derivations are retained). issuer/subject record the
	// session's auth provenance (SPEC-0012): the provider that established it
	// and the subject that provider asserted — empty for the dev-password
	// login, the OIDC issuer and ID-token subject for Pocket ID, and the
	// GitHub origin and numeric account id for GitHub. userID is the users row
	// the session acts for and actorID its wire rendering. operatorGroup is the
	// configured operator group the sign-in's groups claim carried, or "".
	Create(ctx context.Context, issuer, subject, actorID, userID, operatorGroup string, ttl time.Duration) (*Session, error)
	// Get resolves a raw session token to its live session, or ErrNotFound when
	// the token is unknown, expired, or revoked, or its user is suspended.
	Get(ctx context.Context, token string) (*Session, error)
	// Delete revokes the session addressed by its raw token. Revoking an absent
	// session is a no-op (logout is idempotent).
	Delete(ctx context.Context, token string) error
}

Store is the session persistence seam. The Postgres implementation backs the running server; the in-memory implementation backs unit tests. OAuth (#22) adds no new method — it mints sessions through Create like the dev login does.

Jump to

Keyboard shortcuts

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