| 1 | package web |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "amadan.net/rastrillo/rastrillo/auth" |
| 7 | ) |
| 8 | |
| 9 | type signinPage struct { |
| 10 | Name string |
| 11 | State auth.SigninState |
| 12 | Refused bool |
| 13 | ProblemText string |
| 14 | Confirm *auth.ConfirmPageData |
| 15 | // Oficina is where "Sign in with Oficina" goes (/auth/start), set only |
| 16 | // on /signin with Oficina ID on. |
| 17 | Oficina string |
| 18 | // Linking says an Oficina link is waiting on this local sign-in, which |
| 19 | // only an emailed link or Keymail can give: the passkey is hidden. |
| 20 | Linking bool |
| 21 | } |
| 22 | |
| 23 | var problemText = map[auth.SigninProblem]string{ |
| 24 | auth.ProblemRate: "Too many tries. Try again in a few minutes.", |
| 25 | auth.ProblemAddress: "That doesn't look like an email address.", |
| 26 | auth.ProblemExpired: "That link has expired or was already used. Send a new one.", |
| 27 | auth.ProblemKeymail: "Keymail couldn't confirm it's you.", |
| 28 | auth.ProblemGeneric: "Something went wrong. Try again.", |
| 29 | auth.ProblemReauth: "Sign in again to continue.", |
| 30 | auth.ProblemCheck: "Your browser couldn't finish a security check. Try again.", |
| 31 | } |
| 32 | |
| 33 | // signin is the email and passkey sign-in page: /signin, or /signin/local |
| 34 | // with Oficina ID on. |
| 35 | func (a *app) signin(w http.ResponseWriter, r *http.Request) { a.signinWith(w, r, "") } |
| 36 | |
| 37 | // signinWith is the sign-in page, led by "Sign in with Oficina" to oficina |
| 38 | // when that is set. |
| 39 | func (a *app) signinWith(w http.ResponseWriter, r *http.Request, oficina string) { |
| 40 | state := a.auth.SigninState(r) |
| 41 | linking := a.oficina != nil && linkPending(r) |
| 42 | if !linking { |
| 43 | door := passkeyDoor |
| 44 | state.Passkey = &door |
| 45 | } |
| 46 | a.auth.PrepareSigninResponse(w, state) |
| 47 | w.Header().Set("Cache-Control", "no-store") |
| 48 | a.render(w, r, "signin", signinPage{ |
| 49 | Name: a.cfg.Name, State: state, |
| 50 | Refused: r.URL.Query().Get("refused") == "1", |
| 51 | ProblemText: problemText[state.Problem], |
| 52 | Oficina: oficina, |
| 53 | Linking: linking, |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | // renderConfirm draws the page an emailed link lands on: one button that |
| 58 | // posts the token, so a mail scanner opening the link spends nothing. |
| 59 | func (a *app) renderConfirm(w http.ResponseWriter, r *http.Request, d auth.ConfirmPageData) { |
| 60 | w.Header().Set("Cache-Control", "no-store") |
| 61 | a.render(w, r, "confirm", signinPage{Name: a.cfg.Name, Confirm: &d}) |
| 62 | } |
| 63 | |
| 64 | // render draws a page in the signed-in member's Appearance; with no member |
| 65 | // (the sign-in pages), in the device's. |
| 66 | func (a *app) render(w http.ResponseWriter, r *http.Request, name string, data any) { |
| 67 | if err := a.pages.PageTheme(w, http.StatusOK, name, data, themeOf(r)); err != nil { |
| 68 | a.logger.Error("render", "page", name, "err", err) |
| 69 | http.Error(w, "something went wrong", http.StatusInternalServerError) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // linkNavigationCookie is the sealed marker connect leaves when it sends |
| 74 | // someone here to sign in locally on the way to linking: from "Connect your |
| 75 | // Oficina account" without a fresh sign-in, or after an Oficina sign-in with |
| 76 | // no link. TestOficinaLocalSigninDuringLinkHidesPasskey goes through the |
| 77 | // second for real, so it fails if connect renames the cookie. |
| 78 | const linkNavigationCookie = "__Host-oficina-local-link-navigation" |
| 79 | |
| 80 | // linkPending reports whether connect's marker is present. Base cannot open |
| 81 | // it, and need not: at worst a stale marker hides the passkey for the ten |
| 82 | // minutes it lives. |
| 83 | func linkPending(r *http.Request) bool { |
| 84 | c, err := r.Cookie(linkNavigationCookie) |
| 85 | return err == nil && c.Value != "" |
| 86 | } |
| 87 | |
| 88 | // themeOf is the signed-in member's Appearance, "" (the device's) without one. |
| 89 | func themeOf(r *http.Request) string { |
| 90 | me, _ := CurrentMember(r) |
| 91 | return me.Theme |
| 92 | } |
| 93 | |