Documentation
¶
Overview ¶
Package people keeps accounts and their members, and serves the account and superadmin JSON API: members and invites of an account, its connections and audit, accounts made by the superadmin, the system audit, and "delete everything", which lets the application erase its own data of the account before the gatekeeper deletes its rows.
The account comes from the session, always: no account route takes an account id from the client, and no route returns another account's members, connections or audit, to anyone, the superadmin included. The superadmin's own account works like anyone else's. The superadmin routes manage accounts (counts only) and read the system audit.
The application plugs in through small interfaces (see Options): the Inviter (package auth), the GrantRevoker (package oauth), and the optional AccountFields, SummaryFields, Eraser and ConnectionSource of its own.
Index ¶
- Constants
- Variables
- func NoStore(next http.Handler) http.Handler
- func RequireOwner(next http.Handler) http.Handler
- func RequireSuperadmin(next http.Handler) http.Handler
- type APIError
- type AccountFields
- type Connection
- type ConnectionSource
- type EraseError
- type Eraser
- type GrantRevoker
- type Inviter
- type IssuedInvite
- type Options
- type Service
- func (s *Service) CreateAccount(ctx context.Context, name, ownerName string, by core.PersonID, ip string) (core.Account, IssuedInvite, error)
- func (s *Service) CreateInvite(ctx context.Context, a core.AccountID, name string, by core.PersonID, ...) (IssuedInvite, error)
- func (s *Service) DeleteAccount(ctx context.Context, a core.AccountID, actor core.PersonID, ip, by string) error
- func (s *Service) Invites(ctx context.Context, a core.AccountID) ([]core.Invite, error)
- func (s *Service) Members(ctx context.Context, a core.AccountID) ([]core.Person, error)
- func (s *Service) RegisterRoutes(mux *http.ServeMux)
- func (s *Service) RemoveMember(ctx context.Context, a core.AccountID, target, by core.PersonID, ip string) error
- func (s *Service) RevokeInvite(ctx context.Context, a core.AccountID, id string, by core.PersonID, ip string) error
- func (s *Service) Summaries(ctx context.Context) ([]Summary, error)
- type Summary
- type SummaryFields
Constants ¶
const ( ActionInviteCreate = "account.invite.create" ActionInviteRevoke = "account.invite.revoke" ActionMemberRemove = "account.member.remove" ActionCreate = "account.create" ActionDelete = "account.delete" ActionDeleteFail = "account.delete.fail" )
Audit actions of this package. Invites and members are audited in the account; creating and deleting accounts are system entries (a deleted account's own entries can no longer be read by anyone).
Variables ¶
var ( // ErrSelf refuses removing yourself. ErrSelf = errors.New("people: you cannot remove yourself") // ErrSuperadmin refuses removing a superadmin from an account: the application would lose its administrator. ErrSuperadmin = fmt.Errorf("people: a superadmin cannot be removed from an account: %w", core.ErrConflict) // ErrInvalidName is wrapped by every refusal of a name (the rule is Options.NormalizeName). ErrInvalidName = errors.New("people: invalid name") )
var ( // ErrOwnerOnly is 403 forbidden for a member where only the owner may act. ErrOwnerOnly = NewError(http.StatusForbidden, httpsec.CodeForbidden, "only the owner of the account may do this") // ErrSuperadminOnly is 403 forbidden for anyone but the superadmin. ErrSuperadminOnly = NewError(http.StatusForbidden, httpsec.CodeForbidden, "only the superadmin may do this") // ErrNoConnection is 404 not_found for a connection the viewer may not see or that is gone. ErrNoConnection = NewError(http.StatusNotFound, httpsec.CodeNotFound, "no such connection") )
Refusals an application's extension may answer with, so they read exactly as this package's own.
Functions ¶
func RequireOwner ¶
RequireOwner lets only the account's owner through (403 forbidden). It runs after core.Sessions.RequireViewer.
Types ¶
type APIError ¶
APIError is an answer of the JSON error form {"error":"<code>","message":"…"} (httpsec). The message is English and never holds a secret or application data. Extra adds fields to the body (Family Hub: "module" and "reason" of a module that did not erase an account); it never replaces "error" or "message".
type AccountFields ¶
type AccountFields interface {
// AccountFields returns top-level fields for the viewer's account; every value must marshal to JSON. A key of
// this package's answer ("id", "name", "members", "invites") is refused with 500. An error is answered like
// every error of this package.
AccountFields(ctx context.Context, v core.Viewer) (map[string]any, error)
}
AccountFields adds the application's fields to GET /api/account (Family Hub: "modules", the modules the viewer sees with their switches).
type Connection ¶
type Connection struct {
ID string // the "grant_id" of the row; ConnectionSource.Owns reports true for it
ResourceID core.ResourceID
Assistant core.Assistant // e.g. "site"
ClientName string
Label string // optional
PersonID core.PersonID
CreatedAt time.Time
LastUsedAt *time.Time
}
Connection is a connection the application keeps besides the OAuth grants (Family Hub: the legacy keys of ChatGPT Sites), as GET /api/account/connections lists it.
type ConnectionSource ¶
type ConnectionSource interface {
// Connections returns the connections of members, the people of v's account (oldest first). The handler drops
// any of a person outside the account, shows a member only their own, and names each person.
Connections(ctx context.Context, v core.Viewer, members []core.Person) ([]Connection, error)
// Owns reports whether id names one of its connections (Family Hub: the "lk_" prefix). DELETE
// /api/account/connections/{id} hands such an id to Revoke and never to the grants.
Owns(id string) bool
// Revoke removes connection id of v's account and audits it, from ip. Refusals are *APIErrors, e.g.
// ErrNoConnection for one that is unknown or not v's to see, ErrOwnerOnly.
Revoke(ctx context.Context, v core.Viewer, id, ip string) error
}
ConnectionSource lists and revokes the application's own connections.
type EraseError ¶
EraseError is an Eraser's refusal: Detail goes into the "account.delete.fail" audit entry, Err is what the client is answered (an *APIError, e.g. 502 upstream with the failing part in Extra).
func (*EraseError) Error ¶
func (e *EraseError) Error() string
func (*EraseError) Unwrap ¶
func (e *EraseError) Unwrap() error
type Eraser ¶
type Eraser interface {
// EraseAccount erases account a, deleted by actor. Its detail goes into the "account.delete" audit entry (never
// "by", which this package sets). An error stops the deletion: the account stays, so deleting it again retries.
// An *EraseError says what to audit and what to answer; any other error is audited without detail and answered
// like every error of this package (an *APIError as it is, otherwise 500 internal). ctx is not cancelled when
// the client goes away.
EraseAccount(ctx context.Context, a core.AccountID, actor core.PersonID) (detail map[string]string, err error)
}
Eraser erases the application's own data of an account before the gatekeeper deletes the account's rows (Family Hub: every module the account used erases it).
type GrantRevoker ¶
type GrantRevoker interface {
RevokeGrant(ctx context.Context, id core.GrantID, actor core.PersonID, ip string) error
}
GrantRevoker revokes a connection and audits it; (*oauth.Server).RevokeGrant implements it.
type Inviter ¶
type Inviter interface {
IssueInvite(ctx context.Context, kind core.InviteKind, account core.AccountID, name string,
by core.PersonID) (IssuedInvite, error)
// IssueInviteIn is IssueInvite on st; the transaction travels in ctx.
IssueInviteIn(ctx context.Context, st store.Store, kind core.InviteKind, account core.AccountID, name string,
by core.PersonID) (IssuedInvite, error)
}
Inviter issues owner and member invites; package auth implements it (its auth.IssuedInvite converts to IssuedInvite). Errors wrap core.ErrNotFound for an unknown account.
type IssuedInvite ¶
IssuedInvite is a new one-time link. URL carries the token in its fragment: show it once to whoever asked, never log or store it. Its fields are those of auth.IssuedInvite, so one converts to the other.
type Options ¶
type Options struct {
Site core.Site
Brand core.Brand // SessionCookie: DELETE /api/account clears it
Sessions core.Sessions
Store store.Store
Inviter Inviter
Revoker GrantRevoker
// NormalizeName is the name rule of package auth (auth.NormalizeName): it trims a display name or refuses it.
NormalizeName func(string) (string, error)
Eraser Eraser // nil: the application keeps nothing of an account
Fields AccountFields // nil: GET /api/account has no fields of the application
Summaries SummaryFields // nil: GET /api/admin/accounts has no fields of the application
Connections ConnectionSource // nil: the connections are the OAuth grants only
Log *slog.Logger // nil discards
Clock core.Clock // nil is the system clock
}
Options configure New. Site, Brand.SessionCookie, Sessions, Store, Inviter, Revoker and NormalizeName are required.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service manages accounts and members and serves their API (RegisterRoutes). It is safe for concurrent use.
func (*Service) CreateAccount ¶
func (s *Service) CreateAccount(ctx context.Context, name, ownerName string, by core.PersonID, ip string) (core.Account, IssuedInvite, error)
CreateAccount makes an account called name and the invite of its owner, called ownerName, in one transaction, by from ip. A bad name wraps ErrInvalidName.
func (*Service) CreateInvite ¶
func (s *Service) CreateInvite(ctx context.Context, a core.AccountID, name string, by core.PersonID, ip string) (IssuedInvite, error)
CreateInvite makes a member invite to account a for a person called name, by from ip. A bad name wraps ErrInvalidName. The URL holds the token: it goes to the asker only, never to a log or the audit.
func (*Service) DeleteAccount ¶
func (s *Service) DeleteAccount(ctx context.Context, a core.AccountID, actor core.PersonID, ip, by string) error
DeleteAccount erases account a everywhere, by actor from ip; by says who asked ("owner" or "superadmin").
The application's Eraser erases its own data first. Only when it succeeded are the account's rows deleted, in one transaction; otherwise the account stays, so deleting it again retries, and the failure is audited in the account ("account.delete.fail"). The work runs on a context the client cannot cancel: a browser giving up must not leave it half done.
func (*Service) Invites ¶
Invites returns the live member invites of account a: not used, not withdrawn, not expired.
func (*Service) RegisterRoutes ¶
RegisterRoutes implements core.RouteRegistrar. Every route runs NoStore and RequireViewer, which also applies the CSRF checks to writes; then the role check, then step-up where it is required, so a member is told "forbidden" rather than asked for a passkey.
func (*Service) RemoveMember ¶
func (s *Service) RemoveMember(ctx context.Context, a core.AccountID, target, by core.PersonID, ip string) error
RemoveMember deletes person target of account a with their passkeys, sessions and grants, by from ip. Removing yourself is ErrSelf, a superadmin ErrSuperadmin, a person of another account core.ErrNotFound.
type SummaryFields ¶
type SummaryFields interface {
// SummaryFields returns fields of account a, under the same rules as AccountFields ("id", "name", "members" and
// "created_at" are this package's).
SummaryFields(ctx context.Context, a core.AccountID) (map[string]any, error)
}
SummaryFields adds the application's fields to each row of GET /api/admin/accounts (Family Hub: "modules", how many are switched on). Counts only: the superadmin never sees an account's data.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package actorname keeps the name of an audit entry's actor, as it was when the entry was written.
|
Package actorname keeps the name of an audit entry's actor, as it was when the entry was written. |
|
internal
|
|
|
memstore
Package memstore is an in-memory store.Store for the tests of package people (Family Hub's admintest store, on the gatekeeper's interfaces).
|
Package memstore is an in-memory store.Store for the tests of package people (Family Hub's admintest store, on the gatekeeper's interfaces). |