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
- Variables
- func Authenticate(r *http.Request, sessSvc *Service, keySvc *auth.Service) (context.Context, error)
- func HashID(raw string) string
- func NameActor(ctx context.Context, s Session)
- func RequireCaller(sessSvc *Service, keySvc *auth.Service) func(http.Handler) http.Handler
- type CallerRequired
- type CookieConfig
- type IssueInput
- type Service
- func (s *Service) End(ctx context.Context, rawID string, then func(tx *sql.Tx, sess Session) error) (Session, bool, error)
- func (s *Service) EndAllForUserTx(ctx context.Context, tx *sql.Tx, userID string) error
- func (s *Service) EndForMemberTx(ctx context.Context, tx *sql.Tx, userID, tenantID string) error
- func (s *Service) EndTx(ctx context.Context, tx *sql.Tx, rawID string) (bool, error)
- func (s *Service) IssueTx(ctx context.Context, tx *sql.Tx, in IssueInput) (rawID string, sess Session, err error)
- func (s *Service) Resolve(ctx context.Context, rawID string) (Session, error)
- func (s *Service) SetLivemode(ctx context.Context, rawID string, livemode bool, ...) (Session, bool, error)
- type Session
- type Store
Constants ¶
const CookieName = "hardcount_session"
CookieName is the cookie that carries the raw session id.
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 ¶
var ErrNoCredentials = errors.New("no credentials")
ErrNoCredentials means the request carries neither a session cookie that resolves nor an API key.
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
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 NameActor ¶ added in v0.10.0
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
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 ¶
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 ¶
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 ¶
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
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
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
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 ¶
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 ¶
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).