| 1 | package slides |
| 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 this app's |
| 9 | // (slides.signin.error.*). |
| 10 | |
| 11 | import ( |
| 12 | "context" |
| 13 | "html" |
| 14 | "net/http" |
| 15 | "net/url" |
| 16 | "strings" |
| 17 | |
| 18 | "amadan.net/oficina/estilo.git/brand" |
| 19 | "amadan.net/oficina/estilo.git/chrome" |
| 20 | "amadan.net/oficina/estilo.git/sky" |
| 21 | "amadan.net/oficina/home/connect" |
| 22 | "amadan.net/rastrillo/rastrillo" |
| 23 | rauth "amadan.net/rastrillo/rastrillo/auth" |
| 24 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 25 | ) |
| 26 | |
| 27 | // signinForgetPath is where rastrillo auth's Forget is mounted (app.go): |
| 28 | // "Use a different email" on the door, and "Forget me on this device" on |
| 29 | // the signed-out page. It is auth's own default, named here because the |
| 30 | // signed-out page and connect's RoutesConfig.ForgetPath must agree with it. |
| 31 | const signinForgetPath = "/signin/forget" |
| 32 | |
| 33 | // oficinaChain is the hidden field the signed-out page's Forget form posts |
| 34 | // (chain=oficina) when Home is configured: after forgetting this browser |
| 35 | // here, Slides sends it on to Oficina ID's own Forget (forget, below). The |
| 36 | // door's "Use a different email" posts to the same Forget without it, and |
| 37 | // stays on the door. |
| 38 | const oficinaChain = "oficina" |
| 39 | |
| 40 | // oficinaIDName is the suite's sign-in service, the federated door's name: |
| 41 | // the same in every language, like the door's own "Slides", and rastrillo |
| 42 | // draws it as "Continue with {name}". |
| 43 | const oficinaIDName = "Oficina ID" |
| 44 | |
| 45 | // signinView is the sign-in door's view data (templates/signin.html). |
| 46 | // Every state is that one template: the door, the chooser ("Other ways to |
| 47 | // sign in"), Sent, the Keymail continuation, a problem or refusal, and the |
| 48 | // confirm step an emailed link opens. |
| 49 | type signinView struct { |
| 50 | shell |
| 51 | Screen chrome.SignInScreen |
| 52 | } |
| 53 | |
| 54 | // standaloneSignin is GET /signin without Home: somebody already signed in |
| 55 | // and admitted never sees the door (spec §9.2) unless the query names one |
| 56 | // of its states, and goes to a safe return_to or the deck list. A session |
| 57 | // the signed-in routes refuse gets the door with the refusal, as with Home |
| 58 | // (admittedOnly). Standalone Slides has only email, so the door is the |
| 59 | // email way. |
| 60 | func (a *app) standaloneSignin(w http.ResponseWriter, r *http.Request) { |
| 61 | if signedIn, admitted := a.admission(r, false); signedIn { |
| 62 | if !admitted { |
| 63 | r = a.refusedSession(w, r) |
| 64 | } else if !signinOutcome(r) { |
| 65 | alreadySignedIn(w, r) |
| 66 | return |
| 67 | } |
| 68 | } |
| 69 | a.signinDoor(w, r) |
| 70 | } |
| 71 | |
| 72 | // admittedOnly runs connect's SigninDoor, SigninLocal or SigninOficina for |
| 73 | // a browser whose session the signed-in routes would admit, and as signed |
| 74 | // out for any other. SigninDoor and SigninOficina take any live session |
| 75 | // cookie as signed in and send it straight to the page; a session the |
| 76 | // signed-in routes refuse (an address taken off the allowlist, a Home |
| 77 | // identity that no longer resolves) would then go round /signin, the page, |
| 78 | // the refusal and back (Astra on Docs #65, Sheets #39), so they are not |
| 79 | // shown that cookie. SigninLocal is wrapped too, for the refusals that |
| 80 | // land there. |
| 81 | func (a *app) admittedOnly(next http.HandlerFunc) http.HandlerFunc { |
| 82 | return func(w http.ResponseWriter, r *http.Request) { |
| 83 | if signedIn, admitted := a.admission(r, false); signedIn && !admitted { |
| 84 | r = withoutCookie(a.refusedSession(w, r), a.sessions.CookieName()) |
| 85 | } |
| 86 | next(w, r) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // refusedSession answers for a session the probe refused: its cookie is |
| 91 | // cleared on this response (the probe's own answer, which cleared it, was |
| 92 | // thrown away), so a reload is plainly signed out rather than a dead |
| 93 | // cookie over the plain door; a remembered Oficina ID is forgotten, |
| 94 | // because offered again it would sign the same refused person in and |
| 95 | // send them back here; and the door is marked to say why (the refusal, in |
| 96 | // the screen's app-problem slot with the email field). A remembered email |
| 97 | // way is kept: it is no loop. |
| 98 | func (a *app) refusedSession(w http.ResponseWriter, r *http.Request) *http.Request { |
| 99 | a.sessions.SignOut(w, r) |
| 100 | jar := a.auth.RememberJar() |
| 101 | if rec, res := jar.Read(r); res == lastsignin.Valid && rec.Method == lastsignin.MethodFederated { |
| 102 | jar.Clear(w) |
| 103 | } |
| 104 | return withRefusal(r) |
| 105 | } |
| 106 | |
| 107 | // admission reports whether r holds a session, and whether the signed-in |
| 108 | // routes would let it in right now, by running the same gate they run |
| 109 | // (requireSession over rauth's RequireSession, then currentUser through |
| 110 | // viewer: session, identity, the allowlist recheck or the roster) around a |
| 111 | // probe GET / whose answer is thrown away. A session is not enough: an |
| 112 | // address taken off the allowlist still holds a valid one. currentUser |
| 113 | // ends a session it refuses (offboard), as it would on the person's next |
| 114 | // page. |
| 115 | // |
| 116 | // A gate that cannot decide (a 5xx: storage or the roster unavailable) |
| 117 | // counts as admitted unless strict: the door must not tell somebody they |
| 118 | // cannot sign in, nor sign them out, because a read failed, and nothing |
| 119 | // is remembered on one. |
| 120 | func (a *app) admission(r *http.Request, strict bool) (signedIn, admitted bool) { |
| 121 | if a.auth == nil || a.sessions == nil { |
| 122 | return false, false |
| 123 | } |
| 124 | if _, ok := a.sessions.From(r); !ok { |
| 125 | return false, false |
| 126 | } |
| 127 | probe := r.Clone(r.Context()) |
| 128 | probe.Method = http.MethodGet |
| 129 | probe.URL = &url.URL{Path: "/"} |
| 130 | probe.RequestURI = "/" |
| 131 | probe.Body = http.NoBody |
| 132 | reached := false |
| 133 | rec := &bufferedResponse{header: make(http.Header)} |
| 134 | a.requireSession(a.auth.RequireSession)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 135 | _, reached = a.viewer(w, r) |
| 136 | })).ServeHTTP(rec, probe) |
| 137 | return true, reached || (!strict && rec.status >= http.StatusInternalServerError) |
| 138 | } |
| 139 | |
| 140 | // withoutCookie is r, as seen by a handler that must not see the named |
| 141 | // cookie. Only admittedOnly and admittedAs use it. |
| 142 | func withoutCookie(r *http.Request, name string) *http.Request { |
| 143 | c := r.Clone(r.Context()) |
| 144 | c.Header.Del("Cookie") |
| 145 | for _, cookie := range r.Cookies() { |
| 146 | if cookie.Name != name { |
| 147 | c.AddCookie(cookie) |
| 148 | } |
| 149 | } |
| 150 | return c |
| 151 | } |
| 152 | |
| 153 | // rememberOficina is the smart door's memory of a Home sign-in, written in |
| 154 | // MintHomeSession once the session is stored, so it is written only where |
| 155 | // connect turns a Home sign-in into a session here (a finished exchange, |
| 156 | // "I'm new here"), never on link, choose, unknown or failed. The record is |
| 157 | // Oficina ID, with the address of the person connect resolved for this |
| 158 | // very sign-in (connect.MintedPerson), or none for a Home-only member. |
| 159 | // Nothing is carried over from the record it replaces: that address could |
| 160 | // be somebody else's. Remembering is a convenience, and Remember fails |
| 161 | // silently, so it can never fail the sign-in. |
| 162 | // |
| 163 | // Only for a session the signed-in routes admit, asked of the session just |
| 164 | // minted (admittedAs) and strictly: connect mints for any linked person, |
| 165 | // and a record for a refused one would offer Oficina ID again, round a |
| 166 | // loop of sign-in, refusal and door (Fable on Docs #139, A2). |
| 167 | func (a *app) rememberOficina(w http.ResponseWriter, r *http.Request) { |
| 168 | if a.auth == nil || a.sessions == nil { |
| 169 | return |
| 170 | } |
| 171 | if _, admitted := a.admission(a.admittedAs(w, r), true); !admitted { |
| 172 | return |
| 173 | } |
| 174 | address := "" |
| 175 | if p, ok := connect.MintedPerson(r); ok { |
| 176 | address = p.Label |
| 177 | } |
| 178 | a.auth.RememberJar().Remember(w, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: address}) |
| 179 | } |
| 180 | |
| 181 | // admittedAs is r as the session MintHomeSession just set on w would send |
| 182 | // it: the request's own cookies, with that session's in place of any other. |
| 183 | func (a *app) admittedAs(w http.ResponseWriter, r *http.Request) *http.Request { |
| 184 | name := a.sessions.CookieName() |
| 185 | var minted *http.Cookie |
| 186 | for _, c := range (&http.Response{Header: w.Header()}).Cookies() { |
| 187 | if c.Name == name && c.MaxAge >= 0 && c.Value != "" { |
| 188 | minted = c |
| 189 | } |
| 190 | } |
| 191 | probe := withoutCookie(r, name) |
| 192 | if minted != nil { |
| 193 | probe.AddCookie(&http.Cookie{Name: minted.Name, Value: minted.Value}) |
| 194 | } |
| 195 | return probe |
| 196 | } |
| 197 | |
| 198 | // signinOutcome reports whether GET /signin names a state of the door |
| 199 | // rather than opening it afresh: an outcome of the email flow (?sent, |
| 200 | // ?err, ?force), the Keymail continuation (?continue) or a step-up |
| 201 | // (?reauth). connect's SigninDoor draws the same five for a signed-in |
| 202 | // browser too (its doorStates). |
| 203 | func signinOutcome(r *http.Request) bool { |
| 204 | q := r.URL.Query() |
| 205 | return q.Has("sent") || q.Has("err") || q.Has("force") || q.Has("continue") || q.Has("reauth") |
| 206 | } |
| 207 | |
| 208 | // alreadySignedIn sends a signed-in GET /signin back where it came from, |
| 209 | // with no door (spec §9.2: 303 to return_to or the deck list). |
| 210 | func alreadySignedIn(w http.ResponseWriter, r *http.Request) { |
| 211 | target := "/" |
| 212 | if path, ok := requestedReturn(r); ok { |
| 213 | target = path |
| 214 | } |
| 215 | w.Header().Set("Cache-Control", "no-store") |
| 216 | http.Redirect(w, r, target, http.StatusSeeOther) |
| 217 | } |
| 218 | |
| 219 | // signinDoor is the sign-in door: standalone GET /signin, and with Home |
| 220 | // configured GET /signin and /signin/local (connect's SigninDoor and |
| 221 | // SigninLocal hand both here, RoutesConfig.LocalSignin), plus auth's |
| 222 | // RenderConfirm (the confirm step) and Refused (the refusal). |
| 223 | // |
| 224 | // rastrillo decides what the door shows from the query and this browser's |
| 225 | // cookies (SigninState: the way in it used last, Sent, problems, the |
| 226 | // chooser), with SigninOneWay drawing one way in per page. Slides adds |
| 227 | // what only it knows: Oficina ID where connect says the door may offer it |
| 228 | // (OficinaHref is "" on /signin/local, on a step-up and standalone), the |
| 229 | // refusal, which only someone who has just verified the address, or whose |
| 230 | // session the signed-in routes no longer admit, reaches, and the removal |
| 231 | // offboard lands on. |
| 232 | func (a *app) signinDoor(w http.ResponseWriter, r *http.Request) { |
| 233 | a.signinDoorStatus(w, r, http.StatusOK) |
| 234 | } |
| 235 | |
| 236 | func (a *app) signinDoorStatus(w http.ResponseWriter, r *http.Request, status int) { |
| 237 | st := a.auth.SigninState(r) |
| 238 | if st.Unavailable { |
| 239 | // Under a base path or on a direct host the hub signs people in: |
| 240 | // estilo-signin-screen draws nothing, and every route rastrillo's |
| 241 | // door names answers 404 there. So does the door. |
| 242 | a.routeNotFound(w, r) |
| 243 | return |
| 244 | } |
| 245 | if href := connect.OficinaHref(r); href != "" { |
| 246 | st.Federated = &rauth.FederatedDoor{ |
| 247 | Provider: lastsignin.ProviderOficina, |
| 248 | Href: href, |
| 249 | Label: oficinaIDName, |
| 250 | Icon: chrome.OficinaGlyph, |
| 251 | } |
| 252 | } |
| 253 | switch { |
| 254 | case refusedHere(r): |
| 255 | st.AppProblem = &rauth.AppProblem{Text: rastrillo.T(r, "slides.signin.error.refused"), Ask: true} |
| 256 | case removedHere(r): |
| 257 | // offboard's landing: this address had access and no longer does, |
| 258 | // and asking for another link will not help. rastrillo knows no |
| 259 | // err=removed (it reads an unknown err as its generic "Something |
| 260 | // went wrong"), so the sentence is Slides', in the app-problem |
| 261 | // slot, in place of rastrillo's. |
| 262 | st.Problem = rauth.ProblemNone |
| 263 | st.AppProblem = &rauth.AppProblem{Text: rastrillo.T(r, "slides.signin.error.removed"), Ask: true} |
| 264 | } |
| 265 | a.rememberSigninReturn(w, r, &st) |
| 266 | a.auth.PrepareSigninResponse(w, st) |
| 267 | screen := chrome.SignInScreen{Icon: brand.Path("slides"), App: appName, Sky: sky.For(r.Host), State: st} |
| 268 | title, err := signinTitle(screen) |
| 269 | if err != nil { |
| 270 | a.fail(w, r, "render the sign-in title", err) |
| 271 | return |
| 272 | } |
| 273 | a.renderPage(w, "signin", status, signinView{ |
| 274 | shell: a.publicShell(r, frame{title: title, signinPage: true}), |
| 275 | Screen: screen, |
| 276 | }) |
| 277 | } |
| 278 | |
| 279 | // signinTitle is the door's <title>: estilo's estilo-signin-screen-title, |
| 280 | // which is rastrillo's signin-title for the same state, so the tab and the |
| 281 | // heading always say the same thing ("Sign in to Slides", "Check your |
| 282 | // email to sign in to Slides"). The template writes escaped text and the |
| 283 | // layout escapes the title again, so it is unescaped here. |
| 284 | func signinTitle(screen chrome.SignInScreen) (string, error) { |
| 285 | var b strings.Builder |
| 286 | if err := pages["signin"].ExecuteTemplate(&b, "estilo-signin-screen-title", screen); err != nil { |
| 287 | return "", err |
| 288 | } |
| 289 | return strings.TrimSpace(html.UnescapeString(b.String())), nil |
| 290 | } |
| 291 | |
| 292 | // renderPage renders name's layout with status. render alone always |
| 293 | // answers 200, which the refusal must not. |
| 294 | func (a *app) renderPage(w http.ResponseWriter, name string, status int, data any) { |
| 295 | var buf strings.Builder |
| 296 | if err := pages[name].ExecuteTemplate(&buf, "layout", data); err != nil { |
| 297 | a.logger.Error("slides: render "+name, "err", err) |
| 298 | http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 299 | return |
| 300 | } |
| 301 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 302 | w.WriteHeader(status) |
| 303 | _, _ = w.Write([]byte(buf.String())) |
| 304 | } |
| 305 | |
| 306 | // refusedKey marks a request the door draws with the refusal. |
| 307 | type refusedKey struct{} |
| 308 | |
| 309 | // withRefusal marks r as a refusal the door draws: a verified address the |
| 310 | // allowlist refused (refused), or a session the signed-in routes no |
| 311 | // longer admit (refusedSession). |
| 312 | // |
| 313 | // Only on the plain door: the chooser (?ways=) and a way's own page |
| 314 | // (?way=) are how a refused person reaches another way in, another Oficina |
| 315 | // ID account included, and a refusal there would draw the field again and |
| 316 | // leave "Other ways to sign in" going nowhere (Fable on Docs #139, N1). |
| 317 | func withRefusal(r *http.Request) *http.Request { |
| 318 | if q := r.URL.Query(); q.Has("ways") || q.Has("way") { |
| 319 | return r |
| 320 | } |
| 321 | return r.WithContext(context.WithValue(r.Context(), refusedKey{}, true)) |
| 322 | } |
| 323 | |
| 324 | // refusedHere reports whether r is a refusal the door is drawing. Never |
| 325 | // from the query: a query parameter is something anybody can type. |
| 326 | func refusedHere(r *http.Request) bool { |
| 327 | v, _ := r.Context().Value(refusedKey{}).(bool) |
| 328 | return v |
| 329 | } |
| 330 | |
| 331 | // removedHere reports whether the door is offboard's landing, ?err=removed, |
| 332 | // on the plain door only, like the refusal. It is from the query, as it |
| 333 | // always was: the sentence names no address and tells nobody anything |
| 334 | // about one. |
| 335 | func removedHere(r *http.Request) bool { |
| 336 | q := r.URL.Query() |
| 337 | return q.Get("err") == "removed" && !q.Has("ways") && !q.Has("way") |
| 338 | } |
| 339 | |
| 340 | // renderSignedOut is where Sign out lands: /signed-out, in both modes, in |
| 341 | // the door's words: "You’re signed out", Sign in, then the quiet "Forget |
| 342 | // me on this device" (the smart-door spec's "Signed out"), a POST to |
| 343 | // rastrillo auth's Forget. With Home configured the page is connect's |
| 344 | // SignedOut, which carries that path (RoutesConfig.ForgetPath) for PageFor, |
| 345 | // and the form also asks Slides to go on to Oficina ID's own Forget once |
| 346 | // this one has answered (forget). Standalone it is Slides' own path, with |
| 347 | // no chain. |
| 348 | func (a *app) renderSignedOut(w http.ResponseWriter, r *http.Request, status int) { |
| 349 | w.Header().Set("Cache-Control", "no-store") |
| 350 | p := connect.PageFor(r, "signedout", "") |
| 351 | forget, chain := p.ForgetAction, []chrome.Hidden{{Name: "chain", Value: oficinaChain}} |
| 352 | if forget == "" { |
| 353 | forget, chain = signinForgetPath, nil |
| 354 | } |
| 355 | notice := chrome.SignInNotice{Icon: brand.Path("slides"), App: appName, Sky: sky.For(r.Host)}. |
| 356 | SignedOut(p.Action).Forgetting(forget, "", chain...) |
| 357 | a.renderPage(w, "signed_out", status, signedOutView{ |
| 358 | shell: a.publicShell(r, frame{parts: []string{notice.Page()}, signinPage: true}), |
| 359 | Notice: notice, |
| 360 | }) |
| 361 | } |
| 362 | |
| 363 | // signedOutView is the signed-out page's data. |
| 364 | type signedOutView struct { |
| 365 | shell |
| 366 | Notice chrome.SignInNotice |
| 367 | } |
| 368 | |
| 369 | // signedOut serves GET /signed-out in standalone mode. |
| 370 | func (a *app) signedOut(w http.ResponseWriter, r *http.Request) { |
| 371 | a.renderSignedOut(w, r, http.StatusOK) |
| 372 | } |
| 373 | |
| 374 | // forget is POST /signin/forget, rastrillo auth's Forget: "Use a different |
| 375 | // email" on the door, and "Forget me on this device" on the signed-out |
| 376 | // page. Each host remembers for itself, so with Home configured the |
| 377 | // signed-out page's form (chain=oficina) goes on to Oficina ID's own Forget |
| 378 | // once this one has answered, with Slides' signed-out page as the way back: |
| 379 | // {issuer}/forget?return=<origin>/signed-out. id.oficina.works asks "Forget |
| 380 | // me on Oficina ID too?" and returns only to a registered consumer's exact |
| 381 | // /signed-out. Anything but Forget's own redirect (a cross-origin refusal, |
| 382 | // a wrong method) is passed on as it is. A page a sign-in in progress kept |
| 383 | // (the return memory) is forgotten too. |
| 384 | func (a *app) forget(w http.ResponseWriter, r *http.Request) { |
| 385 | if r.Method == http.MethodPost { |
| 386 | a.forgetReturn(w) |
| 387 | } |
| 388 | if a.homeIssuer == "" { |
| 389 | a.auth.Forget(w, r) |
| 390 | return |
| 391 | } |
| 392 | result := &bufferedResponse{header: make(http.Header)} |
| 393 | a.auth.Forget(result, r) |
| 394 | if result.status == http.StatusSeeOther && r.PostFormValue("chain") == oficinaChain { |
| 395 | result.header.Set("Location", oficinaForgetURL(a.homeIssuer, a.cfg.Origin)) |
| 396 | } |
| 397 | result.replay(w) |
| 398 | } |
| 399 | |
| 400 | // oficinaForgetURL is Oficina ID's Forget, returning to this origin's |
| 401 | // signed-out page (Home's internal/home/door.go, forgetChainReturn). |
| 402 | func oficinaForgetURL(issuer, origin string) string { |
| 403 | back := strings.TrimSuffix(origin, "/") + "/signed-out" |
| 404 | return strings.TrimSuffix(issuer, "/") + "/forget?" + url.Values{"return": {back}}.Encode() |
| 405 | } |
| 406 | |
| 407 | // standaloneSignout is rastrillo's Sign out, landing on /signed-out |
| 408 | // (signedOutAfter), which also forgets a page a sign-in in progress kept: |
| 409 | // it was this person's, not the next one's. |
| 410 | func (a *app) standaloneSignout(signout http.HandlerFunc) http.HandlerFunc { |
| 411 | landing := signedOutAfter(signout) |
| 412 | return func(w http.ResponseWriter, r *http.Request) { |
| 413 | a.forgetReturn(w) |
| 414 | landing(w, r) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // homeSignout is connect's Sign out, which also forgets a page a sign-in |
| 419 | // in progress kept, for the same reason. |
| 420 | func (a *app) homeSignout(w http.ResponseWriter, r *http.Request) { |
| 421 | a.forgetReturn(w) |
| 422 | a.homeRoutes.Signout(w, r) |
| 423 | } |
| 424 | |