session

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 11 Imported by: ? 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 a cookie session manager over alexedwards/scs/v2: middleware that loads and saves the session, login with token rotation, logout, ending every session of one user, and an authn.Authenticator that reads the caller from the loaded session.

Storage is the product's: it implements Store, which is scs's store plus a per-user index of session tokens. Verifying credentials, and deciding what the caller may do, happen elsewhere; this package only records who logged in and answers who a request's session belongs to.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

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

Manager is the session layer. Wrap the routes that use sessions with Middleware; Login, Logout and Authenticate act on the session it loaded.

func New

func New(store Store, opts Options) *Manager

New returns a Manager that keeps sessions in store.

func (*Manager) Authenticate

func (m *Manager) Authenticate(r *http.Request) (*principal.Principal, bool, error)

Authenticate implements authn.Authenticator over the session Middleware loaded for r, which must run first. A request with no session, or one that expired or never existed, is not claimed: it continues as anonymous, as a missing cookie would.

func (*Manager) DestroyUser

func (m *Manager) DestroyUser(ctx context.Context, userID string) error

DestroyUser ends every session the user holds, wherever the store is shared. A session carries the roles it logged in with, so a role change or a disable has to end it rather than wait out its lifetime.

func (*Manager) Login

func (m *Manager) Login(ctx context.Context, userID, username string, roles ...string) error

Login records userID, username and roles in the current session, rotating its token to prevent session fixation. Call it after the credentials are verified.

func (*Manager) LoginOIDC

func (m *Manager) LoginOIDC(ctx context.Context, userID, username, oidcSubject, idpSessionID string, groups []string, roles ...string) error

LoginOIDC is Login for an identity-provider login. It also records the IdP subject ("issuer|sub") and the IdP session id (the id_token sid claim), the keys a back-channel logout needs to find the sessions minted from one IdP session, and the groups the IdP asserted, which the session's principal carries for the rest of its life.

func (*Manager) Logout

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

Logout destroys the current session.

func (*Manager) Middleware

func (m *Manager) Middleware(h http.Handler) http.Handler

Middleware wraps h with scs's LoadAndSave: it loads the session named by the cookie and persists changes with the response.

type Options

type Options struct {
	// CookieName defaults to "session".
	CookieName string
	// Secure marks the cookie HTTPS-only; set it everywhere but local development.
	Secure bool
	// SameSite defaults to Strict. A login or consent flow that returns through the identity provider needs Lax, or the browser withholds the cookie on the final redirect.
	SameSite http.SameSite
	// Lifetime is the absolute session lifetime; there is no idle timeout. Defaults to 24h.
	Lifetime time.Duration
	// Subjects renders the binding subjects of the session's principal, which carries its ID, Issuer, Subject, Groups (as the identity provider asserted them) and Roles. It runs on every Authenticate; an error fails the request as a server failure. Nil leaves Principal.Subjects empty.
	Subjects func(ctx context.Context, p *principal.Principal) ([]string, error)
}

Options configure a Manager. The cookie is always HttpOnly and scoped to Path=/.

type Store

type Store interface {
	scs.Store
	// IndexUserToken records that token belongs to userID, for ttl.
	IndexUserToken(ctx context.Context, userID, token string, ttl time.Duration) error
	// UserTokens lists the tokens indexed under userID.
	UserTokens(ctx context.Context, userID string) ([]string, error)
	// DeleteUserToken deletes the session token names and its entry in userID's index.
	DeleteUserToken(ctx context.Context, userID, token string) error
}

Store holds session payloads by token (scs.Store: Find, Commit, Delete) and an index of the tokens each user holds, which DestroyUser walks.

Jump to

Keyboard shortcuts

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