| 1 | package calls |
| 2 | |
| 3 | // The door's admission check (home/connect MIGRATING.md, step 6, item 2): |
| 4 | // before /signin, /signin/local or /signin/oficina may treat a live session |
| 5 | // as signed in, the gate every signed-in page runs is asked about it, on a |
| 6 | // probe. A session it refuses gets the door with the refusal (or the |
| 7 | // removal) and is ended; one it cannot decide on is left alone. And the |
| 8 | // smart door's memory of an Oficina ID sign-in, written only for a session |
| 9 | // that gate admits. |
| 10 | // |
| 11 | // What the door judges, and what it never touches: |
| 12 | // |
| 13 | // - A web sign-in (an emailed link, Keymail, a Home sign-in) is judged, |
| 14 | // as the old /signin checks (signin, teamSignin) judged it. |
| 15 | // - A guest's session (method "guest", minted by joining a call by name) |
| 16 | // is never judged, refused, remembered or ended here: the door is drawn |
| 17 | // for it as for anybody signed out, because signing in is what a guest |
| 18 | // at the door is asking to do, and the cookie is left as it is. |
| 19 | // connect's SigninDoor reads a guest as signed out for the same reason. |
| 20 | // - The native apps hold sessions of their own: native_auth.go mints the |
| 21 | // app its own session row, a copy of the browser's, and callscore |
| 22 | // never follows a redirect, so the app's cookie never reaches a door. |
| 23 | // The door only ever ends the one session the browser presented |
| 24 | // (refusedSession), so no native session is refused or ended by it. |
| 25 | |
| 26 | import ( |
| 27 | "context" |
| 28 | "net/http" |
| 29 | "net/url" |
| 30 | |
| 31 | "amadan.net/oficina/home/connect" |
| 32 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 33 | ) |
| 34 | |
| 35 | // signinRoute is GET /signin: the door. With Home, connect's SigninDoor |
| 36 | // behind the admission check; standalone, standaloneSignin. |
| 37 | // |
| 38 | // The join cookie (joincookie.go: an invitation this browser followed) |
| 39 | // opens the door on the email way, whatever the browser used last, with |
| 40 | // Oficina ID under Other ways (Paul's ruling, 2026-11-22, #46). An |
| 41 | // outsider is let in by the pending join the emailed link proves (spec |
| 42 | // §3.2); a member may use either, and an Oficina ID sign-in lands back on |
| 43 | // the invitation (oficinaJoinLanding). Before the smart door this was a |
| 44 | // call straight to the local form, email only. |
| 45 | func (a *app) signinRoute(w http.ResponseWriter, r *http.Request) { |
| 46 | if a.home == nil { |
| 47 | a.standaloneSignin(w, r) |
| 48 | return |
| 49 | } |
| 50 | if _, pending := a.readJoinCookie(r); pending { |
| 51 | r = withJoining(r) |
| 52 | } |
| 53 | a.homeDoor(a.home.routes.SigninDoor)(w, r) |
| 54 | } |
| 55 | |
| 56 | // oficinaJoinLanding is GET /signin/oficina while a join cookie is live: |
| 57 | // the Home exchange lands on /continue, as an emailed link does, instead |
| 58 | // of the page return_to named, so the sign-in goes on into the invitation |
| 59 | // the cookie names (continueInvitation). /continue is a fixed local path, |
| 60 | // and the invitation is read from the sealed cookie by its hash, so no |
| 61 | // destination or token is taken from the request. A step-up is left to |
| 62 | // SigninOficina, which sends it to the email-only door. |
| 63 | func (a *app) oficinaJoinLanding(next http.HandlerFunc) http.HandlerFunc { |
| 64 | return func(w http.ResponseWriter, r *http.Request) { |
| 65 | q := r.URL.Query() |
| 66 | if _, pending := a.readJoinCookie(r); pending && !q.Has("reauth") { |
| 67 | q.Set("return_to", "/continue") |
| 68 | r = r.Clone(r.Context()) |
| 69 | r.URL.RawQuery = q.Encode() |
| 70 | r.RequestURI = r.URL.RequestURI() |
| 71 | } |
| 72 | next(w, r) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // homeDoor is one of connect's three door routes as Calls mounts it: under |
| 77 | // Home's session middleware, which clears a cookie whose session is |
| 78 | // definitively gone (and leaves it on a storage failure), so a stale cookie |
| 79 | // cannot stand in the way of the exchange (connect's Start refuses a |
| 80 | // session cookie that does not resolve); then the admission check. |
| 81 | func (a *app) homeDoor(next http.HandlerFunc) http.HandlerFunc { |
| 82 | return a.home.sessions.Middleware(a.admittedOnly(next)).ServeHTTP |
| 83 | } |
| 84 | |
| 85 | // standaloneSignin is GET /signin without Home: somebody already signed in |
| 86 | // and admitted never sees the door (spec shared-chrome §9.4) unless the |
| 87 | // query names one of its states, and goes to a safe return_to or "/". A |
| 88 | // session the signed-in routes refuse gets the door with the refusal, as |
| 89 | // with Home (admittedOnly). Standalone Calls has only email, so the door is |
| 90 | // the email way. |
| 91 | func (a *app) standaloneSignin(w http.ResponseWriter, r *http.Request) { |
| 92 | if ad := a.admission(r, false); ad.signedIn { |
| 93 | if !ad.admitted { |
| 94 | r = a.refusedSession(w, r, ad.removed) |
| 95 | } else if !signinOutcome(r) { |
| 96 | alreadySignedIn(w, r) |
| 97 | return |
| 98 | } |
| 99 | } |
| 100 | a.signinDoor(w, r) |
| 101 | } |
| 102 | |
| 103 | // admittedOnly runs connect's SigninDoor, SigninLocal or SigninOficina for |
| 104 | // a browser whose session the signed-in routes would admit, and as signed |
| 105 | // out for any other. SigninDoor and SigninOficina take any live session |
| 106 | // cookie as signed in and send it straight to the page; a session the |
| 107 | // signed-in routes refuse (a Home link to a deleted person, an address no |
| 108 | // longer a member, somebody the roster says is off the team) would then go |
| 109 | // round /signin, the page, the refusal and back (Astra on Docs #65, Sheets |
| 110 | // #39), so they are not shown that cookie. SigninLocal is wrapped too, for |
| 111 | // the refusals that land there. |
| 112 | func (a *app) admittedOnly(next http.HandlerFunc) http.HandlerFunc { |
| 113 | return func(w http.ResponseWriter, r *http.Request) { |
| 114 | if ad := a.admission(r, false); ad.signedIn && !ad.admitted { |
| 115 | r = withoutCookie(a.refusedSession(w, r, ad.removed), a.sessionCookie()) |
| 116 | } |
| 117 | next(w, r) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // refusedSession answers for a session the probe refused: it is ended and |
| 122 | // its cookie cleared on this response, so a reload is plainly signed out |
| 123 | // rather than a dead cookie over the plain door; a remembered Oficina ID is |
| 124 | // forgotten, because offered again it would sign the same refused person |
| 125 | // in and send them back here; and the door is marked to say why: the |
| 126 | // refusal, or for a session the signed-in routes end as removed the |
| 127 | // removal, in the screen's app-problem slot with the email field. A |
| 128 | // remembered email way is kept: it is no loop. |
| 129 | func (a *app) refusedSession(w http.ResponseWriter, r *http.Request, removed bool) *http.Request { |
| 130 | a.nativeAuth.sessions.SignOut(w, r) |
| 131 | w.Header().Set("Cache-Control", "no-store") |
| 132 | jar := a.auth.RememberJar() |
| 133 | if rec, res := jar.Read(r); res == lastsignin.Valid && rec.Method == lastsignin.MethodFederated { |
| 134 | jar.Clear(w) |
| 135 | } |
| 136 | if removed { |
| 137 | return withRemoval(r) |
| 138 | } |
| 139 | return withRefusal(r) |
| 140 | } |
| 141 | |
| 142 | // admissionResult is what the probe found: whether r holds a web sign-in |
| 143 | // at all, whether the signed-in routes let it in, and whether they ended it |
| 144 | // as removed (endRemovedSession). |
| 145 | type admissionResult struct { |
| 146 | signedIn, admitted, removed bool |
| 147 | } |
| 148 | |
| 149 | // admission runs the gate every signed-in page runs (rastrillo auth's |
| 150 | // RequireSession, then requirePerson: identity, a Home link and whether |
| 151 | // it still resolves to a member, or in team mode the roster) around a |
| 152 | // probe GET / whose answer is thrown away. A session is not enough: a |
| 153 | // Home link to a person who has gone still holds a valid one. The probe |
| 154 | // ends nothing itself (endRemovedSession skips its sign-out under it): |
| 155 | // the door's caller does, for a session it decides to refuse. |
| 156 | // |
| 157 | // requireMember is not part of it: in Calls a signed-in non-member is no |
| 158 | // refusal but a guest-like participant, who lands on /welcome and joins |
| 159 | // calls through their invitations, and the old /signin checks never asked |
| 160 | // it either. |
| 161 | // |
| 162 | // A gate that cannot decide (a 5xx: storage or the roster unavailable) |
| 163 | // counts as admitted unless strict: the door must not tell somebody they |
| 164 | // cannot sign in, nor sign them out, because a read failed, and nothing is |
| 165 | // remembered on one. |
| 166 | func (a *app) admission(r *http.Request, strict bool) admissionResult { |
| 167 | probe := r.Clone(context.WithValue(r.Context(), probingKey{}, true)) |
| 168 | probe.Method = http.MethodGet |
| 169 | probe.URL = &url.URL{Path: "/"} |
| 170 | probe.RequestURI = "/" |
| 171 | probe.Body = http.NoBody |
| 172 | // The session is read from the probe's own cookie, never from the |
| 173 | // context a middleware filled: admittedAs asks about a session minted |
| 174 | // on this very response. |
| 175 | s, ok := a.nativeAuth.sessions.From(probe) |
| 176 | if !ok || !doorJudges(s.Method) { |
| 177 | // Signed out, or a session that is not a web sign-in (a guest): |
| 178 | // the door is drawn, and nothing is refused or ended. |
| 179 | return admissionResult{} |
| 180 | } |
| 181 | reached := false |
| 182 | gate := a.auth.RequireSession(a.requirePerson(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached = true }))) |
| 183 | rec := &bufferedResponse{header: make(http.Header)} |
| 184 | gate.ServeHTTP(rec, probe) |
| 185 | undecided := rec.status >= http.StatusInternalServerError |
| 186 | return admissionResult{ |
| 187 | signedIn: true, |
| 188 | admitted: reached || (!strict && undecided), |
| 189 | removed: !reached && rec.status == http.StatusSeeOther && removedRedirect(rec.header.Get("Location")), |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // doorJudges reports whether the door's probe judges a session by method: |
| 194 | // the three requirePerson speaks for as a sign-in (an emailed link, |
| 195 | // Keymail, a Home sign-in on the web), as signin and teamSignin did. A |
| 196 | // guest is never judged by the door. |
| 197 | func doorJudges(method string) bool { |
| 198 | return method == connect.HomeMethod || method == "magiclink" || method == "keymail" |
| 199 | } |
| 200 | |
| 201 | // removedRedirect reports whether location is endRemovedSession's: the |
| 202 | // door with err=removed. |
| 203 | func removedRedirect(location string) bool { |
| 204 | u, err := url.Parse(location) |
| 205 | return err == nil && u.Path == signinPath && u.Query().Get("err") == "removed" |
| 206 | } |
| 207 | |
| 208 | // probingKey marks the admission probe's request, so the gate it runs |
| 209 | // answers without ending anything (endRemovedSession). |
| 210 | type probingKey struct{} |
| 211 | |
| 212 | // probing reports whether ctx is the admission probe's. |
| 213 | func probing(ctx context.Context) bool { |
| 214 | v, _ := ctx.Value(probingKey{}).(bool) |
| 215 | return v |
| 216 | } |
| 217 | |
| 218 | // withoutCookie is r, as seen by a handler that must not see the named |
| 219 | // cookie. Only admittedOnly and admittedAs use it. |
| 220 | func withoutCookie(r *http.Request, name string) *http.Request { |
| 221 | c := r.Clone(r.Context()) |
| 222 | c.Header.Del("Cookie") |
| 223 | for _, cookie := range r.Cookies() { |
| 224 | if cookie.Name != name { |
| 225 | c.AddCookie(cookie) |
| 226 | } |
| 227 | } |
| 228 | return c |
| 229 | } |
| 230 | |
| 231 | // sessionCookie is the session cookie's name: auth's, Home's and the |
| 232 | // native hand-off's are the same table and the same cookie. |
| 233 | func (a *app) sessionCookie() string { return a.auth.SessionCookie() } |
| 234 | |
| 235 | // rememberOficina is the smart door's memory of a Home sign-in, written in |
| 236 | // MintHomeSession once the session is stored, so it is written only where |
| 237 | // connect turns a Home sign-in into a session here (a finished exchange, |
| 238 | // "I'm new here"), never on link, choose, unknown or failed. The record is |
| 239 | // Oficina ID, with the address of the person connect resolved for this |
| 240 | // very sign-in (connect.MintedPerson), or none for a Home-only member. |
| 241 | // Nothing is carried over from the record it replaces: that address could |
| 242 | // be somebody else's. Remembering is a convenience, and Remember fails |
| 243 | // silently, so it can never fail the sign-in. |
| 244 | // |
| 245 | // Only for a session the signed-in routes admit, asked of the session just |
| 246 | // minted (admittedAs) and strictly: connect mints for any linked person, |
| 247 | // and a record for a refused one would offer Oficina ID again, round a |
| 248 | // loop of sign-in, refusal and door (Fable on Docs #139, A2). |
| 249 | func (a *app) rememberOficina(w http.ResponseWriter, r *http.Request) { |
| 250 | if a.auth == nil || a.home == nil || a.nativeAuth == nil { |
| 251 | return |
| 252 | } |
| 253 | if !a.admission(a.admittedAs(w, r), true).admitted { |
| 254 | return |
| 255 | } |
| 256 | address := "" |
| 257 | if p, ok := connect.MintedPerson(r); ok { |
| 258 | address = p.Label |
| 259 | } |
| 260 | a.auth.RememberJar().Remember(w, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: address}) |
| 261 | } |
| 262 | |
| 263 | // admittedAs is r as the session MintHomeSession just set on w would send |
| 264 | // it: the request's own cookies, with that session's in place of any other. |
| 265 | func (a *app) admittedAs(w http.ResponseWriter, r *http.Request) *http.Request { |
| 266 | name := a.sessionCookie() |
| 267 | var minted *http.Cookie |
| 268 | for _, c := range (&http.Response{Header: w.Header()}).Cookies() { |
| 269 | if c.Name == name && c.MaxAge >= 0 && c.Value != "" { |
| 270 | minted = c |
| 271 | } |
| 272 | } |
| 273 | probe := withoutCookie(r, name) |
| 274 | if minted != nil { |
| 275 | probe.AddCookie(&http.Cookie{Name: minted.Name, Value: minted.Value}) |
| 276 | } |
| 277 | return probe |
| 278 | } |
| 279 | |
| 280 | // signinOutcome reports whether GET /signin names a state of the door |
| 281 | // rather than opening it afresh: an outcome of the email flow (?sent, |
| 282 | // ?err, ?force), the Keymail continuation (?continue) or a step-up |
| 283 | // (?reauth). connect's SigninDoor draws the same five for a signed-in |
| 284 | // browser too (its doorStates). |
| 285 | func signinOutcome(r *http.Request) bool { |
| 286 | q := r.URL.Query() |
| 287 | return q.Has("sent") || q.Has("err") || q.Has("force") || q.Has("continue") || q.Has("reauth") |
| 288 | } |
| 289 | |
| 290 | // alreadySignedIn sends a signed-in GET /signin back where it came from, |
| 291 | // with no door: 303 to a safe return_to, or "/". |
| 292 | func alreadySignedIn(w http.ResponseWriter, r *http.Request) { |
| 293 | target := "/" |
| 294 | if path, ok := requestedReturn(r); ok { |
| 295 | target = path |
| 296 | } |
| 297 | w.Header().Set("Cache-Control", "no-store") |
| 298 | http.Redirect(w, r, target, http.StatusSeeOther) |
| 299 | } |
| 300 | |
| 301 | // chooseJoinLanding is the member choice's POST while a join cookie is |
| 302 | // live: connect lands "I'm new here" on "/", whatever the exchange was |
| 303 | // started for, so that one redirect goes to /continue instead, which |
| 304 | // follows the invitation (continueInvitation), as oficinaJoinLanding does |
| 305 | // for every other Oficina ID sign-in. Any other answer is passed on. |
| 306 | func (a *app) chooseJoinLanding(next http.HandlerFunc) http.HandlerFunc { |
| 307 | return func(w http.ResponseWriter, r *http.Request) { |
| 308 | if _, pending := a.readJoinCookie(r); !pending { |
| 309 | next(w, r) |
| 310 | return |
| 311 | } |
| 312 | result := &bufferedResponse{header: make(http.Header)} |
| 313 | next(result, r) |
| 314 | if result.status == http.StatusSeeOther && result.header.Get("Location") == "/" { |
| 315 | result.header.Set("Location", "/continue") |
| 316 | } |
| 317 | result.replay(w) |
| 318 | } |
| 319 | } |
| 320 | |