| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "encoding/hex" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/estilo.git/chrome" |
| 13 | "amadan.net/oficina/estilo.git/sky" |
| 14 | "amadan.net/rastrillo/rastrillo/auth" |
| 15 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 16 | "github.com/keymaildev/signin" |
| 17 | ) |
| 18 | |
| 19 | // The smart door on id.oficina.works (smart-door spec, step 5; |
| 20 | // docs/superpowers/specs/2026-11-19-smart-door.md). The sign-in page is |
| 21 | // rastrillo's one-way screen, drawn by its own "signin" partial inside |
| 22 | // estilo's door (estilo-signin-screen): Home fills an auth.SigninState |
| 23 | // itself, because Home is blind and runs its own sign-in, and never |
| 24 | // builds auth's handlers. This file is the only one in Home that may |
| 25 | // import auth or lastsignin, and only for the state, the jar and their |
| 26 | // constants (internal/guardstest, TestHomeNeverMountsFrameworkSignin). |
| 27 | // |
| 28 | // What the door remembers is ruling 26 (design spec §1): the way in this |
| 29 | // browser used last, and the address it last proved, sealed into a |
| 30 | // cookie on the person's own device and rendered back only to the browser |
| 31 | // that presents it, on a no-store page. Never at rest in Home, never in a |
| 32 | // log, never evidence of anything: a one-tap posts the address to Begin, |
| 33 | // which proves it afresh. |
| 34 | |
| 35 | // doorName is the brand on the door (the spec: id.oficina.works is "Oficina |
| 36 | // ID"): the card's head, and the partial's "Sign in to Oficina ID". |
| 37 | const doorName = "Oficina ID" |
| 38 | |
| 39 | // jarLabel is the label HOME_DEK's Sealer derives the jar's instance key |
| 40 | // under (seal.Sealer.Derive). Changing it forgets every browser, which is |
| 41 | // harmless: an old cookie reads as Invalid and is cleared. |
| 42 | const jarLabel = "home/lastsignin/v1" |
| 43 | |
| 44 | // The door's paths. signinPath is the door itself; forgetPath is "Use a |
| 45 | // different email" (rastrillo's Forget, POST); forgetChainPath is the |
| 46 | // confirm page an app's signed-out page links to after its own Forget. |
| 47 | const ( |
| 48 | signinPath = "/signin" |
| 49 | forgetPath = "/signin/forget" |
| 50 | forgetChainPath = "/forget" |
| 51 | ) |
| 52 | |
| 53 | // The passkey door's endpoints and scripts. The begin/finish pair is the |
| 54 | // discoverable ("direct") ceremony, whose finish answers {"ok":true,"to":…}, |
| 55 | // the shape rastrillo's door module reads. signinModule is that module |
| 56 | // (passkey.JS()), served by Home; webauthnModule is rastrillo's WebAuthn |
| 57 | // helper, which the module imports. |
| 58 | const ( |
| 59 | directBegin = "/passkey/direct/begin" |
| 60 | directFinish = "/passkey/direct/finish" |
| 61 | signinModule = "/static/signin.mjs" |
| 62 | webauthnModule = "/static/webauthn.mjs" |
| 63 | ) |
| 64 | |
| 65 | // doorMemory is the door's remembered way in: lastsignin's jar, which |
| 66 | // seals an address into this browser's cookie (ruling 26). It is held |
| 67 | // behind this type, and every write is one of the narrow functions below |
| 68 | // (rememberEmail, passkeyRemembrance, forget), so nothing outside this |
| 69 | // file can seal an arbitrary address: internal/guardstest refuses a |
| 70 | // ".jar" or any lastsignin name in any other production file. |
| 71 | type doorMemory struct { |
| 72 | jar *lastsignin.Jar |
| 73 | } |
| 74 | |
| 75 | // newDoorMemory builds the id.* jar: lastsignin's cookie, its key derived |
| 76 | // from HOME_DEK under jarLabel, bound to the credential origin. No attempt |
| 77 | // cookie: Home keeps none. |
| 78 | func newDoorMemory(secrets Secrets, origin string) (*doorMemory, error) { |
| 79 | key, err := secrets.Sealer.Derive(jarLabel) |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | jar, err := lastsignin.New(lastsignin.Config{ |
| 84 | Origin: origin, |
| 85 | InstanceKey: hex.EncodeToString(key), |
| 86 | Mode: lastsignin.On, |
| 87 | }) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | return &doorMemory{jar: jar}, nil |
| 92 | } |
| 93 | |
| 94 | // remember records how this browser just signed in. A no-op on an App with |
| 95 | // no door (the directory: newDirectory builds it without one, and it never |
| 96 | // serves the door). The response that carries the cookie is no-store, so no |
| 97 | // cache keeps it. A record the jar refuses to write (an address it would |
| 98 | // not read back, such as one over 254 bytes) clears the cookie instead: |
| 99 | // otherwise the previous person's record would survive this sign-in. |
| 100 | func (a *App) remember(w http.ResponseWriter, rec lastsignin.Record) { |
| 101 | if a.door == nil { |
| 102 | return |
| 103 | } |
| 104 | w.Header().Set("Cache-Control", "no-store") |
| 105 | before := len(w.Header().Values("Set-Cookie")) |
| 106 | a.door.jar.Remember(w, rec) |
| 107 | if len(w.Header().Values("Set-Cookie")) == before { |
| 108 | a.door.jar.Clear(w) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // rememberEmail records a proven link or Keymail callback, with the |
| 113 | // address this browser proved. |
| 114 | func (a *App) rememberEmail(w http.ResponseWriter, id signin.Identity) { |
| 115 | method := lastsignin.MethodMagicLink |
| 116 | if id.Method == signin.MethodKeymail { |
| 117 | method = lastsignin.MethodKeymail |
| 118 | } |
| 119 | a.remember(w, lastsignin.Record{Method: method, Address: id.Address}) |
| 120 | } |
| 121 | |
| 122 | // passkeyRemembrance is what a finished passkey sign-in will remember, |
| 123 | // decided inside the sign-in's own transaction and written, by the |
| 124 | // function it returns, only once that transaction has committed: passkey, |
| 125 | // and the address this browser had remembered only if its blind index |
| 126 | // resolves to the same person ref as the passkey's owner. Anything else |
| 127 | // blanks it, so one person's passkey sign-in is never labelled with |
| 128 | // another's address. The lookup reads; it never mints a person. |
| 129 | func (a *App) passkeyRemembrance(tx *sql.Tx, r *http.Request, owner string) func(http.ResponseWriter) { |
| 130 | rec := lastsignin.Record{Method: lastsignin.MethodPasskey} |
| 131 | if a.door != nil { |
| 132 | if prev, res := a.door.jar.Read(r); res == lastsignin.Valid && prev.Address != "" { |
| 133 | var ref string |
| 134 | err := tx.QueryRow("SELECT person_ref FROM people WHERE blind_index=?", blindIndex(a.secrets.Pepper, prev.Address)).Scan(&ref) |
| 135 | if err == nil && ref == owner { |
| 136 | rec.Address = prev.Address |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | return func(w http.ResponseWriter) { a.remember(w, rec) } |
| 141 | } |
| 142 | |
| 143 | // queryProblem maps ?err= as rastrillo's screen does, so its words are |
| 144 | // rastrillo's approved copy. Home sends none of |
| 145 | // these itself today; the door honours them so that every problem is drawn |
| 146 | // the same way, on the email field. An err it does not know is the |
| 147 | // generic problem (ChooseWay). |
| 148 | func queryProblem(q url.Values) auth.SigninProblem { |
| 149 | switch q.Get("err") { |
| 150 | case "rate": |
| 151 | return auth.ProblemRate |
| 152 | case "address": |
| 153 | return auth.ProblemAddress |
| 154 | case "expired": |
| 155 | return auth.ProblemExpired |
| 156 | case "keymail": |
| 157 | return auth.ProblemKeymail |
| 158 | } |
| 159 | return auth.ProblemNone |
| 160 | } |
| 161 | |
| 162 | // doorState fills the one-way screen's state for r. It reads the query, |
| 163 | // this browser's remembered cookie and its return and step-up cookies, |
| 164 | // and consults no store, so the page is a function of what the browser |
| 165 | // already holds and cannot reveal whether an address is known. A cookie |
| 166 | // that cannot be believed is cleared on w. |
| 167 | // |
| 168 | // target is where the sign-in is going (a credentialReturn value): every |
| 169 | // link, Back, the Forget forms and the passkey finish carry it as |
| 170 | // return_to, so neither the chooser nor Forget loses the app's handoff. |
| 171 | func (a *App) doorState(w http.ResponseWriter, r *http.Request, target string, problem auth.SigninProblem) auth.SigninState { |
| 172 | q := r.URL.Query() |
| 173 | st := auth.SigninState{ |
| 174 | Step: auth.StepAsk, |
| 175 | BeginPath: signinPath, |
| 176 | ForgetPath: forgetPath, |
| 177 | SigninPath: signinPath, |
| 178 | OneWay: true, |
| 179 | Force: q.Get("force") == "1", |
| 180 | Passkey: &auth.PasskeyDoor{ |
| 181 | BeginPath: directBegin, |
| 182 | FinishPath: directFinish, |
| 183 | ModuleURL: webauthnModule, |
| 184 | ScriptURL: signinModule, |
| 185 | LegacyRPID: a.passkeyCfg.LegacyRPID, |
| 186 | }, |
| 187 | } |
| 188 | // The form's own action keeps force: a browser submits the action, not |
| 189 | // the URL that drew the form, and a forced retry that lost it would go |
| 190 | // straight back to Keymail. The partial also posts a hidden force=1. |
| 191 | if st.Force { |
| 192 | st.BeginPath = signinPath + "?force=1" |
| 193 | } |
| 194 | if target != "/" { |
| 195 | st.ReturnTo = target |
| 196 | } |
| 197 | if a.door != nil { |
| 198 | switch rec, res := a.door.jar.Read(r); res { |
| 199 | case lastsignin.Valid: |
| 200 | st.Remembered = &auth.Remembered{Method: rec.Method, Address: rec.Address, Provider: rec.Provider} |
| 201 | // The field's prefill is the remembered address, never the |
| 202 | // query or a posted value: the one account-authored string |
| 203 | // id.* may render is the one Home sealed itself (ruling 26). |
| 204 | st.Address = rec.Address |
| 205 | case lastsignin.Invalid: |
| 206 | a.door.jar.Clear(w) |
| 207 | } |
| 208 | } |
| 209 | if problem == auth.ProblemNone { |
| 210 | problem = queryProblem(q) |
| 211 | } |
| 212 | switch { |
| 213 | case problem != auth.ProblemNone: |
| 214 | st.Problem = problem |
| 215 | case q.Get("reauth") == "1" || a.freshStepUp(r): |
| 216 | // A step-up (max_age): every way id.* offers is local, so the |
| 217 | // door keeps them all and says why it is asking. |
| 218 | st.Problem = auth.ProblemReauth |
| 219 | } |
| 220 | if st.Remembered != nil && (st.Problem == auth.ProblemNone || st.Problem == auth.ProblemReauth) { |
| 221 | st.Step = auth.StepReturning |
| 222 | } |
| 223 | st.ChooseWay(q) |
| 224 | return st |
| 225 | } |
| 226 | |
| 227 | // freshStepUp reports whether this sign-in is a fresh step-up: a live |
| 228 | // assertion intent with max_age (implementation/fresh-assertion.md). |
| 229 | func (a *App) freshStepUp(r *http.Request) bool { |
| 230 | intent, ok := a.readAssertionIntent(r, time.Now()) |
| 231 | return ok && intent.MaxAge != 0 |
| 232 | } |
| 233 | |
| 234 | // doorScreen is door.html's data: estilo's door around rastrillo's |
| 235 | // partial, with the suite's sky and the Oficina mark. |
| 236 | func doorScreen(st auth.SigninState) chrome.SignInScreen { |
| 237 | return chrome.SignInScreen{ |
| 238 | Icon: versioned("/static/brand/icon.svg"), |
| 239 | App: doorName, |
| 240 | Sky: sky.For(sky.SuiteSeed), |
| 241 | State: st, |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // renderDoor draws one state of the door. Every door response is |
| 246 | // no-store: it may carry the remembered address, and a shared cache or |
| 247 | // the back-forward history keeps a page, not a cookie. |
| 248 | func (a *App) renderDoor(w http.ResponseWriter, r *http.Request, status int, st auth.SigninState) { |
| 249 | w.Header().Set("Cache-Control", "no-store") |
| 250 | if st.Step == auth.StepContinue { |
| 251 | // The page navigates to a URL carrying state and the PKCE |
| 252 | // challenge. |
| 253 | w.Header().Set("Referrer-Policy", "no-referrer") |
| 254 | } |
| 255 | a.render(w, r, status, "door.html", doorScreen(st)) |
| 256 | } |
| 257 | |
| 258 | // doorTarget is where a sign-in drawn for r is going, as rememberReturn |
| 259 | // decides it for a GET of the door. |
| 260 | func doorTarget(r *http.Request) string { |
| 261 | q, err := url.ParseQuery(r.URL.RawQuery) |
| 262 | if err != nil { |
| 263 | return "/" |
| 264 | } |
| 265 | values, present := q["return_to"] |
| 266 | if !present && q.Get("force") == "1" { |
| 267 | return readReturn(r) |
| 268 | } |
| 269 | if len(values) == 1 { |
| 270 | return credentialReturn(values[0]) |
| 271 | } |
| 272 | return "/" |
| 273 | } |
| 274 | |
| 275 | // handleForget is POST /signin/forget, "Use a different email": it forgets |
| 276 | // this browser's remembered way in and opens the email field, keeping |
| 277 | // where the sign-in was going (the posted return_to, else the return |
| 278 | // cookie) and a step-up. Behind csrf.Protect, as every form here is; the |
| 279 | // worst a forged one can do is forget. |
| 280 | func (a *App) handleForget(w http.ResponseWriter, r *http.Request) { |
| 281 | w.Header().Set("Cache-Control", "no-store") |
| 282 | if err := r.ParseForm(); err != nil { |
| 283 | a.errorPage(w, r, http.StatusBadRequest) |
| 284 | return |
| 285 | } |
| 286 | a.forget(w) |
| 287 | target := credentialReturn(r.PostFormValue("return_to")) |
| 288 | if target == "/" { |
| 289 | target = readReturn(r) |
| 290 | } |
| 291 | q := url.Values{} |
| 292 | if auth.ParseWay(r.PostFormValue("way")) == auth.WayEmail { |
| 293 | q.Set("way", string(auth.WayEmail)) |
| 294 | } |
| 295 | if target != "/" { |
| 296 | q.Set("return_to", target) |
| 297 | } |
| 298 | if r.PostFormValue("reauth") == "1" { |
| 299 | q.Set("reauth", "1") |
| 300 | } |
| 301 | to := signinPath |
| 302 | if len(q) > 0 { |
| 303 | to += "?" + q.Encode() |
| 304 | } |
| 305 | http.Redirect(w, r, to, http.StatusSeeOther) |
| 306 | } |
| 307 | |
| 308 | func (a *App) forget(w http.ResponseWriter) { |
| 309 | if a.door != nil { |
| 310 | a.door.jar.Clear(w) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // forgetChainReturn is where the chained Forget may send the browser: an |
| 315 | // app's own signed-out page, so exactly <registered consumer origin> |
| 316 | // + "/signed-out", no query, no fragment, no userinfo. The URL is built |
| 317 | // from the matched registration, never echoed. Anything else is refused, |
| 318 | // and the caller lands on id.*'s own signed-out page. |
| 319 | func (a *App) forgetChainReturn(raw string) (string, error) { |
| 320 | u, err := url.Parse(raw) |
| 321 | if err != nil || u.User != nil || u.Opaque != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || u.RawFragment != "" || |
| 322 | u.Path != "/signed-out" || u.RawPath != "" || u.Scheme == "" || u.Host == "" { |
| 323 | return "", errForgetReturn |
| 324 | } |
| 325 | matched, ok := a.consumerOrigin(u.Scheme + "://" + u.Host) |
| 326 | if !ok { |
| 327 | return "", errForgetReturn |
| 328 | } |
| 329 | return matched + "/signed-out", nil |
| 330 | } |
| 331 | |
| 332 | var errForgetReturn = errors.New("home: forget return is not a consumer's signed-out page") |
| 333 | |
| 334 | // handleForgetChainPage is GET /forget?return=<an app's signed-out URL>, |
| 335 | // the second half of a shared computer's sign-out: the app has forgotten |
| 336 | // this browser on its own host, and links here so id.* can forget it too, |
| 337 | // because each host remembers separately. One button, a same-origin POST, |
| 338 | // and a quiet "Not now" back to the app. A GET changes nothing. |
| 339 | func (a *App) handleForgetChainPage(w http.ResponseWriter, r *http.Request) { |
| 340 | w.Header().Set("Cache-Control", "no-store") |
| 341 | back, err := a.forgetChainReturn(r.URL.Query().Get("return")) |
| 342 | data := map[string]any{"Back": "/signed-out"} |
| 343 | if err == nil { |
| 344 | data["Return"], data["Back"] = back, back |
| 345 | // form-action governs the POST's redirect too, so the page names |
| 346 | // the one consumer it may land on (setConsumerPolicy only ever |
| 347 | // adds a registered one). |
| 348 | a.setConsumerPolicy(w, strings.TrimSuffix(back, "/signed-out")) |
| 349 | } |
| 350 | a.render(w, r, http.StatusOK, "id_forget.html", data) |
| 351 | } |
| 352 | |
| 353 | // handleForgetChain is POST /forget: forget this browser on id.*, then |
| 354 | // back to the app's signed-out page if return names one, else id.*'s own. |
| 355 | func (a *App) handleForgetChain(w http.ResponseWriter, r *http.Request) { |
| 356 | w.Header().Set("Cache-Control", "no-store") |
| 357 | if err := r.ParseForm(); err != nil { |
| 358 | a.errorPage(w, r, http.StatusBadRequest) |
| 359 | return |
| 360 | } |
| 361 | a.forget(w) |
| 362 | to := "/signed-out" |
| 363 | if back, err := a.forgetChainReturn(r.PostFormValue("return")); err == nil { |
| 364 | to = back |
| 365 | } |
| 366 | http.Redirect(w, r, to, http.StatusSeeOther) |
| 367 | } |
| 368 | |