| 1 | // Package door is Equipo's sign-in door (Home spec |
| 2 | // 2026-11-19-smart-door.md, step 6): rastrillo's one-way sign-in screen, |
| 3 | // its ui "signin" partial fed by auth.SigninState, drawn inside estilo's |
| 4 | // door (the suite's sky, and Equipo's mark and name at the card's head) by |
| 5 | // estilo-signin-screen, with Oficina ID in the partial's federated slot. |
| 6 | // |
| 7 | // Equipo has its own Oficina ID flow (oficinaid: /auth/home, the exchange |
| 8 | // and /auth/finish) rather than Home connect's routes, so the door's three |
| 9 | // routes are Equipo's own, with connect's behaviour (connect/door.go): |
| 10 | // /signin is the door, /signin/local is the door with email as the one way |
| 11 | // in, and /signin/oficina starts Equipo's exchange. |
| 12 | // |
| 13 | // Its words are rastrillo's approved copy (rastrillo.ui.signin_*) and the |
| 14 | // signed-out page's are estilo's chrome.English, so every app in the suite |
| 15 | // says the same thing. Only the refusal is Equipo's (Look.Refusal). |
| 16 | package door |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "net/http" |
| 21 | "net/url" |
| 22 | "path" |
| 23 | "strings" |
| 24 | |
| 25 | "amadan.net/oficina/estilo.git/brand" |
| 26 | "amadan.net/oficina/estilo.git/chrome" |
| 27 | "amadan.net/oficina/estilo.git/sky" |
| 28 | "amadan.net/oficina/home/handoff" |
| 29 | "amadan.net/rastrillo/rastrillo/auth" |
| 30 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 31 | ) |
| 32 | |
| 33 | // The door's routes, and Forget's (setup.ForgetPath): connect's paths. |
| 34 | const ( |
| 35 | SigninPath = "/signin" |
| 36 | SigninLocalPath = "/signin/local" |
| 37 | SigninOficinaPath = "/signin/oficina" |
| 38 | SignedOutPath = "/signed-out" |
| 39 | // StartPath is Equipo's own Oficina ID exchange (oficinaid.Start). |
| 40 | StartPath = "/auth/home" |
| 41 | ) |
| 42 | |
| 43 | // chainOficina is the hidden field the signed-out page's Forget posts |
| 44 | // (chain=oficina) with Home configured: after forgetting this browser here, |
| 45 | // Forget sends it on to Oficina ID's own Forget. The door's "Use a |
| 46 | // different email" posts to the same Forget without it. |
| 47 | const chainOficina = "oficina" |
| 48 | |
| 49 | // Look is what the door's pages need from the app: its name and mark, its |
| 50 | // two strings, the path auth's Forget is mounted at, and the page renderer |
| 51 | // (templates/door.html in Equipo). |
| 52 | type Look struct { |
| 53 | // Brand is the app's name, the door's head and rastrillo's |
| 54 | // "Sign in to {name}". |
| 55 | Brand string |
| 56 | // Oficina is the federated door's name, "Oficina ID": rastrillo draws |
| 57 | // "Continue with {name}". |
| 58 | Oficina string |
| 59 | // Refusal is the app's refusal of a verified address, or of a session |
| 60 | // the signed-in routes no longer admit. |
| 61 | Refusal string |
| 62 | // Forget is where rastrillo auth's Forget is mounted. |
| 63 | Forget string |
| 64 | // Render writes page with status. |
| 65 | Render func(w http.ResponseWriter, status int, page Page) |
| 66 | } |
| 67 | |
| 68 | // Page is one door page. Exactly one of Screen and SignedOut is set: |
| 69 | // rastrillo's screen in estilo's door (every sign-in state, the confirm |
| 70 | // step and the refusal included), or the page Sign out lands on. |
| 71 | type Page struct { |
| 72 | Screen *chrome.SignInScreen |
| 73 | SignedOut *chrome.SignInNotice |
| 74 | // HomeSignout is Oficina ID's own sign-out, offered on the signed-out |
| 75 | // page after a Home sign-in (oficinaid.SignedOut). |
| 76 | HomeSignout string |
| 77 | } |
| 78 | |
| 79 | // Icon is Equipo's mark at the head of every door card: estilo's brand |
| 80 | // icon, served by Static. |
| 81 | func Icon() string { return brand.Path("equipo") } |
| 82 | |
| 83 | // Static mounts what the door's pages fetch that is not a file of the |
| 84 | // app's: the sky's layers and Equipo's mark. Public artwork, no session. |
| 85 | func Static(mux *http.ServeMux) { |
| 86 | mux.Handle("GET "+sky.Prefix, sky.Handler()) |
| 87 | mux.Handle("GET "+brand.Prefix, brand.Handler()) |
| 88 | } |
| 89 | |
| 90 | // offerKey marks a request whose door may offer Oficina ID: GET /signin |
| 91 | // with Home configured, as connect's doorKey does. |
| 92 | type offerKey struct{} |
| 93 | |
| 94 | // refusedKey marks a request the door draws with the refusal. |
| 95 | type refusedKey struct{} |
| 96 | |
| 97 | func withOffer(r *http.Request) *http.Request { |
| 98 | return r.WithContext(context.WithValue(r.Context(), offerKey{}, true)) |
| 99 | } |
| 100 | |
| 101 | // withRefusal marks r as a refusal the door draws: a verified address |
| 102 | // Authorize refused (auth's Refused), or a session the signed-in routes no |
| 103 | // longer admit (refusedSession). Only on the plain door: the chooser |
| 104 | // (?ways=) and a way's own page (?way=) are how a refused person reaches |
| 105 | // another way in, another Oficina ID account included, and a refusal there |
| 106 | // would draw the field again and leave "Other ways to sign in" going |
| 107 | // nowhere (Fable on Docs #139, N1). Never from the query: a query |
| 108 | // parameter is something anybody can type. |
| 109 | func withRefusal(r *http.Request) *http.Request { |
| 110 | if choosing(r) { |
| 111 | return r |
| 112 | } |
| 113 | return r.WithContext(context.WithValue(r.Context(), refusedKey{}, true)) |
| 114 | } |
| 115 | |
| 116 | func choosing(r *http.Request) bool { |
| 117 | q := r.URL.Query() |
| 118 | return q.Has("ways") || q.Has("way") |
| 119 | } |
| 120 | |
| 121 | func refusedHere(r *http.Request) bool { |
| 122 | v, _ := r.Context().Value(refusedKey{}).(bool) |
| 123 | return v |
| 124 | } |
| 125 | |
| 126 | // Draw is the door for r: setup's SessionHandler.Door, so auth's |
| 127 | // RenderConfirm (the confirm step an emailed link opens) and Refused draw |
| 128 | // it too. rastrillo decides what the door shows from the query and this |
| 129 | // browser's cookies (SigninState, with SigninOneWay drawing one way in per |
| 130 | // page); Equipo adds Oficina ID where the route may offer it, and the |
| 131 | // refusal. Every door is drawn signed out: the page has no bar, nav or |
| 132 | // account, even for a session it has just refused. |
| 133 | func (l Look) Draw(a *auth.Auth, w http.ResponseWriter, r *http.Request, status int, refused bool) { |
| 134 | if a == nil || l.Render == nil { |
| 135 | http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable) |
| 136 | return |
| 137 | } |
| 138 | if refused { |
| 139 | r = withRefusal(r) |
| 140 | } |
| 141 | st := a.SigninState(r) |
| 142 | if st.Unavailable { |
| 143 | // Under a base path the hub signs people in: estilo draws nothing, |
| 144 | // and every route rastrillo's door names answers 404 there. |
| 145 | http.NotFound(w, r) |
| 146 | return |
| 147 | } |
| 148 | if href := oficinaHref(r); href != "" { |
| 149 | st.Federated = &auth.FederatedDoor{Provider: lastsignin.ProviderOficina, Href: href, Label: l.Oficina, Icon: chrome.OficinaGlyph} |
| 150 | } |
| 151 | if refusedHere(r) { |
| 152 | st.AppProblem = &auth.AppProblem{Text: l.Refusal, Ask: true} |
| 153 | } |
| 154 | if signedOutPage(st.ReturnTo) { |
| 155 | // A fresh sign-in must not land on "You’re signed out". |
| 156 | st.ReturnTo = "" |
| 157 | } |
| 158 | a.PrepareSigninResponse(w, st) |
| 159 | w.Header().Set("Cache-Control", "no-store") |
| 160 | l.Render(w, status, Page{Screen: &chrome.SignInScreen{Icon: Icon(), App: l.Brand, Sky: sky.For(r.Host), State: st}}) |
| 161 | } |
| 162 | |
| 163 | // SignedOut is where Sign out lands, in the door's words: "You’re signed |
| 164 | // out", Sign in, then the quiet "Forget me on this device", a POST to |
| 165 | // auth's Forget, and with chain set (Home configured) on to Oficina ID's |
| 166 | // own Forget. homeSignout, when set, is Oficina ID's sign-out, offered |
| 167 | // after a Home sign-in. |
| 168 | func (l Look) SignedOut(w http.ResponseWriter, r *http.Request, chain bool, homeSignout string) { |
| 169 | var hidden []chrome.Hidden |
| 170 | if chain { |
| 171 | hidden = append(hidden, chrome.Hidden{Name: "chain", Value: chainOficina}) |
| 172 | } |
| 173 | out := chrome.SignInNotice{Icon: Icon(), App: l.Brand, Sky: sky.For(r.Host)}. |
| 174 | SignedOut(SigninPath).Forgetting(l.Forget, "", hidden...) |
| 175 | w.Header().Set("Cache-Control", "no-store") |
| 176 | l.Render(w, http.StatusOK, Page{SignedOut: &out, HomeSignout: homeSignout}) |
| 177 | } |
| 178 | |
| 179 | // oficinaHref is "Continue with Oficina ID" on the door being drawn for |
| 180 | // r, as connect's OficinaHref: /signin/oficina, with r's return_to when it |
| 181 | // is one safe page. It is "" wherever the door must not offer Oficina ID: |
| 182 | // on /signin/local, on any step-up (a live id.* session hands back an old |
| 183 | // auth_time, and linking needs a fresh local sign-in), and on any door not |
| 184 | // drawn by GET /signin with Home configured. |
| 185 | func oficinaHref(r *http.Request) string { |
| 186 | if v, _ := r.Context().Value(offerKey{}).(bool); !v { |
| 187 | return "" |
| 188 | } |
| 189 | q, err := url.ParseQuery(r.URL.RawQuery) |
| 190 | if err != nil || q.Has("reauth") { |
| 191 | return "" |
| 192 | } |
| 193 | if path, ok := requestedReturn(q); ok { |
| 194 | return SigninOficinaPath + "?" + url.Values{"return_to": {path}}.Encode() |
| 195 | } |
| 196 | return SigninOficinaPath |
| 197 | } |
| 198 | |
| 199 | // requestedReturn is the query's return_to when it is exactly one page a |
| 200 | // sign-in may land on (returnPath). |
| 201 | func requestedReturn(q url.Values) (string, bool) { |
| 202 | values, ok := q["return_to"] |
| 203 | if !ok || len(values) != 1 || !returnPath(values[0]) { |
| 204 | return "", false |
| 205 | } |
| 206 | return values[0], true |
| 207 | } |
| 208 | |
| 209 | // returnPath is handoff.SafePath (no scheme, no host, no "//", no control |
| 210 | // characters, not /signin or /auth/*), and neither the Oficina ID link's |
| 211 | // own pages (oficinaid.Start refuses them) nor /signed-out. |
| 212 | func returnPath(p string) bool { |
| 213 | return handoff.SafePath(p) && !strings.HasPrefix(p, "/account/home/") && !signedOutPage(p) |
| 214 | } |
| 215 | |
| 216 | // signedOutPage reports whether p is /signed-out, with or without a query. |
| 217 | func signedOutPage(p string) bool { |
| 218 | if p == "" { |
| 219 | return false |
| 220 | } |
| 221 | u, err := url.Parse(p) |
| 222 | return err != nil || path.Clean("/"+strings.ReplaceAll(u.Path, "\\", "/")) == SignedOutPath |
| 223 | } |
| 224 | |