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