session

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 16 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 implements dashboard sessions, issued by the email and password login (ADR-011). The browser holds an httpOnly cookie with a random id; the database stores only its sha256 (HashID), so a copy of the table cannot be replayed. A session is live while it is not revoked and not expired: Resolve, End, EndTx and SetLivemode act only on a live one.

Index

Constants

View Source
const CookieName = "hardcount_session"

CookieName is the cookie that carries the raw session id.

View Source
const DefaultTTL = 7 * 24 * time.Hour

DefaultTTL is a session's lifetime: long enough that an operator does not sign in every day, short enough that a stolen cookie stops working on its own.

Variables

View Source
var ErrNoCredentials = errors.New("no credentials")

ErrNoCredentials means the request carries neither a session cookie that resolves nor an API key.

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

ErrNotFound signals that a session id resolves to no row, or to a row that has been revoked or expired.

Functions

func Authenticate added in v0.10.0

func Authenticate(r *http.Request, sessSvc *Service, keySvc *auth.Service) (context.Context, error)

Authenticate resolves r's credentials to a context that carries the caller (auth.CallerOf) and its test or live mode. A session cookie that resolves wins over any key; a cookie that does not resolve falls through to the Bearer key. It returns ErrNoCredentials when there is neither, ValidateKey's error for a bad key, and an unavailableError when a store fails.

func HashID

func HashID(raw string) string

HashID returns the storage form of a raw session id.

func NameActor added in v0.10.0

func NameActor(ctx context.Context, s Session)

