package connect import ( "context" "net/http" "net/url" "amadan.net/oficina/home/handoff" ) // The sign-in door's three routes (Home spec 2026-11-19-smart-door.md, // "Routes, and every state the door must answer"). Mount them with GET: // // - SigninPath at Routes.SigninDoor: the door, always. The app draws it. // - SigninLocalPath at Routes.SigninLocal: the door with email as the one // way in. connect sends people here when Oficina ID cannot help them // (unknown, failed, a link to resume, "Connect my existing account", a // step-up for linking), so it never offers Oficina ID, which would loop. // - SigninOficinaPath at Routes.SigninOficina: "Continue with Oficina ID", // which starts the Home exchange. const ( SigninPath = "/signin" SigninLocalPath = "/signin/local" SigninOficinaPath = "/signin/oficina" ) // doorKey marks a request connect's door routes handed to the app's door // (RoutesConfig.LocalSignin), with whether that door may offer Oficina ID. type doorKey struct{} type doorOffer struct{ oficina bool } // OficinaHref is the href of "Continue with Oficina ID" on the door being // drawn for r: the app's auth.FederatedDoor.Href. It is SigninOficinaPath, // with r's return_to when that is one safe local path (rastrillo's // SigninState.FederatedHref sets the page's own return_to on it again). // // It is "" whenever the door must not offer Oficina ID, and the app then // sets no FederatedDoor at all: // - on SigninLocalPath, where offering it would loop; // - on a step-up (any reauth in the query). A live id.* session hands back // an old auth_time, and linking needs FreshLocal, which only a local // sign-in gives, so a step-up offers local ways only; // - on any request that did not come through Routes.SigninDoor, so a door // drawn from somewhere else offers nothing it was not told to. func OficinaHref(r *http.Request) string { offer, ok := r.Context().Value(doorKey{}).(doorOffer) if !ok || !offer.oficina { return "" } q, err := url.ParseQuery(r.URL.RawQuery) if err != nil || q.Has("reauth") { return "" } if path, ok := requestedReturn(q); ok { return SigninOficinaPath + "?" + url.Values{"return_to": {path}}.Encode() } return SigninOficinaPath } // requestedReturn is the query's return_to when it is exactly one safe local // path: connect's rule for every destination it is handed. func requestedReturn(q url.Values) (string, bool) { values, ok := q["return_to"] if !ok || len(values) != 1 || !handoff.SafePath(values[0]) { return "", false } return values[0], true } // doorStates are the query keys that name a state of the door itself: a // link sent, a problem, a failed Keymail's force, a step-up, a Keymail // continuation. Each is drawn even for a browser with a live session, as // the sent, err and force states always were; a step-up in particular is // asked of somebody who is signed in. var doorStates = []string{"sent", "err", "force", "reauth", "continue"} // SigninDoor is GET SigninPath: the door, always. It never starts a Home // exchange; "Continue with Oficina ID" does that (SigninOficina), and the // app's door offers it where OficinaHref says it may. // // A browser already signed in here (spec shared-chrome §9.4) goes straight // to the validated return_to, or "/", unless the query names a state of the // door (doorStates). Otherwise the request is handed to // RoutesConfig.LocalSignin, the app's door. func (h *Routes) SigninDoor(w http.ResponseWriter, r *http.Request) { q, err := url.ParseQuery(r.URL.RawQuery) if err != nil { h.bridge.fail(w, r, http.StatusBadRequest) return } state := false for _, key := range doorStates { state = state || q.Has(key) } if !state && h.signedIn(r) { h.alreadySignedIn(w, r, q) return } h.cfg.LocalSignin(w, r.WithContext(context.WithValue(r.Context(), doorKey{}, doorOffer{oficina: true}))) } // SigninLocal is GET SigninLocalPath: the app's door with email as the one // way in (OficinaHref is ""). It has no signed-in shortcut: a signed-in // person whose local sign-in is too old to link is sent here to sign in // again (Routes.Account). func (h *Routes) SigninLocal(w http.ResponseWriter, r *http.Request) { h.cfg.LocalSignin(w, r.WithContext(context.WithValue(r.Context(), doorKey{}, doorOffer{oficina: false}))) } // SigninOficina is GET SigninOficinaPath?return_to=…: "Continue with // Oficina ID". It remembers the validated return_to (or "/") in the sealed // return cookie and starts the Home exchange (handoff.StartPath). A browser // already signed in here goes straight to that page instead, as SigninDoor // does. A step-up never reaches Home: any reauth in the query is sent to // SigninLocalPath with it, because linking needs a fresh local sign-in. func (h *Routes) SigninOficina(w http.ResponseWriter, r *http.Request) { q, err := url.ParseQuery(r.URL.RawQuery) if err != nil { h.bridge.fail(w, r, http.StatusBadRequest) return } path, ok := requestedReturn(q) if !ok { path = "/" } w.Header().Set("Cache-Control", "no-store") if q.Has("reauth") { local := url.Values{"reauth": {"1"}} if ok { local.Set("return_to", path) } http.Redirect(w, r, SigninLocalPath+"?"+local.Encode(), http.StatusSeeOther) return } if h.signedIn(r) { h.alreadySignedIn(w, r, q) return } if !h.navigation.rememberReturn(w, path) { h.bridge.fail(w, r, http.StatusServiceUnavailable) return } http.Redirect(w, r, handoff.StartPath+"?"+url.Values{"return_to": {path}}.Encode(), http.StatusSeeOther) } // alreadySignedIn sends a signed-in browser where it was headed, with no // door and no second exchange. SafePath refuses /signin and /auth/*, so // this cannot loop. func (h *Routes) alreadySignedIn(w http.ResponseWriter, r *http.Request, q url.Values) { path, ok := requestedReturn(q) if !ok { path = "/" } w.Header().Set("Cache-Control", "no-store") http.Redirect(w, r, path, http.StatusSeeOther) }