package calls // Team mode's per-request standing. With a.team nil nothing here runs: // requirePerson, the route gates, the hub's standing and every store // write are what they have always been. // // Standing itself is home/teammode's Access. Identity is resolved from // the actual session — a guest's own session, a local address (and its // account link, if it has one), or a Home subject (an account link or a // Home-only person) — and the roster is asked about the Home subject, if // there is one. The answer is one of three: // // - Member: the team. Every meeting, every member page, and entry to // every call, as an allowlisted address has outside team mode. The // roster's role is never consulted: Calls has no owner-only power. // - NotMember: for somebody the roster knows (a Home subject), removal. // They are signed out and told so, existing sessions included, which // is removal within the lease (a member never holds a guest grant, so // a guest session would reach nothing). For somebody it does not — // an anonymous guest, a local account with no Oficina link — it is // what a non-member always was here: a guest, standing on the grants // the team's links gave them. // - Unknown: the roster could not be read within its lease. The meeting // list is partial (empty: without standing a member reaches nothing) // with a notice; everything else that needs standing is a retryable // 503, never a 404 that would claim the meeting is gone. // // A Member decision carries a lease. Access.State checks it every time it // is asked; a write checks it again inside its own transaction, before // and after (team_guard.go); the list asks again after it is built; a // call's stream re-decides before each delivery and at the lease's end // (team_stream.go). import ( "context" "errors" "net/http" "amadan.net/oficina/home/connect" "amadan.net/oficina/home/roster" "amadan.net/oficina/home/teammode" "amadan.net/rastrillo/rastrillo/sessions" "amadan.net/oficina/calls/internal/calls/hub" "amadan.net/oficina/calls/internal/calls/teamstate" ) // errTeamUnavailable is team standing that cannot be decided now: the // roster within its lease, or the storage behind a session's identity. It // is teammode's ErrUnavailable, so the guard's refusals and the gates' // read the same, and the hub's ErrStandingUnknown, so the hub holds a // resuming stream rather than refusing it (hub.Resume). var errTeamUnavailable = errors.Join(teammode.ErrUnavailable, hub.ErrStandingUnknown) // teamReq is who one request's session speaks for in team mode, and // their standing. Never stored beyond the request. type teamReq struct { person Person // subject is the Home subject the roster knows them by, "" for an // anonymous guest or a local account with no Oficina link. subject string // method is the session's sign-in method. method string access *teammode.Access } // removed is a definite NotMember for somebody the roster knows: a // member taken off the team, who is signed out. func (t *teamReq) removed() bool { return t.subject != "" && t.access.State() == roster.NotMember } type teamReqKey struct{} // withTeamReq carries t, and its Access for the store's guard, on ctx. func withTeamReq(ctx context.Context, t *teamReq) context.Context { return context.WithValue(teammode.WithAccess(ctx, t.access), teamReqKey{}, t) } func teamReqFrom(ctx context.Context) (*teamReq, bool) { t, ok := ctx.Value(teamReqKey{}).(*teamReq) return t, ok && t != nil } // teamSubjectFor is the Home subject a session's person is known by: a // Home session's own subject, a local account's link, or "" for a guest // and for a local account with no link. errTeamUnavailable is storage // that cannot be read now. func (a *app) teamSubjectFor(ctx context.Context, s sessions.Session, p Person) (string, error) { switch { case p.Anonymous || s.Method == "guest": return "", nil case s.Method == connect.HomeMethod: return s.Subject, nil } return a.homeSubjectOf(ctx, p.ID) } // homeSubjectOf is the Home subject the roster knows a person by: a // Home-only row or an account link, "" for neither. func (a *app) homeSubjectOf(ctx context.Context, id int64) (string, error) { subject, err := teamstate.HomeSubject(ctx, a.team.db, a.team.issuer(), id) switch { case err == nil: return subject, nil case errors.Is(err, teamstate.ErrRefused): return "", nil } return "", errTeamUnavailable } // teamReqFor resolves a session to its person and asks the roster about // them, once. errHomeIdentity (and errHomeRemoved) are personForSession's: // a session that speaks for nobody here. errTeamUnavailable is storage. func (a *app) teamReqFor(ctx context.Context, s sessions.Session) (*teamReq, error) { p, err := a.personForSession(ctx, s) if err != nil { if !errors.Is(err, errHomeIdentity) { return nil, errTeamUnavailable } return nil, err } subject, err := a.teamSubjectFor(ctx, s, p) if err != nil { return nil, err } return &teamReq{person: p, subject: subject, method: s.Method, access: a.team.Access(ctx, subject)}, nil } // teamRequirePerson is requirePerson in team mode: who the session is, // and the roster's answer, asked once and carried on the request. // // A definite NotMember for a Home subject is signed out and told so // (endRemovedSession), existing sessions included: that is removal within // the lease. A guest, and a local account with no link, are let through // as the non-members they are. A session that speaks for nobody is the // 404 (or, for a refused Home link, the sign-out) it has always been. // Storage that cannot be read is a 503, never a sign-out. Unknown ends // nothing. func (a *app) teamRequirePerson(w http.ResponseWriter, r *http.Request, s sessions.Session, next http.Handler) { t, err := a.teamReqFor(r.Context(), s) switch { case errors.Is(err, errHomeRemoved): a.endRemovedSession(w, r) return case errors.Is(err, errHomeIdentity): a.notFound(w, r) return case err != nil: a.teamUnavailable(w, r) return } if t.removed() { a.endRemovedSession(w, r) return } ctx := context.WithValue(withTeamReq(r.Context(), t), personCtxKey{}, t.person) next.ServeHTTP(w, r.WithContext(ctx)) } // memberState is p's member standing. Outside team mode it is the // allowlist's, Member or NotMember, exactly isMember. In team mode it is // the roster's: for the request's own person the Access requirePerson // carried (latched); for anyone else asked now (standingOf). func (a *app) memberState(ctx context.Context, p Person) roster.State { if a.team == nil { if a.isMember(p) { return roster.Member } return roster.NotMember } if p.Anonymous { return roster.NotMember } if t, ok := teamReqFrom(ctx); ok && t.person.ID == p.ID { return t.access.State() } return a.standingOf(ctx, p.ID).State() } // memberNow is memberState == Member: the one question the pages ask. func (a *app) memberNow(ctx context.Context, p Person) bool { return a.memberState(ctx, p) == roster.Member } // standingOf builds the roster's answer about somebody other than the // request's own person — a meeting's host, an agent's operator, a // knock's push recipient, a participation the hub re-derives — so what it // says about them is what their own requests would be told. A person the // roster cannot be asked about (no link, no Home-only row) is NotMember; // one whose subject cannot be read now is Unknown (a nil Access). func (a *app) standingOf(ctx context.Context, person int64) *teammode.Access { subject, err := a.homeSubjectOf(ctx, person) if err != nil { return nil } return a.team.Access(ctx, subject) } // requireTeamMember is requireMember in team mode: Member, or a retryable // 503 for Unknown. A non-member is the 404 the gate always answered — an // unlinked local account the local door admits is sent to link instead // (requireLinkable), which is the only way it can gain standing. func (a *app) requireTeamMember(w http.ResponseWriter, r *http.Request, next http.Handler) { t, ok := teamReqFrom(r.Context()) if !ok { // requirePerson puts the request on every route behind this // gate; a route that reaches here without it is a wiring bug, and // the closed answer is the one that cannot leak. a.teamUnavailable(w, r) return } switch t.access.State() { case roster.Member: next.ServeHTTP(w, r) case roster.Unknown: a.teamUnavailable(w, r) default: if a.linkable(t) && (r.Method == http.MethodGet || r.Method == http.MethodHead) { w.Header().Set("Cache-Control", "no-store") http.Redirect(w, r, "/account/home", http.StatusSeeOther) return } a.notFound(w, r) } } // linkable is a local session the allowlist admits, not yet linked to an // Oficina account: in team mode the allowlist is the local door and // nothing more, and linking is how such an account comes to stand on the // roster. func (a *app) linkable(t *teamReq) bool { return a.home != nil && t.subject == "" && !t.person.Anonymous && (t.method == "magiclink" || t.method == "keymail") && a.allowNow().allows(t.person.Email) } // requireLinkable gates /account/home in team mode: the local door's // admitted, unlinked accounts, and nobody else (a linked account and a // Home session already are the connection). Outside team mode the link // routes stay behind requireMember. func (a *app) requireLinkable(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t, ok := teamReqFrom(r.Context()) if !ok || !a.linkable(t) { a.notFound(w, r) return } next.ServeHTTP(w, r) }) } // homeOnlyLabel is how a Home-only person, who has no address, is named // where an address or a name would be: "Oficina account" and the last // four characters of the person ref ("Oficina account …k7qz"), as Home's // Team page shows a ref. It is the persons row's name. func homeOnlyLabel(subject string) string { const label = "Oficina account" if len(subject) <= 4 { return label } return label + " …" + subject[len(subject)-4:] } // shown is how a person is named where an address would be: their // address, or for a Home-only person, who has none, their label. Outside // team mode no persons row holds a Home-only placeholder, so it is // exactly Email. func (p Person) shown() string { if teamstate.Addressless(p.Email) { return p.Name } return p.Email }