sessionconfig

package
v0.5.4 Latest Latest
Warning

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

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

Documentation

Overview

Package sessionconfig holds the configuration bindings of per-browser state.

The structs live here rather than in pw so that both sides of the boundary can name them without importing each other: pw installs the session middleware and popcornweb/plugin/auth supplies the lifetime, and neither may depend on the other. pw re-exports every type below as a true alias, so an application writes pw.SessionConfig and the configuration registry, which is keyed by reflect.Type, resolves the two names to one entry.

Nothing here imports anything of the framework, which is what keeps it usable from either side.

Index

Constants

View Source
const (
	// SessionBackendRDB keeps records in a database through
	// sessionstore/sqlite. It is the backend a deployment that must revoke a
	// session, or that outgrows a cookie, uses.
	SessionBackendRDB = "rdb"
	// SessionBackendCookie keeps records in a sealed browser cookie. It needs
	// no storage at all, and it cannot revoke a record it already wrote.
	SessionBackendCookie = "cookie"
	// SessionBackendDevVolatile discards process-local records on restart.
	SessionBackendDevVolatile = "dev-volatile"
	// SessionBackendDevPersist keeps development records in a sealed cookie.
	SessionBackendDevPersist = "dev-persist"
	// SessionBackendRedis keeps records in Redis or Valkey through
	// sessionstore/redis, where the server owns expiry and no sweep runs.
	SessionBackendRedis = "redis"
	// SessionBackendDynamo keeps records in DynamoDB through
	// sessionstore/dynamo, for a deployment with no relational database at
	// all. TTL on the deployed table removes dead records, and nothing sweeps.
	SessionBackendDynamo = "dynamo"
	// SessionBackendFirestore keeps records in Firestore in Datastore mode
	// through sessionstore/firestore, for the same deployment on Google Cloud.
	// A TTL policy on the deployed kind removes dead records, and nothing
	// sweeps.
	SessionBackendFirestore = "firestore"
)

Session storage backends.

Variables

This section is empty.

Functions

This section is empty.

Types

type SessionConfig

type SessionConfig struct {
	Enabled bool `default:"false"`
	// Backend selects the storage plugin or development intent: rdb, cookie,
	// dev-volatile, dev-persist, redis, dynamo, or firestore. General server
	// backends reach the binary through their own blank imports; cookie and both
	// development intent modes are built in. It names which server backend a
	// server-placed slot uses, never whether a slot is server-placed, which
	// RegisterSessionStore states instead.
	// The enum is what makes the per-backend sections below checkable: each of
	// them names a value of this key, and a mistyped one would hide that
	// section from the startup summary silently and forever. Generation rejects
	// a condition naming a value that is not listed here.
	Backend string `` /* 199-byte string literal not displayed */
	// Retention bounds how long the store may hold one record.
	//
	// It is not the session lifetime, which [auth] declares: an expiry states
	// how long a proof of identity stays good, and this states how long bytes
	// may sit in a table. They bound different things, so the effective record
	// lifetime is whichever is shorter.
	//
	// A server backend needs it whatever authentication says. A record with no
	// deadline is one the expiry sweep has no cutoff for, and the sweep is what
	// keeps a table bounded when sessions are abandoned rather than ended.
	Retention time.Duration `` /* 126-byte string literal not displayed */
	// Each store section below names the backend it belongs to, so a summary
	// reports the one in force rather than all seven. The enabled switch is not
	// repeated: Backend answers to it, and a condition on Backend inherits that
	// gate transitively.
	//
	// Cookie is the token cookie every backend travels under, and Keyring signs
	// and seals whatever the browser carries, which is every backend that keeps
	// state on the server. Keyring states that as a not-equal so it does not
	// have to be revisited each time a backend ships; an equal list would hide a
	// secret that is in force the first time one was forgotten.
	Cookie      SessionCookieConfig      `dependon:".enabled"`
	RDB         SessionRDBConfig         `dependon:".backend=rdb"`
	Redis       SessionRedisConfig       `dependon:".backend=redis" summary:"omit"`
	CookieStore SessionCookieStoreConfig `dependon:".backend=cookie"`
	Keyring     SessionKeyringConfig     `dependon:".backend!=cookie"`
	Dynamo      SessionDynamoConfig      `dependon:".backend=dynamo"`
	Firestore   SessionFirestoreConfig   `dependon:".backend=firestore"`
}

SessionConfig selects where per-browser state lives and how its token cookie travels. It declares no duration at all: an expiry states how long a proof of identity stays good, so every session lifetime is declared under [auth].

The session token is opaque in every backend; only SessionBackendCookie keeps the record on the client for the whole session. Persistent server backends seal Private state into the browser while anonymous; the development memory backend keeps it server-side from the first write.

type SessionCookieConfig

type SessionCookieConfig struct {
	Name     string `default:"pw_session"`
	Path     string `default:"/"`
	Domain   string
	Secure   bool   `default:"true" help:"disable only for loopback development"`
	HTTPOnly bool   `default:"true"`
	SameSite string `default:"lax"`
}

SessionCookieConfig is the browser cookie policy of the session middleware.

type SessionCookieStoreConfig

type SessionCookieStoreConfig struct {
	// Name holds the sealed record beside the token cookie.
	Name string `default:"pw_session_data" help:"cookie holding the sealed record"`
}

