| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | |
| 8 | "amadan.net/oficina/home/handoff" |
| 9 | ) |
| 10 | |
| 11 | // The sign-in door's three routes (Home spec 2026-11-19-smart-door.md, |
| 12 | // "Routes, and every state the door must answer"). Mount them with GET: |
| 13 | // |
| 14 | // - SigninPath at Routes.SigninDoor: the door, always. The app draws it. |
| 15 | // - SigninLocalPath at Routes.SigninLocal: the door with email as the one |
| 16 | // way in. connect sends people here when Oficina ID cannot help them |
| 17 | // (unknown, failed, a link to resume, "Connect my existing account", a |
| 18 | // step-up for linking), so it never offers Oficina ID, which would loop. |
| 19 | // - SigninOficinaPath at Routes.SigninOficina: "Continue with Oficina ID", |
| 20 | // which starts the Home exchange. |
| 21 | const ( |
| 22 | SigninPath = "/signin" |
| 23 | SigninLocalPath = "/signin/local" |
| 24 | SigninOficinaPath = "/signin/oficina" |
| 25 | ) |
| 26 | |
| 27 | // doorKey marks a request connect's door routes handed to the app's door |
| 28 | // (RoutesConfig.LocalSignin), with whether that door may offer Oficina ID. |
| 29 | type doorKey struct{} |
| 30 | |
| 31 | type doorOffer struct{ oficina bool } |
| 32 | |
| 33 | // OficinaHref is the href of "Continue with Oficina ID" on the door being |
| 34 | // drawn for r: the app's auth.FederatedDoor.Href. It is SigninOficinaPath, |
| 35 | // with r's return_to when that is one safe local path (rastrillo's |
| 36 | // SigninState.FederatedHref sets the page's own return_to on it again). |
| 37 | // |
| 38 | // It is "" whenever the door must not offer Oficina ID, and the app then |
| 39 | // sets no FederatedDoor at all: |
| 40 | // - on SigninLocalPath, where offering it would loop; |
| 41 | // - on a step-up (any reauth in the query). A live id.* session hands back |
| 42 | // an old auth_time, and linking needs FreshLocal, which only a local |
| 43 | // sign-in gives, so a step-up offers local ways only; |
| 44 | // - on any request that did not come through Routes.SigninDoor, so a door |
| 45 | // drawn from somewhere else offers nothing it was not told to. |
| 46 | func OficinaHref(r *http.Request) string { |
| 47 | offer, ok := r.Context().Value(doorKey{}).(doorOffer) |
| 48 | if !ok || !offer.oficina { |
| 49 | return "" |
| 50 | } |
| 51 | q, err := url.ParseQuery(r.URL.RawQuery) |
| 52 | if err != nil || q.Has("reauth") { |
| 53 | return "" |
| 54 | } |
| 55 | if path, ok := requestedReturn(q); ok { |
| 56 | return SigninOficinaPath + "?" + url.Values{"return_to": {path}}.Encode() |
| 57 | } |
| 58 | return SigninOficinaPath |
| 59 | } |
| 60 | |
| 61 | // requestedReturn is the query's return_to when it is exactly one safe local |
| 62 | // path: connect's rule for every destination it is handed. |
| 63 | func requestedReturn(q url.Values) (string, bool) { |
| 64 | values, ok := q["return_to"] |
| 65 | if !ok || len(values) != 1 || !handoff.SafePath(values[0]) { |
| 66 | return "", false |
| 67 | } |
| 68 | return values[0], true |
| 69 | } |
| 70 | |
| 71 | // doorStates are the query keys that name a state of the door itself: a |
| 72 | // link sent, a problem, a failed Keymail's force, a step-up, a Keymail |
| 73 | // continuation. Each is drawn even for a browser with a live session, as |
| 74 | // the sent, err and force states always were; a step-up in particular is |
| 75 | // asked of somebody who is signed in. |
| 76 | var doorStates = []string{"sent", "err", "force", "reauth", "continue"} |
| 77 | |
| 78 | // SigninDoor is GET SigninPath: the door, always. It never starts a Home |
| 79 | // exchange; "Continue with Oficina ID" does that (SigninOficina), and the |
| 80 | // app's door offers it where OficinaHref says it may. |
| 81 | // |
| 82 | // A browser already signed in here (spec shared-chrome §9.4) goes straight |
| 83 | // to the validated return_to, or "/", unless the query names a state of the |
| 84 | // door (doorStates). Otherwise the request is handed to |
| 85 | // RoutesConfig.LocalSignin, the app's door. |
| 86 | func (h *Routes) SigninDoor(w http.ResponseWriter, r *http.Request) { |
| 87 | q, err := url.ParseQuery(r.URL.RawQuery) |
| 88 | if err != nil { |
| 89 | h.bridge.fail(w, r, http.StatusBadRequest) |
| 90 | return |
| 91 | } |
| 92 | state := false |
| 93 | for _, key := range doorStates { |
| 94 | state = state || q.Has(key) |
| 95 | } |
| 96 | if !state && h.signedIn(r) { |
| 97 | h.alreadySignedIn(w, r, q) |
| 98 | return |
| 99 | } |
| 100 | h.cfg.LocalSignin(w, r.WithContext(context.WithValue(r.Context(), doorKey{}, doorOffer{oficina: true}))) |
| 101 | } |
| 102 | |
| 103 | // SigninLocal is GET SigninLocalPath: the app's door with email as the one |
| 104 | // way in (OficinaHref is ""). It has no signed-in shortcut: a signed-in |
| 105 | // person whose local sign-in is too old to link is sent here to sign in |
| 106 | // again (Routes.Account). |
| 107 | func (h *Routes) SigninLocal(w http.ResponseWriter, r *http.Request) { |
| 108 | h.cfg.LocalSignin(w, r.WithContext(context.WithValue(r.Context(), doorKey{}, doorOffer{oficina: false}))) |
| 109 | } |
| 110 | |
| 111 | // SigninOficina is GET SigninOficinaPath?return_to=…: "Continue with |
| 112 | // Oficina ID". It remembers the validated return_to (or "/") in the sealed |
| 113 | // return cookie and starts the Home exchange (handoff.StartPath). A browser |
| 114 | // already signed in here goes straight to that page instead, as SigninDoor |
| 115 | // does. A step-up never reaches Home: any reauth in the query is sent to |
| 116 | // SigninLocalPath with it, because linking needs a fresh local sign-in. |
| 117 | func (h *Routes) SigninOficina(w http.ResponseWriter, r *http.Request) { |
| 118 | q, err := url.ParseQuery(r.URL.RawQuery) |
| 119 | if err != nil { |
| 120 | h.bridge.fail(w, r, http.StatusBadRequest) |
| 121 | return |
| 122 | } |
| 123 | path, ok := requestedReturn(q) |
| 124 | if !ok { |
| 125 | path = "/" |
| 126 | } |
| 127 | w.Header().Set("Cache-Control", "no-store") |
| 128 | if q.Has("reauth") { |
| 129 | local := url.Values{"reauth": {"1"}} |
| 130 | if ok { |
| 131 | local.Set("return_to", path) |
| 132 | } |
| 133 | http.Redirect(w, r, SigninLocalPath+"?"+local.Encode(), http.StatusSeeOther) |
| 134 | return |
| 135 | } |
| 136 | if h.signedIn(r) { |
| 137 | h.alreadySignedIn(w, r, q) |
| 138 | return |
| 139 | } |
| 140 | if !h.navigation.rememberReturn(w, path) { |
| 141 | h.bridge.fail(w, r, http.StatusServiceUnavailable) |
| 142 | return |
| 143 | } |
| 144 | http.Redirect(w, r, handoff.StartPath+"?"+url.Values{"return_to": {path}}.Encode(), http.StatusSeeOther) |
| 145 | } |
| 146 | |
| 147 | // alreadySignedIn sends a signed-in browser where it was headed, with no |
| 148 | // door and no second exchange. SafePath refuses /signin and /auth/*, so |
| 149 | // this cannot loop. |
| 150 | func (h *Routes) alreadySignedIn(w http.ResponseWriter, r *http.Request, q url.Values) { |
| 151 | path, ok := requestedReturn(q) |
| 152 | if !ok { |
| 153 | path = "/" |
| 154 | } |
| 155 | w.Header().Set("Cache-Control", "no-store") |
| 156 | http.Redirect(w, r, path, http.StatusSeeOther) |
| 157 | } |
| 158 | |