Documentation
¶
Overview ¶
Package session models the authenticated caller (Principal) and the store that keeps browser sessions.
Index ¶
- Constants
- func ClearCookie(w http.ResponseWriter, c CookieOptions)
- func EndUserInto(ctx context.Context, eu EndUser) context.Context
- func HasCookie(h http.Header) bool
- func IsBadCookieError(err error) bool
- func IsNotFoundError(err error) bool
- func NewID() (string, error)
- func PrincipalInto(ctx context.Context, p Principal) context.Context
- func ReadCookie(h http.Header, secret []byte) (string, bool)
- func SIDFrom(ctx context.Context) (string, bool)
- func SIDInto(ctx context.Context, sid string) context.Context
- func Sign(secret []byte, value string) string
- func Verify(secret []byte, raw string) (string, error)
- func WriteCookie(w http.ResponseWriter, c CookieOptions, signed string, expires time.Time)
- type BadCookieError
- type CookieOptions
- type EndUser
- type MemoryStore
- func (s *MemoryStore) Delete(_ context.Context, sid string) error
- func (s *MemoryStore) DeleteByUser(_ context.Context, userID uuid.UUID) error
- func (s *MemoryStore) DeleteByUserExcept(_ context.Context, userID uuid.UUID, keepSID string) error
- func (s *MemoryStore) DeleteExpired(_ context.Context) (int, error)
- func (s *MemoryStore) Load(_ context.Context, sid string) (Principal, bool, error)
- func (s *MemoryStore) Save(_ context.Context, sid string, p Principal, expiresAt time.Time) error
- func (s *MemoryStore) Update(_ context.Context, sid string, p Principal) error
- type NotFoundError
- type PostgresStore
- func (s *PostgresStore) Delete(ctx context.Context, sid string) error
- func (s *PostgresStore) DeleteByUser(ctx context.Context, userID uuid.UUID) error
- func (s *PostgresStore) DeleteByUserExcept(ctx context.Context, userID uuid.UUID, keepSID string) error
- func (s *PostgresStore) DeleteExpired(ctx context.Context) (int, error)
- func (s *PostgresStore) Load(ctx context.Context, sid string) (Principal, bool, error)
- func (s *PostgresStore) Save(ctx context.Context, sid string, p Principal, expiresAt time.Time) error
- func (s *PostgresStore) Update(ctx context.Context, sid string, p Principal) error
- type Principal
- type Scheduler
- type SignedCookie
- type Source
- type Store
Constants ¶
const CookieName = "othra_sid"
CookieName is the browser session cookie; its value is Sign(stateSecret, sid).
Variables ¶
This section is empty.
Functions ¶
func ClearCookie ¶
func ClearCookie(w http.ResponseWriter, c CookieOptions)
ClearCookie tells the browser to drop the session cookie.
func EndUserInto ¶
EndUserInto returns ctx carrying eu.
func IsBadCookieError ¶
IsBadCookieError reports whether err's tree contains a *BadCookieError.
func IsNotFoundError ¶
IsNotFoundError reports whether err's tree contains a *NotFoundError.
func PrincipalInto ¶
PrincipalInto returns ctx carrying p.
func ReadCookie ¶
ReadCookie returns the session id of the first session cookie in h whose signature verifies.
func WriteCookie ¶
func WriteCookie(w http.ResponseWriter, c CookieOptions, signed string, expires time.Time)
WriteCookie sets the session cookie to signed until expires; it is always HttpOnly and SameSite=Lax.
Types ¶
type BadCookieError ¶
type BadCookieError struct{}
BadCookieError reports a session cookie that fails its HMAC check.
func (*BadCookieError) Error ¶
func (*BadCookieError) Error() string
type CookieOptions ¶
CookieOptions are the deployment-wide attributes of the session cookie.
type EndUser ¶
type EndUser struct {
OrgID uuid.UUID
ProjectID uuid.UUID
ProjectPublicID string
ProjectName string
EndUserID uuid.UUID
Subject string
UserSession string
AuthTime time.Time
// SignOutToken and LinkToken are this browser's CSRF tokens for the hosted sign-out and connect-an-upstream forms the portal posts.
SignOutToken string
LinkToken string
}
EndUser is the end user an account.v1 call runs for: the browser's active account in one project.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore keeps sessions in process memory; sessions are lost on restart.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore returns an empty MemoryStore.
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(_ context.Context, sid string) error
Delete removes the session for sid.
func (*MemoryStore) DeleteByUser ¶
DeleteByUser removes every session of userID.
func (*MemoryStore) DeleteByUserExcept ¶
DeleteByUserExcept removes every session of userID but keepSID.
func (*MemoryStore) DeleteExpired ¶
func (s *MemoryStore) DeleteExpired(_ context.Context) (int, error)
DeleteExpired removes expired sessions and reports how many went.
func (*MemoryStore) Load ¶
Load returns the live session for sid; ok is false when it is missing or expired.
type NotFoundError ¶
type NotFoundError struct{}
NotFoundError reports a session that is missing or expired.
func (*NotFoundError) Error ¶
func (*NotFoundError) Error() string
type PostgresStore ¶
type PostgresStore struct {
// contains filtered or unexported fields
}
PostgresStore keeps sealed sessions in the sessions table, always on the primary.
func NewPostgresStore ¶
func NewPostgresStore(conn *sql.DB, schema, tablePrefix string, s sealer.Sealer, unexpected apperror.UnexpectedFunc) *PostgresStore
NewPostgresStore builds a PostgresStore; conn must be the writer pool so a fresh sign-in is never missed on a lagging replica.
func (*PostgresStore) Delete ¶
func (s *PostgresStore) Delete(ctx context.Context, sid string) error
Delete removes the session for sid.
func (*PostgresStore) DeleteByUser ¶
DeleteByUser removes every session of userID; it joins the transaction on ctx when there is one.
func (*PostgresStore) DeleteByUserExcept ¶
func (s *PostgresStore) DeleteByUserExcept(ctx context.Context, userID uuid.UUID, keepSID string) error
DeleteByUserExcept removes every session of userID but keepSID; it joins the transaction on ctx when there is one.
func (*PostgresStore) DeleteExpired ¶
func (s *PostgresStore) DeleteExpired(ctx context.Context) (int, error)
DeleteExpired removes expired sessions in batches and reports how many went.
func (*PostgresStore) Load ¶
Load returns the live session for sid; an expired, missing or unopenable row reads as signed out.
type Principal ¶
type Principal struct {
UserID uuid.UUID
KeyID uuid.UUID
KeyHint string
OwnerID uuid.UUID
Email string
Name string
AvatarURL string
Source Source
IDPIssuer string
IDPSubject string
IDToken string
Scopes []string `json:"-"`
TokenScopes []string `json:"-"`
Operator bool `json:"-"`
ActiveOrgID uuid.UUID
ActiveProjectID uuid.UUID
ProjectIDs []uuid.UUID `json:"-"`
AllProjects bool `json:"-"`
ResourceIDs []uuid.UUID `json:"-"`
Locale string
TermsAcceptedAt time.Time
IssuedAt time.Time
}
Principal is the authenticated caller. SECURITY: Scopes, Operator and reach fields are rebuilt per request, never persisted.
func PrincipalFrom ¶
PrincipalFrom returns the Principal in ctx, or the zero Principal when there is none.
func System ¶
func System() Principal
System is the Principal of a trusted local process: `othra mcp` over stdio, run by whoever holds the DB DSN.
func (Principal) IsPerson ¶
IsPerson reports whether p is a signed-in person rather than a key or the shared basic credential.
func (Principal) ReachesProject ¶
ReachesProject reports whether p may act in projectID of orgID. SECURITY: never across orgs; a key only reaches granted projects.
func (Principal) ReachesResource ¶
ReachesResource reports whether p may act on resourceID inside projectID of orgID.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler adapts a Store to scheduler.Provider.
func NewScheduler ¶
NewScheduler binds store to the expired-session sweep job.
func (*Scheduler) SchedulerJobs ¶
SchedulerJobs implements scheduler.Provider.
type SignedCookie ¶
SignedCookie is an HttpOnly, SameSite=Lax cookie whose value is signed with Secret and bound to its Name.
func (SignedCookie) Clear ¶
func (c SignedCookie) Clear(w http.ResponseWriter)
Clear tells the browser to drop the cookie.
func (SignedCookie) Get ¶
func (c SignedCookie) Get(r *http.Request) (string, bool)
Get returns the cookie's value when its signature verifies for this cookie's name.
func (SignedCookie) Set ¶
func (c SignedCookie) Set(w http.ResponseWriter, value string, expires time.Time)
Set writes value, signed, until expires (at least one second).
type Store ¶
type Store interface {
Save(ctx context.Context, sid string, p Principal, expiresAt time.Time) error
Load(ctx context.Context, sid string) (Principal, bool, error)
// Update replaces the Principal of the live session sid and keeps its expiry; a missing or expired one is a *NotFoundError.
Update(ctx context.Context, sid string, p Principal) error
Delete(ctx context.Context, sid string) error
DeleteByUser(ctx context.Context, userID uuid.UUID) error
// DeleteByUserExcept removes every session of userID but keepSID.
DeleteByUserExcept(ctx context.Context, userID uuid.UUID, keepSID string) error
DeleteExpired(ctx context.Context) (int, error)
}
Store keeps browser sessions by opaque id.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package sessiontest holds the behavior every session.Store must share.
|
Package sessiontest holds the behavior every session.Store must share. |