SessionCookieStoreConfig configures the client-side session backend. The record cookie follows the [session.cookie] policy, so both cookies of a session expire and travel under the same rules; only the name is separate.

type SessionDynamoConfig

type SessionDynamoConfig struct {
	// Table is the declared table name, which rule:dynamodb-table-naming maps
	// onto the deployed one.
	Table string `default:"popcornweb_session" help:"declared session table name"`
	// ConsistentRead makes the first read strongly consistent and removes the
	// retry a miss otherwise pays. It costs twice the read capacity.
	ConsistentRead bool `default:"false" help:"read sessions with strong consistency"`
}

SessionDynamoConfig configures the DynamoDB session store. It carries no endpoint and no credential: middleware.dynamo already opens the client this backend borrows.

type SessionFirestoreConfig

type SessionFirestoreConfig struct {
	// Kind is the entity kind. A kind belongs to the type rather than to the
	// deployment, so nothing maps it; this exists for a project that has to
	// share a database with something that already owns the name.
	Kind string `default:"popcornweb_session" help:"session entity kind"`
}

SessionFirestoreConfig configures the Firestore session store. It carries no endpoint and no credential: middleware.firestore already opens the client this backend borrows.

It has no read-consistency key. Datastore mode reads are strongly consistent, so there is no false miss to retry around and nothing to weigh.

type SessionKeyringConfig

type SessionKeyringConfig struct {
	// Secret is 32 or more random bytes in base64, generated with
	// `openssl rand -base64 32`. Keep it out of the file itself outside
	// development: write "${SESSION_KEYRING_SECRET}" or set the environment
	// variable. An explicit development cookie backend gets a generated value;
	// a dev-volatile registry with no ReadOnly slots needs none. `pw doctor`
	// reports a literal in any other environment as an error.
	Secret string `secret:"mask" env:"SESSION_KEYRING_SECRET" help:"base64 secret signing and sealing everything the browser carries"`
	// PreviousSecrets keep values written before a rotation readable. They
	// never write.
	PreviousSecrets []string `secret:"mask" help:"retired secrets kept readable during a rotation"`
}

SessionKeyringConfig holds the secret that protects everything the browser carries.

It is not the cookie backend's setting, which is why it is not under [session.cookie_store]: one secret serves both protections a slot can carry, because a session.ReadOnly slot signs and a session.Private slot seals, and session.Keyring derives a purpose-separated subkey per mode from it. A deployment on rdb, redis, or dynamo needs it exactly as much as one on cookie, because the anonymous phase of a private slot is normally sealed. The dev-volatile mode is the exception for Private and ServerOnly slots.

It is required whenever a declared slot is placed in a protected cookie. session.Shared protects nothing; session.ServerOnly and session.RequestScope never use such a cookie.

type SessionLifetimeConfig

type SessionLifetimeConfig struct {
	// TTL is the absolute session lifetime.
	TTL time.Duration `default:"24h" help:"absolute session lifetime"`
	// IdleTimeout expires a session that stops being used. Zero disables it.
	IdleTimeout time.Duration `default:"0s" help:"inactivity expiry; zero disables it"`
	// RenewalInterval bounds how often an active request renews idle expiry.
	RenewalInterval time.Duration `default:"0s" help:"minimum interval between idle expiry renewals"`
}

SessionLifetimeConfig bounds how long a session stays valid.

These durations used to live under [session]. They belong here because an absolute expiry, an idle expiry, and a re-proof window are three answers to one question, how long a proof of identity stays good, and splitting them across two bindings split one policy across two files. A deployment reasons about them together, and a TTL shorter than a guard window is a misconfiguration only this package can detect.

The session package enforces whatever deadline it is handed and forms no opinion about the number, which is what lets one store hold a shopping cart and a login.

type SessionRDBConfig

type SessionRDBConfig struct {
	Source string `default:"middleware" help:"middleware reuses middleware.rdb; dedicated opens rdb.dsn"`
	// Group names the middleware-source connection group holding the session
	// table. Empty resolves to middleware.rdb.write_group.
	Group string `help:"connection group holding the session table"`
	DSN   string `secret:"mask" help:"dedicated session database DSN"`
	Table string `default:"popcornweb_session"`
}

SessionRDBConfig configures the database-backed session store. The middleware source reuses the pool owned by middleware.rdb; the dedicated source opens its own pool from DSN.

type SessionRedisConfig

type SessionRedisConfig struct {
	// DSN is a redis:// or rediss:// URL. Keep credentials out of the file
	// itself with a ${NAME} reference or SESSION_REDIS_DSN.
	DSN string `secret:"mask" env:"SESSION_REDIS_DSN" help:"redis:// or rediss:// session server"`
	// KeyPrefix isolates session keys from every other user of the server.
	KeyPrefix string `default:"pw:session:" help:"key space owned by the session store"`
	// ConnectTimeout bounds the startup ping and the per-command deadlines.
	ConnectTimeout time.Duration `default:"5s" help:"startup ping and per-command deadline"`
}

SessionRedisConfig configures the Redis-compatible session store. The server owns record expiry, so nothing here schedules a sweep.

Jump to

Keyboard shortcuts

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