people

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 20 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 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.

Three options serve an application that gives people limited access (Homie): Permissions offers access terms and the application's permissions on invites and members, CoOwners lets the superadmin invite more owners into their own account, and SingleAccount turns account creation and deletion off. Without them every answer is as before.

Index

Constants

View Source
const (
	ActionInviteCreate = "account.invite.create"
	ActionInviteRevoke = "account.invite.revoke"
	ActionMemberRemove = "account.member.remove"
	ActionMemberUpdate = "account.member.update"
	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).

View Source
const CodeInvalidPermission = "invalid_permission"

CodeInvalidPermission is the error code of a permission the application does not offer (400).

Variables

View Source
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")
)
View Source
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.

View Source
var ErrWidens = errors.New("people: the change widens the person's access; confirm it with step-up")

ErrWidens is Service.UpdateMember's refusal of a change that gives the person more access (a later or no end, or a permission they lacked) when the caller has not confirmed it with step-up. Nothing was changed.

Functions

func NoStore

func NoStore(next http.Handler) http.Handler

NoStore keeps every answer of next out of caches.

func RequireOwner

func RequireOwner(next http.Handler) http.Handler

RequireOwner lets only the account's owner through (403 forbidden). It runs after core.Sessions.RequireViewer.

func RequireSuperadmin

func RequireSuperadmin(next http.Handler) http.Handler

RequireSuperadmin lets only the superadmin through (403 forbidden). It runs after core.Sessions.RequireViewer.

Types

type APIError

type APIError struct {
	Status  int
	Code    string
	Message string
	Extra   map[string]string
}

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".

func NewError

func NewError(status int, code, message string) *APIError

NewError returns an *APIError.

func (*APIError) Error

func (e *APIError) Error() string

type AccessChange added in v0.2.0

type AccessChange struct {
	SetUntil       bool
	Until          *time.Time // the new end; nil is no end. A time in the past ends the access now.
	SetPermissions bool
	Permissions    []string // the new permissions, checked against the catalog; nil or empty is none
}

AccessChange is a change of a person's term and permissions; a field whose Set flag is false stays as it is.

type AccessInviter added in v0.2.0

type AccessInviter interface {
	Inviter
	// IssueInviteWithAccess is IssueInvite with the term (nil: no end) and the permissions (nil: none) of the invite.
	IssueInviteWithAccess(ctx context.Context, kind core.InviteKind, account core.AccountID, name string,
		by core.PersonID, until *time.Time, perms []string) (IssuedInvite, error)
}

AccessInviter is an Inviter that also gives a member invite an access term and permissions (core.Invite.AccessUntil and Permissions), which the person who redeems it gets. Options.Permissions requires one, so a term is never lost on the way to the invite.

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

type EraseError struct {
	Detail map[string]string
	Err    error
}

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 InviteSpec added in v0.2.0

type InviteSpec struct {
	Name string
	// Role is core.RoleMember, or empty, for a member. core.RoleOwner makes a co-owner of the account: only with
	// Options.CoOwners, only by the superadmin into their own account, and with no term or permissions.
	Role core.Role
	// AccessUntil and Permissions are the member's term (nil: no end) and permissions (nil: none), copied to the
	// person who redeems the invite. Only with Options.Permissions; see Service.Invite for the rules.
	AccessUntil *time.Time
	Permissions []string
}

InviteSpec is an invite Service.Invite makes.

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

type IssuedInvite struct {
	ID        string
	URL       string
	ExpiresAt time.Time
}

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

	// Permissions offers access terms and the application's permissions: member invites take access_until and
	// permissions, PATCH /api/account/members/{id} changes them, and GET /api/account lists them with "active". The
	// Inviter must then be an AccessInviter. nil refuses both fields and leaves every answer as before.
	Permissions core.PermissionCatalog
	// CoOwners lets the superadmin invite owners into their own account (an invite with role "owner"). Only the
	// superadmin then changes or removes an owner, revokes an owner's connection, withdraws an owner invite, or
	// deletes an account that holds another owner; owners manage the members. false refuses "role".
	CoOwners bool
	// SingleAccount is for an application with one account: POST and DELETE /api/admin/accounts answer 404, and
	// GET /api/admin/accounts lists the superadmin's own account only.
	SingleAccount bool

	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 New

func New(o Options) (*Service, error)

New returns a Service.

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) Invite added in v0.2.0

func (s *Service) Invite(ctx context.Context, a core.AccountID, spec InviteSpec, by core.PersonID,
	ip string) (IssuedInvite, error)

Invite makes invite spec to account a, by from ip. A bad name wraps ErrInvalidName; a refused role, term or permission is an *APIError: the term must be in the future and at most a year away, and every permission one the catalog offers (or core.PermissionAll). The URL holds the token: it goes to the asker only, never to a log or the audit.

func (*Service) Invites

func (s *Service) Invites(ctx context.Context, a core.AccountID) ([]core.Invite, error)

Invites returns the live member invites of account a, and its owner invites with Options.CoOwners: not used, not withdrawn, not expired.

func (*Service) Members

func (s *Service) Members(ctx context.Context, a core.AccountID) ([]core.Person, error)

Members returns the people of account a, oldest first.

func (*Service) RegisterRoutes

func (s *Service) RegisterRoutes(mux *http.ServeMux)

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. With Options.CoOwners, removing an owner or a superadmin takes the superadmin (ErrSuperadminOnly), and the audit entry records the role of the person removed.

func (*Service) RevokeInvite

func (s *Service) RevokeInvite(ctx context.Context, a core.AccountID, id string, by core.PersonID, ip string) error

RevokeInvite withdraws live invite id of account a; an invite of another account is core.ErrNotFound. With Options.CoOwners an owner invite is the superadmin's to withdraw (ErrSuperadminOnly).

func (*Service) Summaries

func (s *Service) Summaries(ctx context.Context) ([]Summary, error)

Summaries lists every account with its counts, oldest first.

func (*Service) UpdateMember added in v0.2.0

func (s *Service) UpdateMember(ctx context.Context, a core.AccountID, target, by core.PersonID, ch AccessChange,
	confirmed bool, ip string) (core.Person, error)

UpdateMember changes the term and permissions of person target of account a, by from ip, and returns the person as changed. It is audited as ActionMemberUpdate with the role of the person and what was set.

confirmed says the caller confirmed the change with step-up: a change that widens the person's access without it is ErrWidens, and nothing is changed. The refusals: yourself (400), another account's person (core.ErrNotFound), an owner or a superadmin unless by is the superadmin (ErrSuperadminOnly), a superadmin at all (409), permissions of an owner (400), a term more than a year away (400), and permissions the catalog does not offer (400 invalid_permission). Without Options.Permissions every change is refused.

type Summary

type Summary struct {
	Account core.Account
	Members int
}

Summary is an account as the superadmin's list shows it: counts, no data.

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).

Jump to

Keyboard shortcuts

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