| 1 | package memoria |
| 2 | |
| 3 | // Team mode's per-request standing. Outside team mode (a.teamMode() |
| 4 | // false) nothing here runs and every gate is the owners list it has |
| 5 | // always been. |
| 6 | // |
| 7 | // Standing itself is home/teammode's Access. Identity is resolved from |
| 8 | // the actual session — a local address the owners list admits, an |
| 9 | // account link, or a Home-only member — and the roster is asked about the |
| 10 | // Home subject, if there is one. The answer is one of three: |
| 11 | // |
| 12 | // - Member: the whole of Memoria, which is all team rung: memoria's |
| 13 | // corpus is the team's, with no per-person rows to reach. The |
| 14 | // roster's role says owner or member, and only the roster's; an owner |
| 15 | // also manages the connectors (requireOwner). |
| 16 | // - NotMember: nothing. Memoria has no shares, so a guest reaches |
| 17 | // nothing and is signed out and told so, exactly as a person taken |
| 18 | // off MEMORIA_OWNERS always has been. The one exception is a local |
| 19 | // account with no Oficina link, which has no standing until it links |
| 20 | // and is sent to /account/home to do that. |
| 21 | // - Unknown: the roster could not be read within its lease. Lists are |
| 22 | // partial with a notice (Paul, 2026-10-28) — and, with no shares, |
| 23 | // empty — and everything else is a retryable 503 rather than a 404 |
| 24 | // that would claim the thing is gone. |
| 25 | // |
| 26 | // A Member decision carries a lease. Access.State checks it every time it |
| 27 | // is asked; a write checks it again inside its own transaction, before |
| 28 | // and after (team_guard.go); a list asks it again after it is built. |
| 29 | |
| 30 | import ( |
| 31 | "context" |
| 32 | "errors" |
| 33 | "net/http" |
| 34 | |
| 35 | "amadan.net/oficina/home/connect" |
| 36 | "amadan.net/oficina/home/connect/linkstore" |
| 37 | "amadan.net/oficina/home/roster" |
| 38 | "amadan.net/oficina/home/teammode" |
| 39 | "amadan.net/rastrillo/rastrillo/sessions" |
| 40 | |
| 41 | "amadan.net/oficina/memoria/internal/memoria/teamstate" |
| 42 | ) |
| 43 | |
| 44 | // teamPerson is who one request's session speaks for in team mode, and |
| 45 | // their standing. Never stored beyond the request. |
| 46 | type teamPerson struct { |
| 47 | // address is the local address, "" for a Home-only member. |
| 48 | address string |
| 49 | // subject is the Home subject the roster knows them by, "" for a |
| 50 | // local account with no Oficina link (no standing). |
| 51 | subject string |
| 52 | // method is the session's sign-in method. |
| 53 | method string |
| 54 | access *teammode.Access |
| 55 | } |
| 56 | |
| 57 | // unlinkedLocal reports whether p is a local account with no Oficina |
| 58 | // link: identity, but nothing to ask the roster about. |
| 59 | func (p *teamPerson) unlinkedLocal() bool { |
| 60 | return p.subject == "" && (p.method == "magiclink" || p.method == "keymail") |
| 61 | } |
| 62 | |
| 63 | var errNoIdentity = errors.New("memoria: no team identity") |
| 64 | |
| 65 | type teamPersonKey struct{} |
| 66 | |
| 67 | // withTeamPerson carries p, and its Access for the store's guard, on ctx. |
| 68 | func withTeamPerson(ctx context.Context, p *teamPerson) context.Context { |
| 69 | return context.WithValue(teammode.WithAccess(ctx, p.access), teamPersonKey{}, p) |
| 70 | } |
| 71 | |
| 72 | func teamPersonFrom(ctx context.Context) (*teamPerson, bool) { |
| 73 | p, ok := ctx.Value(teamPersonKey{}).(*teamPerson) |
| 74 | return p, ok && p != nil |
| 75 | } |
| 76 | |
| 77 | // teamIdentify resolves a session to the person it speaks for. It reads |
| 78 | // and never creates: a Home subject resolves through its link or its |
| 79 | // Home-only row (connect's Resolve, with the HomeOnly hook), and a local |
| 80 | // session through the owners list and its link. errTeamUnavailable is |
| 81 | // storage that cannot be read now; errNoIdentity a session that speaks |
| 82 | // for nobody here. |
| 83 | func (a *app) teamIdentify(ctx context.Context, s sessions.Session) (teamPerson, error) { |
| 84 | if a.team == nil { |
| 85 | return teamPerson{}, errTeamUnavailable |
| 86 | } |
| 87 | switch s.Method { |
| 88 | case connect.HomeMethod: |
| 89 | if a.home == nil { |
| 90 | return teamPerson{}, errNoIdentity |
| 91 | } |
| 92 | p, err := a.home.bridge.Resolve(ctx, s.Subject) |
| 93 | if errors.Is(err, linkstore.ErrStorage) { |
| 94 | return teamPerson{}, errTeamUnavailable |
| 95 | } |
| 96 | if err != nil { |
| 97 | return teamPerson{}, errNoIdentity |
| 98 | } |
| 99 | return teamPerson{address: p.Label, subject: s.Subject, method: s.Method}, nil |
| 100 | case "magiclink", "keymail": |
| 101 | // MEMORIA_OWNERS, in team mode, admits a local sign-in and |
| 102 | // nothing more: the session still needs the owners list, and its |
| 103 | // standing comes from the roster through its link. |
| 104 | address := normaliseAddress(s.Subject) |
| 105 | if address == "" || !a.Admit(address) { |
| 106 | return teamPerson{}, errNoIdentity |
| 107 | } |
| 108 | p := teamPerson{address: address, method: s.Method} |
| 109 | id, found, err := a.homeLocalID(ctx, address) |
| 110 | if err != nil { |
| 111 | return teamPerson{}, errTeamUnavailable |
| 112 | } |
| 113 | if !found { |
| 114 | return p, nil |
| 115 | } |
| 116 | subject, err := teamstate.LinkedSubject(ctx, a.team.db, a.team.issuer(), id) |
| 117 | switch { |
| 118 | case err == nil: |
| 119 | p.subject = subject |
| 120 | case errors.Is(err, teamstate.ErrRefused): |
| 121 | default: |
| 122 | return teamPerson{}, errTeamUnavailable |
| 123 | } |
| 124 | return p, nil |
| 125 | } |
| 126 | return teamPerson{}, errNoIdentity |
| 127 | } |
| 128 | |
| 129 | // teamResolve is teamIdentify with the roster's answer attached: one |
| 130 | // Access per request, which every later check reads (State latches). |
| 131 | func (a *app) teamResolve(ctx context.Context, s sessions.Session) (*teamPerson, error) { |
| 132 | p, err := a.teamIdentify(ctx, s) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | p.access = a.team.Access(ctx, p.subject) |
| 137 | return &p, nil |
| 138 | } |
| 139 | |
| 140 | // teamSession is requireSession's inside in team mode: who the session |
| 141 | // is, and the roster's answer, asked once and carried on the request. |
| 142 | // |
| 143 | // A definite NotMember is signed out and told so (offboard), as a person |
| 144 | // taken off MEMORIA_OWNERS always has been, existing sessions included: |
| 145 | // that is removal within the lease. An unlinked local account is let |
| 146 | // through to the routes that need only a session — /account/home, where |
| 147 | // it links — and the content routes send it there (standing). Unknown |
| 148 | // ends nothing. |
| 149 | func (a *app) teamSession(w http.ResponseWriter, r *http.Request, next http.Handler) { |
| 150 | s, ok := sessions.Current(r) |
| 151 | if !ok && a.sess != nil { |
| 152 | s, ok = a.sess.From(r) |
| 153 | } |
| 154 | if !ok { |
| 155 | a.errorPage(w, r, http.StatusNotFound, kindRoute, "") |
| 156 | return |
| 157 | } |
| 158 | p, err := a.teamResolve(r.Context(), s) |
| 159 | switch { |
| 160 | case errors.Is(err, errTeamUnavailable): |
| 161 | a.teamUnavailable(w, r) |
| 162 | return |
| 163 | case err != nil: |
| 164 | a.offboard(w, r) |
| 165 | return |
| 166 | } |
| 167 | if p.access.State() == roster.NotMember && !p.unlinkedLocal() { |
| 168 | a.offboard(w, r) |
| 169 | return |
| 170 | } |
| 171 | next.ServeHTTP(w, r.WithContext(withTeamPerson(r.Context(), p))) |
| 172 | } |
| 173 | |
| 174 | // standingNeed is what a route needs of the roster. |
| 175 | type standingNeed int |
| 176 | |
| 177 | const ( |
| 178 | // needMember: Member. Unknown is a retryable 503. Item pages, the |
| 179 | // add page, the correction page, the AI answer, every write. |
| 180 | needMember standingNeed = iota |
| 181 | // needListing: a list page. Unknown renders it partial, with the |
| 182 | // notice; the handler asks again after building it (teamPartial). |
| 183 | needListing |
| 184 | // needOwner: Member with the roster's owner role (the connectors). |
| 185 | // A member is the same 404 as an unknown route, as before. |
| 186 | needOwner |
| 187 | ) |
| 188 | |
| 189 | // standing is the content routes' gate, inside requireSession. Outside |
| 190 | // team mode it is no gate at all. |
| 191 | func (a *app) standing(need standingNeed) func(http.Handler) http.Handler { |
| 192 | return func(next http.Handler) http.Handler { |
| 193 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 194 | if !a.teamMode() || a.allowUnauthenticated { |
| 195 | next.ServeHTTP(w, r) |
| 196 | return |
| 197 | } |
| 198 | p, ok := teamPersonFrom(r.Context()) |
| 199 | if !ok { |
| 200 | // requireSession puts the person on every request that |
| 201 | // reaches here; a route outside it is a wiring bug, and |
| 202 | // the closed answer is the one that cannot leak. |
| 203 | a.teamUnavailable(w, r) |
| 204 | return |
| 205 | } |
| 206 | switch p.access.State() { |
| 207 | case roster.Member: |
| 208 | if need == needOwner && p.access.Role() != rosterOwner { |
| 209 | if p.access.State() == roster.Unknown { |
| 210 | a.teamUnavailable(w, r) |
| 211 | return |
| 212 | } |
| 213 | a.errorPage(w, r, http.StatusNotFound, kindRoute, "") |
| 214 | return |
| 215 | } |
| 216 | case roster.NotMember: |
| 217 | // Only an unlinked local account gets this far |
| 218 | // (teamSession): it has no standing until it links. |
| 219 | if 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.errorPage(w, r, http.StatusNotFound, kindRoute, "") |
| 225 | return |
| 226 | default: |
| 227 | if need != needListing { |
| 228 | a.teamUnavailable(w, r) |
| 229 | return |
| 230 | } |
| 231 | } |
| 232 | next.ServeHTTP(w, r) |
| 233 | }) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // teamReaches reports whether this request may list the team's memory: |
| 238 | // always outside team mode, and inside it only while the roster says |
| 239 | // Member. Asked BEFORE building a list, so a list is never built on |
| 240 | // standing the request does not have (Sheets' stream-order lesson: read |
| 241 | // standing before deciding reach). |
| 242 | func (a *app) teamReaches(r *http.Request) bool { |
| 243 | if a.allowUnauthenticated && a.previewUnknown { |
| 244 | return false |
| 245 | } |
| 246 | if !a.teamMode() || a.allowUnauthenticated { |
| 247 | return true |
| 248 | } |
| 249 | p, ok := teamPersonFrom(r.Context()) |
| 250 | return ok && p.access.State() == roster.Member |
| 251 | } |
| 252 | |
| 253 | // teamPartial reports whether a list built for this request is |
| 254 | // incomplete because standing is Unknown: Paul's ruling (2026-10-28, |
| 255 | // Home implementation/teammode-package.md §4) is a partial list with a |
| 256 | // notice, not a 503. Asked AFTER building the list, of the same Access: |
| 257 | // State latches, so a lease that lapsed while listing marks the page |
| 258 | // partial — and the caller then withholds what it built, since everything |
| 259 | // Memoria lists stood on the team rung. NotMember is never partial (it |
| 260 | // never reaches a list). Always false outside team mode. |
| 261 | func (a *app) teamPartial(r *http.Request) bool { |
| 262 | if a.allowUnauthenticated && a.previewUnknown { |
| 263 | return true |
| 264 | } |
| 265 | if !a.teamMode() || a.allowUnauthenticated { |
| 266 | return false |
| 267 | } |
| 268 | p, ok := teamPersonFrom(r.Context()) |
| 269 | return !ok || p.access.Partial() |
| 270 | } |
| 271 | |
| 272 | // teamAdmission is admissionOf in team mode: the request's own person if |
| 273 | // requireSession already resolved one, else the session resolved and |
| 274 | // asked about now. It never ends a session itself. |
| 275 | func (a *app) teamAdmission(r *http.Request, s sessions.Session) (viewer, admission) { |
| 276 | p, ok := teamPersonFrom(r.Context()) |
| 277 | if !ok { |
| 278 | if a.team == nil { |
| 279 | return viewer{}, unresolved |
| 280 | } |
| 281 | var err error |
| 282 | p, err = a.teamResolve(r.Context(), s) |
| 283 | switch { |
| 284 | case errors.Is(err, errTeamUnavailable): |
| 285 | return viewer{}, unresolved |
| 286 | case err != nil: |
| 287 | return viewer{}, removed |
| 288 | } |
| 289 | } |
| 290 | if p.access.State() == roster.NotMember && !p.unlinkedLocal() { |
| 291 | return viewer{}, removed |
| 292 | } |
| 293 | label := p.address |
| 294 | if label == "" { |
| 295 | label = homeOnlyLabel(p.subject) |
| 296 | } |
| 297 | return viewer{address: label, method: p.method, ok: true, owner: p.access.Role() == rosterOwner}, admitted |
| 298 | } |
| 299 | |
| 300 | // teamLabel is how a Home-only member, who has no address, is shown where |
| 301 | // an address would be: the account menu. |
| 302 | const teamLabel = "Oficina account" |
| 303 | |
| 304 | // homeOnlyLabel is teamLabel and the last four characters of the person |
| 305 | // ref ("Oficina account …k7qz"), as Home's Team page shows a ref. |
| 306 | func homeOnlyLabel(subject string) string { |
| 307 | if len(subject) <= 4 { |
| 308 | return teamLabel |
| 309 | } |
| 310 | return teamLabel + " …" + subject[len(subject)-4:] |
| 311 | } |
| 312 | |
| 313 | // PreviewUnknownStanding makes the lists render as they do in team mode |
| 314 | // under Unknown standing — partial, with the notice — so tools/drive can |
| 315 | // show the pages a browser would. AppForTest only: it does nothing unless |
| 316 | // the app was built through that constructor, and production never calls |
| 317 | // it (TestProductionDoesNotUseTheTestConstructor). |
| 318 | func (a *app) PreviewUnknownStanding() { a.previewUnknown = true } |
| 319 | |