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 ¶
- type Manager
- func (m *Manager) Authenticate(r *http.Request) (*principal.Principal, bool, error)
- func (m *Manager) DestroyUser(ctx context.Context, userID string) error
- func (m *Manager) Login(ctx context.Context, userID, username string, roles ...string) error
- func (m *Manager) LoginOIDC(ctx context.Context, userID, username, oidcSubject, idpSessionID string, ...) error
- func (m *Manager) Logout(ctx context.Context) error
- func (m *Manager) Middleware(h http.Handler) http.Handler
- type Options
- type Store
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 (*Manager) Authenticate ¶
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 ¶
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 ¶
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.
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.