| 1 | package calls |
| 2 | |
| 3 | // The sign-in door (Home spec 2026-11-19-smart-door.md, step 6; home/connect |
| 4 | // MIGRATING.md): rastrillo's sign-in screen, its ui "signin" partial fed by |
| 5 | // auth.SigninState, drawn inside estilo's door by estilo-signin-screen. Its |
| 6 | // words are rastrillo's approved copy (rastrillo.ui.signin_*), and the |
| 7 | // signed-out page's are estilo's chrome.English, so every app in the suite |
| 8 | // says the same thing. Only the refusal and the removal are Calls' |
| 9 | // (signinRefusal, signinRemoved). |
| 10 | // |
| 11 | // Guests are not the door's business. A guest joins a call by name at the |
| 12 | // invitation (/j/{token}, guest.go), and nothing here stands in front of |
| 13 | // that. A guest session that reaches the door is drawn the door as anybody |
| 14 | // signed out is (signing in is exactly what a guest here is asking to do), |
| 15 | // and it is never refused, remembered or ended here (signin_admission.go). |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "html" |
| 20 | "net/http" |
| 21 | "net/url" |
| 22 | "strings" |
| 23 | |
| 24 | "amadan.net/oficina/estilo.git/chrome" |
| 25 | "amadan.net/oficina/estilo.git/sky" |
| 26 | "amadan.net/oficina/home/connect" |
| 27 | rauth "amadan.net/rastrillo/rastrillo/auth" |
| 28 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 29 | ) |
| 30 | |
| 31 | // signinForgetPath is where rastrillo auth's Forget is mounted (app.go): |
| 32 | // "Use a different email" on the door, and "Forget me on this device" on |
| 33 | // the signed-out page. It is auth's own default, named here because the |
| 34 | // signed-out page and connect's RoutesConfig.ForgetPath must agree with it. |
| 35 | const signinForgetPath = "/signin/forget" |
| 36 | |
| 37 | // oficinaChain is the hidden field the signed-out page's Forget form posts |
| 38 | // (chain=oficina) when Home is configured: after forgetting this browser |
| 39 | // here, Calls sends it on to Oficina ID's own Forget (forget, below). The |
| 40 | // door's "Use a different email" posts to the same Forget without it, and |
| 41 | // stays on the door. |
| 42 | const oficinaChain = "oficina" |
| 43 | |
| 44 | // oficinaIDName is the suite's sign-in service, the federated door's name: |
| 45 | // the same in every language, like the door's own "Calls", and rastrillo |
| 46 | // draws it as "Continue with {name}". |
| 47 | const oficinaIDName = "Oficina ID" |
| 48 | |
| 49 | // signinRefusal is the refusal (refused, and a session the signed-in |
| 50 | // routes no longer admit), in the screen's app-problem slot with the email |
| 51 | // field. Paul approved the wording for Docs and Sheets (2026-11-21/22): |
| 52 | // the same sentence with the app's name. |
| 53 | const signinRefusal = "That email can’t sign in to Calls. Use a different email, or ask whoever runs this team to add it." |
| 54 | |
| 55 | // signinRemoved is the removal: endRemovedSession's landing, and a session |
| 56 | // the signed-in routes end as removed that reaches the door. Calls' own |
| 57 | // sentence, unchanged; only its place on the door moved (the app-problem |
| 58 | // slot, where rastrillo would otherwise draw its generic problem). |
| 59 | const signinRemoved = "Your access has been removed, so you’ve been signed out. Ask whoever runs this team if that’s wrong." |
| 60 | |
| 61 | // doorIcon is Calls' mark at the head of every door card. |
| 62 | func doorIcon() string { return assets.Path("static/brand/icon.svg") } |
| 63 | |
| 64 | // signinDoor is the sign-in door: standalone GET /signin, and with Home |
| 65 | // configured GET /signin and /signin/local (connect's SigninDoor and |
| 66 | // SigninLocal hand both here, RoutesConfig.LocalSignin), plus auth's |
| 67 | // RenderConfirm (the confirm step) and Refused (the refusal). |
| 68 | // |
| 69 | // rastrillo decides what the door shows from the query and this browser's |
| 70 | // cookies (SigninState: the way in it used last, Sent, problems, the |
| 71 | // chooser), with SigninOneWay drawing one way in per page. Calls adds what |
| 72 | // only it knows: Oficina ID where connect says the door may offer it |
| 73 | // (OficinaHref is "" on /signin/local, on a step-up and standalone), the |
| 74 | // email way first while an invitation waits (joining), the refusal, which |
| 75 | // only someone who has just verified the address, or whose session the |
| 76 | // signed-in routes no longer admit, reaches, and the removal. |
| 77 | func (a *app) signinDoor(w http.ResponseWriter, r *http.Request) { |
| 78 | a.signinDoorStatus(w, r, http.StatusOK) |
| 79 | } |
| 80 | |
| 81 | func (a *app) signinDoorStatus(w http.ResponseWriter, r *http.Request, status int) { |
| 82 | st := a.auth.SigninState(r) |
| 83 | if st.Unavailable { |
| 84 | // Under a base path or on a direct host the hub signs people in: |
| 85 | // estilo-signin-screen draws nothing, and every route rastrillo's |
| 86 | // door names answers 404 there. So does the door. |
| 87 | a.notFound(w, r) |
| 88 | return |
| 89 | } |
| 90 | if href := connect.OficinaHref(r); href != "" { |
| 91 | st.Federated = &rauth.FederatedDoor{ |
| 92 | Provider: lastsignin.ProviderOficina, |
| 93 | Href: href, |
| 94 | Label: oficinaIDName, |
| 95 | Icon: chrome.OficinaGlyph, |
| 96 | } |
| 97 | } |
| 98 | if joining(r) && st.Way == "" { |
| 99 | // Paul's ruling (2026-11-22, #46): on the way into a call, email |
| 100 | // is the door's default, whatever this browser used last, with |
| 101 | // Oficina ID a row under Other ways. An outsider gets in by the |
| 102 | // pending join an emailed link proves (spec §3.2). |
| 103 | st.Way = rauth.WayEmail |
| 104 | } |
| 105 | switch { |
| 106 | case refusedHere(r): |
| 107 | st.AppProblem = &rauth.AppProblem{Text: signinRefusal, Ask: true} |
| 108 | case removedHere(r): |
| 109 | // rastrillo knows no err=removed (it reads an unknown err as its |
| 110 | // generic problem), so the sentence is Calls', in place of it. |
| 111 | st.Problem = rauth.ProblemNone |
| 112 | st.AppProblem = &rauth.AppProblem{Text: signinRemoved, Ask: true} |
| 113 | } |
| 114 | a.rememberSigninReturn(w, r, &st) |
| 115 | a.auth.PrepareSigninResponse(w, st) |
| 116 | screen := chrome.SignInScreen{Icon: doorIcon(), App: appName, Sky: sky.For(r.Host), State: st} |
| 117 | title, err := signinTitle(screen) |
| 118 | if err != nil { |
| 119 | a.logger.Error("calls: render the sign-in title", "err", err) |
| 120 | a.ErrorPage(w, r, http.StatusInternalServerError, "") |
| 121 | return |
| 122 | } |
| 123 | w.Header().Set("Cache-Control", "no-store") |
| 124 | if status != http.StatusOK { |
| 125 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 126 | w.WriteHeader(status) |
| 127 | } |
| 128 | render(w, "signin", a.page(r, frame{title: title, signinPage: true}, map[string]any{"Screen": screen})) |
| 129 | } |
| 130 | |
| 131 | // signinTitle is the door's <title>: estilo's estilo-signin-screen-title, |
| 132 | // which is rastrillo's signin-title for the same state, so the tab and the |
| 133 | // heading always say the same thing ("Sign in to Calls", "Check your |
| 134 | // email to sign in to Calls"). The template writes escaped text and the |
| 135 | // layout escapes the title again, so it is unescaped here. |
| 136 | func signinTitle(screen chrome.SignInScreen) (string, error) { |
| 137 | var b strings.Builder |
| 138 | if err := pages["signin"].ExecuteTemplate(&b, "estilo-signin-screen-title", screen); err != nil { |
| 139 | return "", err |
| 140 | } |
| 141 | return strings.TrimSpace(html.UnescapeString(b.String())), nil |
| 142 | } |
| 143 | |
| 144 | // renderConfirm is auth's RenderConfirm, the page an emailed link opens on |
| 145 | // (GET /auth/verify): the door, whose SigninState answers StepConfirm on |
| 146 | // this request alone, with one Sign in button that POSTs the token back, |
| 147 | // where auth spends it. The GET spends nothing, so a mail-security scanner |
| 148 | // that fetches every link cannot sign anybody in. auth has already set |
| 149 | // no-store, Referrer-Policy strict-origin (the URL is a live credential) |
| 150 | // and the framing refusals; nothing here loosens them. |
| 151 | func (a *app) renderConfirm(w http.ResponseWriter, r *http.Request, _ rauth.ConfirmPageData) { |
| 152 | a.signinDoor(w, r) |
| 153 | } |
| 154 | |
| 155 | // refused is auth's Refused hook: Keymail or an emailed link verified the |
| 156 | // address and authorize said no (neither on the allowlist nor holding a |
| 157 | // live pending join). It answers with the door, carrying the refusal, in a |
| 158 | // 403: no session is minted, and auth has already ended the attempt. |
| 159 | // |
| 160 | // Drawn in this response, never from ?err=refused: a query parameter is |
| 161 | // something anybody can type, and the sentence means "you proved this |
| 162 | // address and Calls does not admit it" (Astra on #25, round 3). Only this |
| 163 | // hook can say it, and auth calls it only after the address is proved, so |
| 164 | // saying it is no membership oracle. |
| 165 | func (a *app) refused(w http.ResponseWriter, r *http.Request, _ rauth.Identity) { |
| 166 | a.signinDoorStatus(w, withRefusal(r), http.StatusForbidden) |
| 167 | } |
| 168 | |
| 169 | // refusedKey and removedKey mark a request the door draws with the refusal |
| 170 | // or the removal; joiningKey one drawn while an invitation waits. |
| 171 | type ( |
| 172 | refusedKey struct{} |
| 173 | removedKey struct{} |
| 174 | joiningKey struct{} |
| 175 | ) |
| 176 | |
| 177 | // withRefusal marks r as a refusal the door draws: a verified address |
| 178 | // authorize refused (refused), or a session the signed-in routes no longer |
| 179 | // admit (refusedSession). |
| 180 | // |
| 181 | // Only on the plain door: the chooser (?ways=) and a way's own page |
| 182 | // (?way=) are how a refused person reaches another way in, another Oficina |
| 183 | // ID account included, and a refusal there would draw the field again and |
| 184 | // leave "Other ways to sign in" going nowhere (Fable on Docs #139, N1). |
| 185 | func withRefusal(r *http.Request) *http.Request { |
| 186 | if choosing(r) { |
| 187 | return r |
| 188 | } |
| 189 | return r.WithContext(context.WithValue(r.Context(), refusedKey{}, true)) |
| 190 | } |
| 191 | |
| 192 | // withRemoval is withRefusal for a session the signed-in routes end as |
| 193 | // removed (endRemovedSession). |
| 194 | func withRemoval(r *http.Request) *http.Request { |
| 195 | if choosing(r) { |
| 196 | return r |
| 197 | } |
| 198 | return r.WithContext(context.WithValue(r.Context(), removedKey{}, true)) |
| 199 | } |
| 200 | |
| 201 | // choosing reports whether r is the chooser or a way's own page. |
| 202 | func choosing(r *http.Request) bool { |
| 203 | q := r.URL.Query() |
| 204 | return q.Has("ways") || q.Has("way") |
| 205 | } |
| 206 | |
| 207 | // refusedHere reports whether r is a refusal the door is drawing. Never |
| 208 | // from the query: a query parameter is something anybody can type. |
| 209 | func refusedHere(r *http.Request) bool { |
| 210 | v, _ := r.Context().Value(refusedKey{}).(bool) |
| 211 | return v |
| 212 | } |
| 213 | |
| 214 | // removedHere reports whether the door is drawing the removal: |
| 215 | // endRemovedSession's landing (?err=removed), or a session the probe found |
| 216 | // removed. On the plain door only, like the refusal. The query is read, as |
| 217 | // it always was: the sentence names no address and tells nobody anything |
| 218 | // about one. |
| 219 | func removedHere(r *http.Request) bool { |
| 220 | if v, _ := r.Context().Value(removedKey{}).(bool); v { |
| 221 | return true |
| 222 | } |
| 223 | return r.URL.Query().Get("err") == "removed" && !choosing(r) |
| 224 | } |
| 225 | |
| 226 | // joining reports whether the door is being drawn for somebody on their |
| 227 | // way into a call through an invitation (the join cookie, joincookie.go): |
| 228 | // email is the door's default way, and Oficina ID one of its other ways |
| 229 | // (signinRoute; Paul's ruling, 2026-11-22). |
| 230 | func joining(r *http.Request) bool { |
| 231 | v, _ := r.Context().Value(joiningKey{}).(bool) |
| 232 | return v |
| 233 | } |
| 234 | |
| 235 | // withJoining marks r as joining. |
| 236 | func withJoining(r *http.Request) *http.Request { |
| 237 | return r.WithContext(context.WithValue(r.Context(), joiningKey{}, true)) |
| 238 | } |
| 239 | |
| 240 | // renderSignedOut is where Sign out lands: /signed-out, in both modes, in |
| 241 | // the door's words: "You’re signed out", Sign in, then the quiet "Forget |
| 242 | // me on this device" (the smart-door spec's "Signed out"), a POST to |
| 243 | // rastrillo auth's Forget. With Home configured the page is connect's |
| 244 | // SignedOut, which carries that path (RoutesConfig.ForgetPath) for PageFor, |
| 245 | // and the form also asks Calls to go on to Oficina ID's own Forget once |
| 246 | // this one has answered (forget). Standalone it is Calls' own path, with |
| 247 | // no chain. |
| 248 | func (a *app) renderSignedOut(w http.ResponseWriter, r *http.Request, status int) { |
| 249 | p := connect.PageFor(r, "signedout", "") |
| 250 | signin, forget, chain := p.Action, p.ForgetAction, []chrome.Hidden{{Name: "chain", Value: oficinaChain}} |
| 251 | if signin == "" { |
| 252 | signin = signinPath |
| 253 | } |
| 254 | if forget == "" { |
| 255 | forget, chain = signinForgetPath, nil |
| 256 | } |
| 257 | notice := chrome.SignInNotice{Icon: doorIcon(), App: appName, Sky: sky.For(r.Host)}. |
| 258 | SignedOut(signin).Forgetting(forget, "", chain...) |
| 259 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 260 | w.Header().Set("Cache-Control", "no-store") |
| 261 | w.WriteHeader(status) |
| 262 | render(w, "signedout", a.page(r, frame{parts: []string{notice.Page()}, signinPage: true}, map[string]any{"Notice": notice})) |
| 263 | } |
| 264 | |
| 265 | // signedOut serves GET /signed-out standalone. With Home configured |
| 266 | // connect serves it, through renderHome. |
| 267 | func (a *app) signedOut(w http.ResponseWriter, r *http.Request) { |
| 268 | a.renderSignedOut(w, r, http.StatusOK) |
| 269 | } |
| 270 | |
| 271 | // forget is POST /signin/forget, rastrillo auth's Forget: "Use a different |
| 272 | // email" on the door, and "Forget me on this device" on the signed-out |
| 273 | // page. Each host remembers for itself, so with Home configured the |
| 274 | // signed-out page's form (chain=oficina) goes on to Oficina ID's own Forget |
| 275 | // once this one has answered, with Calls' signed-out page as the way back: |
| 276 | // {issuer}/forget?return=<origin>/signed-out. id.oficina.works asks "Forget |
| 277 | // me on Oficina ID too?" and returns only to a registered consumer's exact |
| 278 | // /signed-out. Anything but Forget's own redirect (a cross-origin refusal, |
| 279 | // a wrong method) is passed on as it is. A page a sign-in in progress kept |
| 280 | // (signin_return.go) is forgotten too. |
| 281 | func (a *app) forget(w http.ResponseWriter, r *http.Request) { |
| 282 | if r.Method == http.MethodPost { |
| 283 | a.forgetReturn(w) |
| 284 | } |
| 285 | if a.home == nil { |
| 286 | a.auth.Forget(w, r) |
| 287 | return |
| 288 | } |
| 289 | result := &bufferedResponse{header: make(http.Header)} |
| 290 | a.auth.Forget(result, r) |
| 291 | if result.status == http.StatusSeeOther && r.PostFormValue("chain") == oficinaChain { |
| 292 | result.header.Set("Location", oficinaForgetURL(a.cfg.home.Issuer, a.cfg.Origin)) |
| 293 | } |
| 294 | result.replay(w) |
| 295 | } |
| 296 | |
| 297 | // oficinaForgetURL is Oficina ID's Forget, returning to this origin's |
| 298 | // signed-out page (Home's internal/home/door.go, forgetChainReturn). |
| 299 | func oficinaForgetURL(issuer, origin string) string { |
| 300 | back := strings.TrimSuffix(origin, "/") + "/signed-out" |
| 301 | return strings.TrimSuffix(issuer, "/") + "/forget?" + url.Values{"return": {back}}.Encode() |
| 302 | } |
| 303 | |
| 304 | // standaloneSignout is rastrillo's Sign out, landing on /signed-out |
| 305 | // (signedOutAfter), which also forgets a page a sign-in in progress kept: |
| 306 | // it was this person's, not the next one's. |
| 307 | func (a *app) standaloneSignout(signout http.HandlerFunc) http.HandlerFunc { |
| 308 | landing := signedOutAfter(signout) |
| 309 | return func(w http.ResponseWriter, r *http.Request) { |
| 310 | a.forgetReturn(w) |
| 311 | landing(w, r) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // homeSignout is connect's Sign out (its 307 on to Oficina ID's, which |
| 316 | // the native app's SignOut reads too), which also forgets a page a sign-in |
| 317 | // in progress kept, for the same reason. |
| 318 | func (a *app) homeSignout(w http.ResponseWriter, r *http.Request) { |
| 319 | a.forgetReturn(w) |
| 320 | a.home.routes.Signout(w, r) |
| 321 | } |
| 322 | |