oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3// Team mode's per-request standing. With a.team nil nothing here runs:
4// requirePerson, the route gates, the hub's standing and every store
5// write are what they have always been.
6//
7// Standing itself is home/teammode's Access. Identity is resolved from
8// the actual session — a guest's own session, a local address (and its
9// account link, if it has one), or a Home subject (an account link or a
10// Home-only person) — and the roster is asked about the Home subject, if
11// there is one. The answer is one of three:
12//
13// - Member: the team. Every meeting, every member page, and entry to
14// every call, as an allowlisted address has outside team mode. The
15// roster's role is never consulted: Calls has no owner-only power.
16// - NotMember: for somebody the roster knows (a Home subject), removal.
17// They are signed out and told so, existing sessions included, which
18// is removal within the lease (a member never holds a guest grant, so
19// a guest session would reach nothing). For somebody it does not —
20// an anonymous guest, a local account with no Oficina link — it is
21// what a non-member always was here: a guest, standing on the grants
22// the team's links gave them.
23// - Unknown: the roster could not be read within its lease. The meeting
24// list is partial (empty: without standing a member reaches nothing)
25// with a notice; everything else that needs standing is a retryable
26// 503, never a 404 that would claim the meeting is gone.
27//
28// A Member decision carries a lease. Access.State checks it every time it
29// is asked; a write checks it again inside its own transaction, before
30// and after (team_guard.go); the list asks again after it is built; a
31// call's stream re-decides before each delivery and at the lease's end
32// (team_stream.go).
33
34import (
35 "context"
36 "errors"
37 "net/http"
38
39 "amadan.net/oficina/home/connect"
40 "amadan.net/oficina/home/roster"
41 "amadan.net/oficina/home/teammode"
42 "amadan.net/rastrillo/rastrillo/sessions"
43
44 "amadan.net/oficina/calls/internal/calls/hub"
45 "amadan.net/oficina/calls/internal/calls/teamstate"
46)
47
48// errTeamUnavailable is team standing that cannot be decided now: the
49// roster within its lease, or the storage behind a session's identity. It
50// is teammode's ErrUnavailable, so the guard's refusals and the gates'
51// read the same, and the hub's ErrStandingUnknown, so the hub holds a
52// resuming stream rather than refusing it (hub.Resume).
53var errTeamUnavailable = errors.Join(teammode.ErrUnavailable, hub.ErrStandingUnknown)
54
55// teamReq is who one request's session speaks for in team mode, and
56// their standing. Never stored beyond the request.
57type teamReq struct {
58 person Person
59 // subject is the Home subject the roster knows them by, "" for an
60 // anonymous guest or a local account with no Oficina link.
61 subject string
62 // method is the session's sign-in method.
63 method string
64 access *teammode.Access
65}
66
67// removed is a definite NotMember for somebody the roster knows: a
68// member taken off the team, who is signed out.
69func (t *teamReq) removed() bool {
70 return t.subject != "" && t.access.State() == roster.NotMember
71}
72
73type teamReqKey struct{}
74
75// withTeamReq carries t, and its Access for the store's guard, on ctx.
76func withTeamReq(ctx context.Context, t *teamReq) context.Context {
77 return context.WithValue(teammode.WithAccess(ctx, t.access), teamReqKey{}, t)
78}
79
80func teamReqFrom(ctx context.Context) (*teamReq, bool) {
81 t, ok := ctx.Value(teamReqKey{}).(*teamReq)
82 return t, ok && t != nil
83}
84
85// teamSubjectFor is the Home subject a session's person is known by: a
86// Home session's own subject, a local account's link, or "" for a guest
87// and for a local account with no link. errTeamUnavailable is storage
88// that cannot be read now.
89func (a *app) teamSubjectFor(ctx context.Context, s sessions.Session, p Person) (string, error) {
90 switch {
91 case p.Anonymous || s.Method == "guest":
92 return "", nil
93 case s.Method == connect.HomeMethod:
94 return s.Subject, nil
95 }
96 return a.homeSubjectOf(ctx, p.ID)
97}
98
99// homeSubjectOf is the Home subject the roster knows a person by: a
100// Home-only row or an account link, "" for neither.
101func (a *app) homeSubjectOf(ctx context.Context, id int64) (string, error) {
102 subject, err := teamstate.HomeSubject(ctx, a.team.db, a.team.issuer(), id)
103 switch {
104 case err == nil:
105 return subject, nil
106 case errors.Is(err, teamstate.ErrRefused):
107 return "", nil
108 }
109 return "", errTeamUnavailable
110}
111
112// teamReqFor resolves a session to its person and asks the roster about
113// them, once. errHomeIdentity (and errHomeRemoved) are personForSession's:
114// a session that speaks for nobody here. errTeamUnavailable is storage.
115func (a *app) teamReqFor(ctx context.Context, s sessions.Session) (*teamReq, error) {
116 p, err := a.personForSession(ctx, s)
117 if err != nil {
118 if !errors.Is(err, errHomeIdentity) {
119 return nil, errTeamUnavailable
120 }
121 return nil, err
122 }
123 subject, err := a.teamSubjectFor(ctx, s, p)
124 if err != nil {
125 return nil, err
126 }
127 return &teamReq{person: p, subject: subject, method: s.Method, access: a.team.Access(ctx, subject)}, nil
128}
129
130// teamRequirePerson is requirePerson in team mode: who the session is,
131// and the roster's answer, asked once and carried on the request.
132//
133// A definite NotMember for a Home subject is signed out and told so
134// (endRemovedSession), existing sessions included: that is removal within
135// the lease. A guest, and a local account with no link, are let through
136// as the non-members they are. A session that speaks for nobody is the
137// 404 (or, for a refused Home link, the sign-out) it has always been.
138// Storage that cannot be read is a 503, never a sign-out. Unknown ends
139// nothing.
140func (a *app) teamRequirePerson(w http.ResponseWriter, r *http.Request, s sessions.Session, next http.Handler) {
141 t, err := a.teamReqFor(r.Context(), s)
142 switch {
143 case errors.Is(err, errHomeRemoved):
144 a.endRemovedSession(w, r)
145 return
146 case errors.Is(err, errHomeIdentity):
147 a.notFound(w, r)
148 return
149 case err != nil:
150 a.teamUnavailable(w, r)
151 return
152 }
153 if t.removed() {
154 a.endRemovedSession(w, r)
155 return
156 }
157 ctx := context.WithValue(withTeamReq(r.Context(), t), personCtxKey{}, t.person)
158 next.ServeHTTP(w, r.WithContext(ctx))
159}
160
161// memberState is p's member standing. Outside team mode it is the
162// allowlist's, Member or NotMember, exactly isMember. In team mode it is
163// the roster's: for the request's own person the Access requirePerson
164// carried (latched); for anyone else asked now (standingOf).
165func (a *app) memberState(ctx context.Context, p Person) roster.State {
166 if a.team == nil {
167 if a.isMember(p) {
168 return roster.Member
169 }
170 return roster.NotMember
171 }
172 if p.Anonymous {
173 return roster.NotMember
174 }
175 if t, ok := teamReqFrom(ctx); ok && t.person.ID == p.ID {
176 return t.access.State()
177 }
178 return a.standingOf(ctx, p.ID).State()
179}
180
181// memberNow is memberState == Member: the one question the pages ask.
182func (a *app) memberNow(ctx context.Context, p Person) bool {
183 return a.memberState(ctx, p) == roster.Member
184}
185
186// standingOf builds the roster's answer about somebody other than the
187// request's own person — a meeting's host, an agent's operator, a
188// knock's push recipient, a participation the hub re-derives — so what it
189// says about them is what their own requests would be told. A person the
190// roster cannot be asked about (no link, no Home-only row) is NotMember;
191// one whose subject cannot be read now is Unknown (a nil Access).
192func (a *app) standingOf(ctx context.Context, person int64) *teammode.Access {
193 subject, err := a.homeSubjectOf(ctx, person)
194 if err != nil {
195 return nil
196 }
197 return a.team.Access(ctx, subject)
198}
199
200// requireTeamMember is requireMember in team mode: Member, or a retryable
201// 503 for Unknown. A non-member is the 404 the gate always answered — an
202// unlinked local account the local door admits is sent to link instead
203// (requireLinkable), which is the only way it can gain standing.
204func (a *app) requireTeamMember(w http.ResponseWriter, r *http.Request, next http.Handler) {
205 t, ok := teamReqFrom(r.Context())
206 if !ok {
207 // requirePerson puts the request on every route behind this
208 // gate; a route that reaches here without it is a wiring bug, and
209 // the closed answer is the one that cannot leak.
210 a.teamUnavailable(w, r)
211 return
212 }
213 switch t.access.State() {
214 case roster.Member:
215 next.ServeHTTP(w, r)
216 case roster.Unknown:
217 a.teamUnavailable(w, r)
218 default:
219 if a.linkable(t) && (r.Method == http.MethodGet || r.Method == http.MethodHead) {
220 w.Header().Set("Cache-Control", "no-store")
221 http.Redirect(w, r, "/account/home", http.StatusSeeOther)
222 return
223 }
224 a.notFound(w, r)
225 }
226}
227
228// linkable is a local session the allowlist admits, not yet linked to an
229// Oficina account: in team mode the allowlist is the local door and
230// nothing more, and linking is how such an account comes to stand on the
231// roster.
232func (a *app) linkable(t *teamReq) bool {
233 return a.home != nil && t.subject == "" && !t.person.Anonymous &&
234 (t.method == "magiclink" || t.method == "keymail") && a.allowNow().allows(t.person.Email)
235}
236
237// requireLinkable gates /account/home in team mode: the local door's
238// admitted, unlinked accounts, and nobody else (a linked account and a
239// Home session already are the connection). Outside team mode the link
240// routes stay behind requireMember.
241func (a *app) requireLinkable(next http.Handler) http.Handler {
242 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
243 t, ok := teamReqFrom(r.Context())
244 if !ok || !a.linkable(t) {
245 a.notFound(w, r)
246 return
247 }
248 next.ServeHTTP(w, r)
249 })
250}
251
252// homeOnlyLabel is how a Home-only person, who has no address, is named
253// where an address or a name would be: "Oficina account" and the last
254// four characters of the person ref ("Oficina account …k7qz"), as Home's
255// Team page shows a ref. It is the persons row's name.
256func homeOnlyLabel(subject string) string {
257 const label = "Oficina account"
258 if len(subject) <= 4 {
259 return label
260 }
261 return label + " …" + subject[len(subject)-4:]
262}
263
264// shown is how a person is named where an address would be: their
265// address, or for a Home-only person, who has none, their label. Outside
266// team mode no persons row holds a Home-only placeholder, so it is
267// exactly Email.
268func (p Person) shown() string {
269 if teamstate.Addressless(p.Email) {
270 return p.Name
271 }
272 return p.Email
273}
274