NameActor names s's user on the request's record, for the access log. It is for a handler outside authentication that reads the session cookie itself (route_table.go's outsideAuth says which); RequireCaller names the caller of every route behind it.

func RequireCaller added in v0.10.0

func RequireCaller(sessSvc *Service, keySvc *auth.Service) func(http.Handler) http.Handler

RequireCaller authenticates each request (Authenticate) and passes it on with the caller and its mode on the context. It answers 401 to a request with no credentials or a key that is not valid, and a retryable 503 when a store failed. The dashboard uses the cookie, and SDKs and curl callers use the API key.

Types

type CallerRequired added in v0.10.0

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

CallerRequired authenticates each request. RequireCaller makes it; its doc has the rule.

func (CallerRequired) ServeHTTP added in v0.10.0

func (c CallerRequired) ServeHTTP(w http.ResponseWriter, r *http.Request)

type CookieConfig

type CookieConfig struct {
	Domain string
	Secure bool
}

CookieConfig holds the session cookie's attributes that differ by install. Every session cookie is also HttpOnly, Path=/ and SameSite=Lax: Lax keeps it on top-level navigation to the dashboard while blocking most cross-site requests.

func DefaultCookieConfig

func DefaultCookieConfig(appEnv, domain string) CookieConfig

DefaultCookieConfig returns the cookie attributes for appEnv, which is config.Config.Env, and domain, which is config.Server.CookieDomain.

Secure is on in staging and production, for every request, so a browser sends the session cookie only over HTTPS, or to localhost (ADR-095 Phase 0(b)). Chrome and Firefox keep a Secure cookie at http://localhost, but Safari does not. Plain HTTP is for local, where Secure is off: make dev, and the Compose quickstart, which sets APP_ENV=local so Safari can sign in at http://localhost.

func (CookieConfig) ClearCookie

func (c CookieConfig) ClearCookie(w http.ResponseWriter)

ClearCookie writes the session cookie with Max-Age=0, which clears it, for sign-out.

func (CookieConfig) SetCookie

func (c CookieConfig) SetCookie(w http.ResponseWriter, raw string, expires time.Time)

SetCookie writes the session cookie on the response. Sign-in (internal/user) and invite accept (internal/dashmembers) call it after IssueTx.

type IssueInput

type IssueInput struct {
	UserID    string
	TenantID  string
	Livemode  bool
	UserAgent string
	IP        string
}

IssueInput is what IssueTx needs to mint a session for a user whose password the caller has verified.

type Service

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

Service issues, resolves and ends sessions, and switches their mode, over a Store.

func NewService

func NewService(store Store) *Service

NewService returns a Service backed by store. Sessions last DefaultTTL.

func (*Service) End added in v0.10.0

func (s *Service) End(ctx context.Context, rawID string, then func(tx *sql.Tx, sess Session) error) (Session, bool, error)

End ends the live session whose raw cookie value is rawID and calls then on the same bypass transaction, so the sign-out and then's writes, such as its audit row, commit or roll back together. then must not commit or roll back tx; its error rolls the sign-out back and is returned. End returns the ended session and whether a live one was found; with none, nothing changes and then is not called.

func (*Service) EndAllForUserTx added in v0.10.0

func (s *Service) EndAllForUserTx(ctx context.Context, tx *sql.Tx, userID string) error

EndAllForUserTx ends every session of a user, in every workspace and mode, on the caller's transaction. It does not commit. tx must be a bypass transaction: a tenant transaction's row-level security hides the user's sessions in other workspaces and in the other mode.

func (*Service) EndForMemberTx added in v0.10.0

func (s *Service) EndForMemberTx(ctx context.Context, tx *sql.Tx, userID, tenantID string) error

EndForMemberTx ends every session of a user in one workspace, in both modes, on the caller's transaction; other workspaces are not touched. It does not commit. tx must be a bypass transaction: a tenant transaction's row-level security hides the user's sessions in the other mode.

func (*Service) EndTx added in v0.10.0

func (s *Service) EndTx(ctx context.Context, tx *sql.Tx, rawID string) (bool, error)

EndTx ends the live session whose raw cookie value is rawID, whoever it belongs to, on the caller's transaction, and reports whether it ended one. It does not commit. tx must be a bypass transaction, to reach the session whoever it belongs to, in either mode.

func (*Service) IssueTx added in v0.10.0

func (s *Service) IssueTx(ctx context.Context, tx *sql.Tx, in IssueInput) (rawID string, sess Session, err error)

IssueTx creates a session row on the caller's transaction, so it commits or rolls back with the caller's change, and returns the raw cookie value: the only time it is shown. It does not commit. tx must be a bypass transaction: a session row is written before any tenant context exists.

func (*Service) Resolve

func (s *Service) Resolve(ctx context.Context, rawID string) (Session, error)

Resolve returns the live session for a raw cookie value. A value of the wrong shape (wellFormed), an unknown one, or a revoked or expired session returns ErrNotFound, which Authenticate treats as no cookie, so these cases cannot be told apart and session ids cannot be enumerated.

func (*Service) SetLivemode

func (s *Service) SetLivemode(ctx context.Context, rawID string, livemode bool, then func(tx *sql.Tx, sess Session) error) (Session, bool, error)

SetLivemode switches the live session whose raw cookie value is rawID to livemode and calls then on the same transaction, under End's contract. It returns the session in its new mode and whether a live one was found. A session already in that mode is returned as it is, and then is not called: nothing changed, so there is nothing to record.

type Session

type Session struct {
	IDHash    string
	UserID    string
	TenantID  string
	Livemode  bool
	CreatedAt time.Time
	ExpiresAt time.Time
	RevokedAt *time.Time
	UserAgent string
	IP        string
}

Session is a dashboard_sessions row. It is bound to the user it was issued for, not to a key (ADR-011). Livemode is the mode it acts in, which SetLivemode switches.

type Store

type Store interface {
	InsertTx(ctx context.Context, tx *sql.Tx, s Session) error
	GetByIDHash(ctx context.Context, idHash string, now time.Time) (Session, error)
	EndTx(ctx context.Context, tx *sql.Tx, idHash string) (bool, error)
	EndAllForUserTx(ctx context.Context, tx *sql.Tx, userID string) error
	EndForMemberTx(ctx context.Context, tx *sql.Tx, userID, tenantID string) error
	EndWith(ctx context.Context, idHash string, then func(tx *sql.Tx, sess Session) error) (Session, bool, error)
	SetLivemodeWith(ctx context.Context, idHash string, livemode bool, then func(tx *sql.Tx, sess Session) error) (Session, bool, error)
}

Store reads and writes dashboard_sessions rows. Service is its one caller, and Service's methods state each contract.

func NewPostgresStore

func NewPostgresStore(db *postgres.DB) Store

NewPostgresStore returns the Postgres-backed Store. GetByIDHash, EndWith and SetLivemodeWith open their own bypass transaction; the …Tx methods run on the caller's, which must be one (each Service method says why).

Jump to

Keyboard shortcuts

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