package web import ( "net/http" "amadan.net/rastrillo/rastrillo/auth" ) type signinPage struct { Name string State auth.SigninState Refused bool ProblemText string Confirm *auth.ConfirmPageData // Oficina is where "Sign in with Oficina" goes (/auth/start), set only // on /signin with Oficina ID on. Oficina string // Linking says an Oficina link is waiting on this local sign-in, which // only an emailed link or Keymail can give: the passkey is hidden. Linking bool } var problemText = map[auth.SigninProblem]string{ auth.ProblemRate: "Too many tries. Try again in a few minutes.", auth.ProblemAddress: "That doesn't look like an email address.", auth.ProblemExpired: "That link has expired or was already used. Send a new one.", auth.ProblemKeymail: "Keymail couldn't confirm it's you.", auth.ProblemGeneric: "Something went wrong. Try again.", auth.ProblemReauth: "Sign in again to continue.", auth.ProblemCheck: "Your browser couldn't finish a security check. Try again.", } // signin is the email and passkey sign-in page: /signin, or /signin/local // with Oficina ID on. func (a *app) signin(w http.ResponseWriter, r *http.Request) { a.signinWith(w, r, "") } // signinWith is the sign-in page, led by "Sign in with Oficina" to oficina // when that is set. func (a *app) signinWith(w http.ResponseWriter, r *http.Request, oficina string) { state := a.auth.SigninState(r) linking := a.oficina != nil && linkPending(r) if !linking { door := passkeyDoor state.Passkey = &door } a.auth.PrepareSigninResponse(w, state) w.Header().Set("Cache-Control", "no-store") a.render(w, r, "signin", signinPage{ Name: a.cfg.Name, State: state, Refused: r.URL.Query().Get("refused") == "1", ProblemText: problemText[state.Problem], Oficina: oficina, Linking: linking, }) } // renderConfirm draws the page an emailed link lands on: one button that // posts the token, so a mail scanner opening the link spends nothing. func (a *app) renderConfirm(w http.ResponseWriter, r *http.Request, d auth.ConfirmPageData) { w.Header().Set("Cache-Control", "no-store") a.render(w, r, "confirm", signinPage{Name: a.cfg.Name, Confirm: &d}) } // render draws a page in the signed-in member's Appearance; with no member // (the sign-in pages), in the device's. func (a *app) render(w http.ResponseWriter, r *http.Request, name string, data any) { if err := a.pages.PageTheme(w, http.StatusOK, name, data, themeOf(r)); err != nil { a.logger.Error("render", "page", name, "err", err) http.Error(w, "something went wrong", http.StatusInternalServerError) } } // linkNavigationCookie is the sealed marker connect leaves when it sends // someone here to sign in locally on the way to linking: from "Connect your // Oficina account" without a fresh sign-in, or after an Oficina sign-in with // no link. TestOficinaLocalSigninDuringLinkHidesPasskey goes through the // second for real, so it fails if connect renames the cookie. const linkNavigationCookie = "__Host-oficina-local-link-navigation" // linkPending reports whether connect's marker is present. Base cannot open // it, and need not: at worst a stale marker hides the passkey for the ten // minutes it lives. func linkPending(r *http.Request) bool { c, err := r.Cookie(linkNavigationCookie) return err == nil && c.Value != "" } // themeOf is the signed-in member's Appearance, "" (the device's) without one. func themeOf(r *http.Request) string { me, _ := CurrentMember(r) return me.Theme }