| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "html" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/estilo.git/brand" |
| 11 | "amadan.net/oficina/estilo.git/chrome" |
| 12 | "amadan.net/oficina/estilo.git/sky" |
| 13 | "amadan.net/oficina/home/connect" |
| 14 | "amadan.net/rastrillo/rastrillo/auth" |
| 15 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 16 | "amadan.net/rastrillo/rastrillo/sessions" |
| 17 | ) |
| 18 | |
| 19 | // The sign-in door (Home spec 2026-11-19-smart-door.md, step 6; home/connect |
| 20 | // MIGRATING.md): rastrillo's sign-in screen, its ui "signin" partial fed by |
| 21 | // auth.SigninState, drawn inside estilo's door by estilo-signin-screen. Its |
| 22 | // words are rastrillo's approved copy (rastrillo.ui.signin_*), and the |
| 23 | // signed-out page's are estilo's chrome.English, so every app in the suite |
| 24 | // says the same thing. Only the refusal is this app's (signinRefusal). |
| 25 | |
| 26 | // signinForgetPath is where rastrillo auth's Forget is mounted |
| 27 | // (home_routes.go): "Use a different email" on the door, and "Forget me on |
| 28 | // this device" on the signed-out page. It is auth's own default, named here |
| 29 | // because the signed-out page and connect's RoutesConfig.ForgetPath must |
| 30 | // 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, Sheets 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 "Sheets", and rastrillo |
| 42 | // draws it as "Continue with {name}". |
| 43 | const oficinaIDName = "Oficina ID" |
| 44 | |
| 45 | // signinRefusal is the refusal (App.refused, and a session the signed-in |
| 46 | // routes no longer admit), in the screen's app-problem slot with the email |
| 47 | // field. Paul approved the wording, 2026-11-21: Docs' sentence with the |
| 48 | // app's name. |
| 49 | const signinRefusal = "That email can’t sign in to Sheets. Use a different email, or ask whoever runs this team to add it." |
| 50 | |
| 51 | // signinView is the sign-in door's view data (templates/signin.html). |
| 52 | // Every state is that one template: the door, the chooser ("Other ways to |
| 53 | // sign in"), Sent, the Keymail continuation, a problem or refusal, and the |
| 54 | // confirm step an emailed link opens. |
| 55 | type signinView struct { |
| 56 | Screen chrome.SignInScreen |
| 57 | } |
| 58 | |
| 59 | // handleSignin is GET /signin without Home: somebody already signed in |
| 60 | // never sees the door (§9.2's last row) unless the query names one of its |
| 61 | // states, and goes to a safe return_to or the list; anyone else gets the |
| 62 | // door. Standalone Sheets has only email, so the door is the email way. |
| 63 | func (a *App) handleSignin(w http.ResponseWriter, r *http.Request) { |
| 64 | if !signinOutcome(r) { |
| 65 | if _, ok := a.identity(r); ok { |
| 66 | http.Redirect(w, r, signedInLanding(r), http.StatusSeeOther) |
| 67 | return |
| 68 | } |
| 69 | } |
| 70 | a.signinDoor(w, r) |
| 71 | } |
| 72 | |
| 73 | // signinOutcome reports whether GET /signin names a state of the door |
| 74 | // rather than opening it afresh: an outcome of the email flow (?sent, |
| 75 | // ?err, ?force), the Keymail continuation (?continue) or a step-up |
| 76 | // (?reauth). connect's SigninDoor draws the same five for a signed-in |
| 77 | // browser too (its doorStates). |
| 78 | func signinOutcome(r *http.Request) bool { |
| 79 | q := r.URL.Query() |
| 80 | return q.Has("sent") || q.Has("err") || q.Has("force") || q.Has("continue") || q.Has("reauth") |
| 81 | } |
| 82 | |
| 83 | // signinDoor is the sign-in door: standalone GET /signin, and with Home |
| 84 | // configured GET /signin and /signin/local (connect's SigninDoor and |
| 85 | // SigninLocal hand both here, RoutesConfig.LocalSignin), plus auth's |
| 86 | // RenderConfirm (the confirm step) and Refused (the refusal). |
| 87 | // |
| 88 | // rastrillo decides what the door shows from the query and this browser's |
| 89 | // cookies (SigninState: the way in it used last, Sent, problems, the |
| 90 | // chooser), with SigninOneWay drawing one way in per page. Sheets adds what |
| 91 | // only it knows: Oficina ID where connect says the door may offer it |
| 92 | // (OficinaHref is "" on /signin/local, on a step-up and standalone), and |
| 93 | // the refusal, which only someone who has just verified the address, or |
| 94 | // whose session the signed-in routes no longer admit, reaches. |
| 95 | func (a *App) signinDoor(w http.ResponseWriter, r *http.Request) { |
| 96 | a.signinDoorStatus(w, r, http.StatusOK) |
| 97 | } |
| 98 | |
| 99 | func (a *App) signinDoorStatus(w http.ResponseWriter, r *http.Request, status int) { |
| 100 | if a.auth == nil { |
| 101 | // Tests without sign-in: there is no door to draw. |
| 102 | a.routeNotFound(w, r) |
| 103 | return |
| 104 | } |
| 105 | st := a.auth.SigninState(r) |
| 106 | if st.Unavailable { |
| 107 | // Under a base path or on a direct host the hub signs people in: |
| 108 | // estilo-signin-screen draws nothing, and every route rastrillo's |
| 109 | // door names answers 404 there. So does the door. |
| 110 | a.routeNotFound(w, r) |
| 111 | return |
| 112 | } |
| 113 | if href := connect.OficinaHref(r); href != "" { |
| 114 | st.Federated = &auth.FederatedDoor{ |
| 115 | Provider: lastsignin.ProviderOficina, |
| 116 | Href: href, |
| 117 | Label: oficinaIDName, |
| 118 | Icon: chrome.OficinaGlyph, |
| 119 | } |
| 120 | } |
| 121 | if refusedHere(r) { |
| 122 | st.AppProblem = &auth.AppProblem{Text: signinRefusal, Ask: true} |
| 123 | } |
| 124 | a.rememberSigninReturn(w, r, &st) |
| 125 | a.auth.PrepareSigninResponse(w, st) |
| 126 | screen := chrome.SignInScreen{Icon: brand.Path("sheets"), App: appName, Sky: sky.For(r.Host), State: st} |
| 127 | title, err := signinTitle(screen) |
| 128 | if err != nil { |
| 129 | a.fail(w, r, err) |
| 130 | return |
| 131 | } |
| 132 | a.render(w, r, "signin", frame{title: title, signinPage: true}, status, signinView{Screen: screen}) |
| 133 | } |
| 134 | |
| 135 | // signinTitle is the door's <title>: estilo's estilo-signin-screen-title, |
| 136 | // which is rastrillo's signin-title for the same state, so the tab and the |
| 137 | // heading always say the same thing ("Sign in to Sheets", "Check your |
| 138 | // email to sign in to Sheets"). The template writes escaped text and the |
| 139 | // layout escapes the title again, so it is unescaped here. |
| 140 | func signinTitle(screen chrome.SignInScreen) (string, error) { |
| 141 | t, ok := pages["signin"] |
| 142 | if !ok { |
| 143 | return "", errNoTemplate |
| 144 | } |
| 145 | var b strings.Builder |
| 146 | if err := t.ExecuteTemplate(&b, "estilo-signin-screen-title", screen); err != nil { |
| 147 | return "", err |
| 148 | } |
| 149 | return strings.TrimSpace(html.UnescapeString(b.String())), nil |
| 150 | } |
| 151 | |
| 152 | // renderConfirm is auth's RenderConfirm, the page an emailed link opens on |
| 153 | // (GET /auth/verify): the door, whose SigninState answers StepConfirm on |
| 154 | // this request alone, with one Sign in button that POSTs the token back, |
| 155 | // where auth spends it. The GET spends nothing, so a mail-security scanner |
| 156 | // that fetches every link cannot sign anybody in. auth has already set |
| 157 | // no-store, Referrer-Policy strict-origin (the URL is a live credential) |
| 158 | // and the framing refusals; nothing here loosens them. |
| 159 | func (a *App) renderConfirm(w http.ResponseWriter, r *http.Request, _ auth.ConfirmPageData) { |
| 160 | a.signinDoor(w, r) |
| 161 | } |
| 162 | |
| 163 | // refusedKey marks a request the door draws with the refusal. |
| 164 | type refusedKey struct{} |
| 165 | |
| 166 | // withRefusal marks r as a refusal the door draws: a verified address |
| 167 | // Admits refused (App.refused), or a session the signed-in routes no |
| 168 | // longer admit (admittedOnly). |
| 169 | // |
| 170 | // Only on the plain door: the chooser (?ways=) and a way's own page |
| 171 | // (?way=) are how a refused person reaches another way in, another Oficina |
| 172 | // ID account included, and a refusal there would draw the field again and |
| 173 | // leave "Other ways to sign in" going nowhere (Fable on Docs #139, N1). |
| 174 | func withRefusal(r *http.Request) *http.Request { |
| 175 | if q := r.URL.Query(); q.Has("ways") || q.Has("way") { |
| 176 | return r |
| 177 | } |
| 178 | return r.WithContext(context.WithValue(r.Context(), refusedKey{}, true)) |
| 179 | } |
| 180 | |
| 181 | // refusedHere reports whether r is a refusal the door is drawing. Never |
| 182 | // from the query: a query parameter is something anybody can type. |
| 183 | func refusedHere(r *http.Request) bool { |
| 184 | v, _ := r.Context().Value(refusedKey{}).(bool) |
| 185 | return v |
| 186 | } |
| 187 | |
| 188 | // signedInLanding is where an already-signed-in /signin goes: the |
| 189 | // request's return_to when rastrillo's SafeReturn accepts it and it is |
| 190 | // not itself a sign-in page (which would loop), otherwise the list. |
| 191 | func signedInLanding(r *http.Request) string { |
| 192 | target := sessions.SafeReturn(r, "/sheets") |
| 193 | if target == "/" || strings.HasPrefix(target, "/signin") || strings.HasPrefix(target, "/auth/") || strings.HasPrefix(target, "/signed-out") { |
| 194 | return "/sheets" |
| 195 | } |
| 196 | return target |
| 197 | } |
| 198 | |
| 199 | // renderSignedOut is where Sign out lands: /signed-out, in both modes, in |
| 200 | // the door's words: "You’re signed out", Sign in, then the quiet "Forget |
| 201 | // me on this device" (the smart-door spec's "Signed out"), a POST to |
| 202 | // rastrillo auth's Forget. With Home configured the page is connect's |
| 203 | // SignedOut, which carries that path (RoutesConfig.ForgetPath) for PageFor, |
| 204 | // and the form also asks Sheets to go on to Oficina ID's own Forget once |
| 205 | // this one has answered (forget). Standalone it is Sheets' own path, with |
| 206 | // no chain. |
| 207 | func (a *App) renderSignedOut(w http.ResponseWriter, r *http.Request, status int) { |
| 208 | w.Header().Set("Cache-Control", "no-store") |
| 209 | p := connect.PageFor(r, "signedout", "") |
| 210 | forget, chain := p.ForgetAction, []chrome.Hidden{{Name: "chain", Value: oficinaChain}} |
| 211 | if forget == "" { |
| 212 | forget, chain = signinForgetPath, nil |
| 213 | } |
| 214 | out := chrome.SignInNotice{Icon: brand.Path("sheets"), App: appName, Sky: sky.For(r.Host)}. |
| 215 | SignedOut(p.Action).Forgetting(forget, "", chain...) |
| 216 | a.render(w, r, "signed_out", frame{parts: []string{out.Page()}, signinPage: true}, status, out) |
| 217 | } |
| 218 | |
| 219 | func (a *App) handleSignedOut(w http.ResponseWriter, r *http.Request) { |
| 220 | a.renderSignedOut(w, r, http.StatusOK) |
| 221 | } |
| 222 | |
| 223 | // forget is POST /signin/forget, rastrillo auth's Forget: "Use a different |
| 224 | // email" on the door, and "Forget me on this device" on the signed-out |
| 225 | // page. Each host remembers for itself, so with Home configured the |
| 226 | // signed-out page's form (chain=oficina) goes on to Oficina ID's own Forget |
| 227 | // once this one has answered, with Sheets' signed-out page as the way back: |
| 228 | // {issuer}/forget?return=<origin>/signed-out. id.oficina.works asks "Forget |
| 229 | // me on Oficina ID too?" and returns only to a registered consumer's exact |
| 230 | // /signed-out. Anything but Forget's own redirect (a cross-origin refusal, |
| 231 | // a wrong method) is passed on as it is. A page a sign-in in progress |
| 232 | // kept (signin_return.go) is forgotten too. |
| 233 | func (a *App) forget(w http.ResponseWriter, r *http.Request) { |
| 234 | if a.returns != nil && r.Method == http.MethodPost { |
| 235 | forgetReturn(w) |
| 236 | } |
| 237 | if a.homeCreds == nil { |
| 238 | a.auth.Forget(w, r) |
| 239 | return |
| 240 | } |
| 241 | result := &bufferedResponse{header: make(http.Header)} |
| 242 | a.auth.Forget(result, r) |
| 243 | if result.status == http.StatusSeeOther && r.PostFormValue("chain") == oficinaChain { |
| 244 | result.header.Set("Location", oficinaForgetURL(a.homeCreds.Issuer, a.origin)) |
| 245 | } |
| 246 | result.replay(w) |
| 247 | } |
| 248 | |
| 249 | // oficinaForgetURL is Oficina ID's Forget, returning to this origin's |
| 250 | // signed-out page (Home's internal/home/door.go, forgetChainReturn). |
| 251 | func oficinaForgetURL(issuer, origin string) string { |
| 252 | back := strings.TrimSuffix(origin, "/") + "/signed-out" |
| 253 | return strings.TrimSuffix(issuer, "/") + "/forget?" + url.Values{"return": {back}}.Encode() |
| 254 | } |
| 255 | |
| 256 | // signOutToPage is rastrillo's Sign out, landing on /signed-out instead of |
| 257 | // the sign-in door. auth.Signout ends the session and 303s to its |
| 258 | // SigninPath; this rewrites only that one Location, so everything else |
| 259 | // auth answers (its cross-origin refusal) is untouched. |
| 260 | func (a *App) signOutToPage(w http.ResponseWriter, r *http.Request) { |
| 261 | a.auth.Signout(&landing{ResponseWriter: w}, r) |
| 262 | } |
| 263 | |
| 264 | type landing struct{ http.ResponseWriter } |
| 265 | |
| 266 | func (l *landing) WriteHeader(code int) { |
| 267 | if code == http.StatusSeeOther && l.Header().Get("Location") == "/signin" { |
| 268 | l.Header().Set("Location", "/signed-out") |
| 269 | } |
| 270 | l.ResponseWriter.WriteHeader(code) |
| 271 | } |
| 272 | |