session

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package session holds secure-mode sessions: the server side of a signed-in browser, its identity refresh, and the /auth/me and /auth/logout routes.

Index

Constants

View Source
const (
	DefaultIdle     = 30 * time.Minute
	DefaultAbsolute = 8 * time.Hour
	DefaultMax      = 1000
)

Defaults for Options.

View Source
const CookieName = "snellius_session"

CookieName is the session cookie.

Variables

View Source
var ErrStale = errors.New("session identity awaits its refresh")

ErrStale is the commit check's answer while the session's identity awaits its refresh.

Functions

func IdentityFrom

func IdentityFrom(ctx context.Context) (oidc.Identity, bool)

IdentityFrom returns the Kubernetes identity of a request that passed Middleware.

Types

type Info

type Info struct {
	Identity oidc.Identity
	// Issuer and Subject name the person; AuditID is the session's opaque audit ID.
	Issuer, Subject, AuditID string
	// contains filtered or unexported fields
}

Info is the signed-in identity a request carries.

func FromContext

func FromContext(ctx context.Context) (Info, bool)

FromContext returns the session identity of a request that passed Middleware.

type Options

type Options struct {
	Idle, Absolute time.Duration
	Max            int
	// Clock is nil for the real clock.
	Clock clock.WithTicker
	// Refresher renews identities; nil ends a session when its identity goes stale.
	Refresher Refresher
	Audit     oidc.Auditor
	// Metrics receives the snellius_sessions_active gauge; nil means a private registry.
	Metrics *obs.Registry
	// CookieSecure marks the session cookie Secure.
	CookieSecure bool
	// ClientID and RedirectURL build the logout origin check and the RP-initiated logout URL.
	ClientID, RedirectURL string
	// EndSession returns the issuer's end_session_endpoint, or "" when it has none.
	EndSession func() string
	// Logger receives refresh failure steps, never a token; nil means slog.Default().
	Logger *slog.Logger
}

Options configure a Store. Zero durations and bound take the defaults.

type Refresher

type Refresher interface {
	Refresh(ctx context.Context, refreshToken string) (oidc.Result, error)
}

Refresher renews an identity with a refresh token; *oidc.Flow is one.

type Store

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

Store holds sessions in memory, keyed by a hash of the cookie value.

func New

func New(o Options) *Store

New builds a Store. Start its sweeper with Run.

func (*Store) Complete

func (s *Store) Complete(w http.ResponseWriter, r *http.Request, res oidc.Result)

Complete starts a session for a sign-in, replacing the browser's previous one or its end notice, then redirects to the return path. It implements oidc.Completer.

func (*Store) Logout

func (s *Store) Logout(w http.ResponseWriter, r *http.Request)

Logout serves POST /auth/logout. The Origin header must be the redirect URL's origin. It deletes the session or its end notice and answers the next URL: the issuer's end-session endpoint without an ID token hint, else "/".

func (*Store) Me

func (s *Store) Me(w http.ResponseWriter, r *http.Request)

Me serves GET /auth/me: the session's username and groups.

func (*Store) Middleware

func (s *Store) Middleware(next http.Handler) http.Handler

Middleware lets an /api/ request through only with a fresh session, carrying its identity in the context; others get 401. Other paths pass unchanged.

func (*Store) Run

func (s *Store) Run(ctx context.Context)

Run sweeps expired sessions every minute until ctx is done.

func (*Store) StillSignedIn

func (s *Store) StillSignedIn(w http.ResponseWriter, r *http.Request) (*http.Request, error)

StillSignedIn is the API's commit check for r, a request that passed Middleware. It answers r while the session holds r's identity and that identity is fresh, a request carrying the current identity when a refresh replaced it, and ErrStale while the identity awaits a refresh. When the session ended it writes the 401 a new request with the cookie would get and answers nil. It never refreshes or waits.

Jump to

Keyboard shortcuts

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