| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/estilo.git/chrome" |
| 12 | "amadan.net/rastrillo/rastrillo/auth" |
| 13 | "amadan.net/rastrillo/rastrillo/sessions" |
| 14 | "github.com/keymaildev/signin" |
| 15 | ) |
| 16 | |
| 17 | // Origin is the credential origin: the host that renders sign-in, holds |
| 18 | // the session cookie and is the WebAuthn RP. §4 splits it from the |
| 19 | // directory origin deliberately, and both the CSRF check and the cookie |
| 20 | // attributes are keyed on this exact string. |
| 21 | const Origin = "https://" + credentialHost |
| 22 | |
| 23 | // linkPath is where magic links land. It must match the route mounted |
| 24 | // below; signin's own default is /auth/verify, which this app does not |
| 25 | // serve. |
| 26 | const linkPath = "/signin/verify" |
| 27 | |
| 28 | // callbackPath is where keymail returns. signin.Keymail defaults to |
| 29 | // /auth/callback, so setting this is not optional — mounting the route |
| 30 | // without it leaves the two disagreeing and every keymail sign-in |
| 31 | // failing at the last hop. |
| 32 | const callbackPath = "/signin/callback" |
| 33 | |
| 34 | // pendingCookie carries the sealed keymail pending blob between Begin |
| 35 | // and the callback. |
| 36 | // |
| 37 | // Every attribute here is load-bearing, and they are the reason the |
| 38 | // callback is allowed to mint a session on a GET where the magic link is |
| 39 | // not. __Host- pins it to this exact origin with no Domain and Path=/, |
| 40 | // so no sibling host can set or read it. HttpOnly keeps script out. |
| 41 | // SameSite=Lax rather than Strict because Strict would break the keymail |
| 42 | // return itself, which is a top-level navigation from another origin. |
| 43 | // Lax does NOT stop this attack shape — it sends the cookie on exactly |
| 44 | // that navigation — and an earlier version of this comment claimed it |
| 45 | // did. What stops an attacker is that the blob is HMAC'd under a config |
| 46 | // key they do not have, and that the sealed state must match. |
| 47 | // |
| 48 | // It is cleared on read. That is worth doing and it is NOT what makes a |
| 49 | // blob single-use: clearing touches one browser's cookie jar and the |
| 50 | // blob stays a valid self-contained value until it expires. An earlier |
| 51 | // version of this comment said "cleared on read, so a blob is good for |
| 52 | // exactly one callback", and a test written to check it found the |
| 53 | // replay went through. What actually makes the round trip single-use is |
| 54 | // the authorization code, which the federation server redeems once — |
| 55 | // standard OAuth, and the test stub models it because a stub that did |
| 56 | // not was how the false claim survived. |
| 57 | const pendingCookie = "__Host-oficina-signin" |
| 58 | |
| 59 | // pendingTTL bounds how long a half-finished keymail round trip stays |
| 60 | // usable. signin's own PendingTTL defaults to 10 minutes; the cookie is |
| 61 | // given the same life so the two expire together rather than leaving a |
| 62 | // cookie that outlives the blob it carries. |
| 63 | const pendingTTL = 10 * time.Minute |
| 64 | |
| 65 | // classifierTimeout bounds the classifier's outbound calls. |
| 66 | // |
| 67 | // The exposure is wider than "slow", and worth naming precisely. The |
| 68 | // classifier resolves _keymail.<domain> for whatever domain was typed |
| 69 | // and then fetches from the host that TXT record names — a value the |
| 70 | // submitter's DNS controls, unvalidated, which may be a port or an |
| 71 | // internal address. So this is an outbound fetch to an attacker-chosen |
| 72 | // host, and the same host becomes the token exchange's Base. It is |
| 73 | // bounded by the fact that csrf.Protect blocks the cross-origin POST, |
| 74 | // so a victim has to type the address themselves; the timeout bounds |
| 75 | // how long one costs us. Neither makes the host safe, and a future |
| 76 | // change that widens who can reach this endpoint should read this |
| 77 | // first. |
| 78 | const classifierTimeout = 5 * time.Second |
| 79 | |
| 80 | // newFlow wires signin.Flow. Exactly one per process: the rate limiter's |
| 81 | // state lives on the value, so one per request would hand every caller a |
| 82 | // fresh empty budget and rate-limit nothing, silently. |
| 83 | func newFlow(secrets Secrets, links signin.LinkStore, origin string) *signin.Flow { |
| 84 | http := &http.Client{Timeout: classifierTimeout} |
| 85 | return &signin.Flow{ |
| 86 | Classifier: &signin.Classifier{HTTP: http, Scheme: "https"}, |
| 87 | Keymail: func(server string) *signin.Keymail { |
| 88 | return &signin.Keymail{ |
| 89 | Base: "https://" + server, |
| 90 | Origin: origin, |
| 91 | RedirectPath: callbackPath, |
| 92 | HTTP: http, |
| 93 | } |
| 94 | }, |
| 95 | Links: links, |
| 96 | Mailer: secrets.Mailer, |
| 97 | Key: secrets.SigninKey, |
| 98 | Origin: origin, |
| 99 | LinkPath: linkPath, |
| 100 | // The suite's subject and body (estilo chrome.English), so the |
| 101 | // email says how long the link lasts rather than keymaildev's |
| 102 | // default "expires shortly", and names Oficina, never "Home". |
| 103 | Subject: chrome.EmailSubject(suiteName), |
| 104 | Body: chrome.EmailBody(suiteName), |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // clientIP is the rate limiter's per-IP key, and getting it wrong is a |
| 109 | // suite-wide outage rather than a missing nicety. |
| 110 | // |
| 111 | // Over a Linux unix socket r.RemoteAddr is "@". net.SplitHostPort("@") |
| 112 | // fails, so a naive implementation passes "@" through — and signin skips |
| 113 | // the per-IP budget only when the key is exactly "". Every request in |
| 114 | // the world would then share the key "@" with a budget of 20 per 15 |
| 115 | // minutes: twenty POSTs would lock sign-in out for the whole suite for a |
| 116 | // quarter of an hour, and §2.1 already names this service the suite's |
| 117 | // single point of failure. |
| 118 | // |
| 119 | // Taking the LAST element is the carlos edge's documented contract, not |
| 120 | // a guess. Verified in carlos/platform at 97eb53f: |
| 121 | // |
| 122 | // - stripForwardedHeaders deletes inbound Forwarded and |
| 123 | // X-Forwarded-*, and ReverseProxy's Rewrite deletes them from the |
| 124 | // outbound request before Rewrite runs, so a client cannot forge |
| 125 | // this header at all. |
| 126 | // - SetXForwarded then writes a single address: the peer that dialled. |
| 127 | // - On a verified edge-to-edge hop the inbound chain is restored |
| 128 | // VERBATIM rather than appended to, and setForwarded says why in as |
| 129 | // many words: "the last element has to stay the client." It cites |
| 130 | // the platform's own consumer, internal/console/authcli.go's |
| 131 | // clientIP — "the LAST element is the one to take". |
| 132 | // |
| 133 | // Appending would have rate-limited every backhauled request as one |
| 134 | // bucket per region, which is the same collapse this function exists to |
| 135 | // avoid, one layer up. Falling back to RemoteAddr covers a direct local |
| 136 | // connection. |
| 137 | // |
| 138 | // Returning "" is deliberate where no IP is knowable: no per-IP budget |
| 139 | // is strictly better than one global budget shared by everyone, and the |
| 140 | // per-address budget still applies. |
| 141 | // |
| 142 | // The residual, stated because nothing in the code checks it: all of |
| 143 | // the above is true BEHIND THE CARLOS EDGE. Run directly — local |
| 144 | // development, an internal caller, a misconfigured unit — any client |
| 145 | // can set X-Forwarded-For per request and hand itself a fresh budget |
| 146 | // every time. The per-address budget is what still holds there. |
| 147 | func clientIP(r *http.Request) string { |
| 148 | if xff := r.Header.Get("X-Forwarded-For"); xff != "" { |
| 149 | parts := strings.Split(xff, ",") |
| 150 | if ip := strings.TrimSpace(parts[len(parts)-1]); ip != "" && ip != "@" { |
| 151 | return ip |
| 152 | } |
| 153 | } |
| 154 | if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil && host != "" && host != "@" { |
| 155 | return host |
| 156 | } |
| 157 | if r.RemoteAddr != "" && r.RemoteAddr != "@" { |
| 158 | return r.RemoteAddr |
| 159 | } |
| 160 | return "" |
| 161 | } |
| 162 | |
| 163 | // handleSigninForm draws the door (door.go): the way this browser used |
| 164 | // last, Other ways to sign in, the chooser, or one way's own page. |
| 165 | // |
| 166 | // The email override (force=1) has to survive into the POST, because the |
| 167 | // browser submits the form's action and not the URL that rendered it. |
| 168 | // Every failed Keymail return lands on /signin?force=1; without this the |
| 169 | // next attempt classified the address again and went straight back to |
| 170 | // Keymail, so "not a dead end" was not true in a browser. Dropping the |
| 171 | // flag from the action is harmless: it buys another Keymail round trip, |
| 172 | // held to the same checks. |
| 173 | func (a *App) handleSigninForm(w http.ResponseWriter, r *http.Request) { |
| 174 | rememberReturn(w, r) |
| 175 | a.renderDoor(w, r, http.StatusOK, a.doorState(w, r, doorTarget(r), "")) |
| 176 | } |
| 177 | |
| 178 | // renderSigninForm redraws the door after a failed Begin: the email field |
| 179 | // with the problem, prefilled from this browser's remembered address and |
| 180 | // never from what was posted (ruling 26: the one account-authored string |
| 181 | // id.* renders is the one Home sealed). The return cookie is unchanged by |
| 182 | // a POST, so it is where the sign-in is still going. |
| 183 | func (a *App) renderSigninForm(w http.ResponseWriter, r *http.Request, status int, problem auth.SigninProblem) { |
| 184 | a.renderDoor(w, r, status, a.doorState(w, r, readReturn(r), problem)) |
| 185 | } |
| 186 | |
| 187 | // handleSigninSent is the check-your-mail page. It is reached by |
| 188 | // redirect and asserts nothing about the address, so it is the same page |
| 189 | // whether or not anybody has an account here: rastrillo's Sent, with no |
| 190 | // address (SentTo stays empty), and "Use a different email" posting to |
| 191 | // Forget with where the sign-in was going. |
| 192 | func (a *App) handleSigninSent(w http.ResponseWriter, r *http.Request) { |
| 193 | st := a.doorState(w, r, readReturn(r), "") |
| 194 | st.Step, st.Problem = auth.StepSent, auth.ProblemNone |
| 195 | a.renderDoor(w, r, http.StatusOK, st) |
| 196 | } |
| 197 | |
| 198 | // againHref is the way back to the form that keeps where the sign-in was |
| 199 | // going: "Start again" on the failed-link page (r3 C1). A bare /signin |
| 200 | // forgets return_to (rememberReturn clears it), so the next link ended at |
| 201 | // id.* "/" and the person on the signed-out front page. |
| 202 | func againHref(r *http.Request) string { |
| 203 | if target := readReturn(r); target != "/" { |
| 204 | return "/signin?return_to=" + url.QueryEscape(target) |
| 205 | } |
| 206 | return "/signin" |
| 207 | } |
| 208 | |
| 209 | // handleSigninBegin starts a sign-in. |
| 210 | // |
| 211 | // It branches on what Begin decided. A magic link has already been sent |
| 212 | // by the time Begin returns; a keymail upgrade has sent nothing and |
| 213 | // hands back a redirect and a pending blob instead. Both answers are |
| 214 | // decided by the address's domain, which the submitter already knows, so |
| 215 | // neither discloses whether that person has an account here. |
| 216 | func (a *App) handleSigninBegin(w http.ResponseWriter, r *http.Request) { |
| 217 | if err := r.ParseForm(); err != nil { |
| 218 | a.renderSigninForm(w, r, http.StatusBadRequest, auth.ProblemGeneric) |
| 219 | return |
| 220 | } |
| 221 | address := strings.TrimSpace(r.PostFormValue("address")) |
| 222 | |
| 223 | // The force override carries a failed keymail round trip back to the |
| 224 | // magic link, so a failed approval is not a dead end. |
| 225 | var force signin.Method |
| 226 | if r.URL.Query().Get("force") == "1" || r.PostFormValue("force") == "1" { |
| 227 | force = signin.MethodMagicLink |
| 228 | } |
| 229 | next, err := a.flow.Begin(r.Context(), address, clientIP(r), force) |
| 230 | if a.beginFailed(w, r, err) { |
| 231 | return |
| 232 | } |
| 233 | |
| 234 | // A fresh step-up (max_age, implementation/fresh-assertion.md) may |
| 235 | // take the Keymail door only when Keymail will demand a new gesture. |
| 236 | // A standing Keymail inbox session approves without one, so a plain |
| 237 | // authorize URL proves nothing about freshness. The test is on the |
| 238 | // URL actually handed to the browser: the library adds prompt=login |
| 239 | // exactly when the server advertised "reauth": true (keymail serve |
| 240 | // answers it with 403 otherwise), so its presence is the fact that |
| 241 | // matters, not a prediction of it. Without it, nothing has been sent |
| 242 | // yet and the email link is what runs, as it always did here. That |
| 243 | // second Begin spends a second rate-limit token for the address; it |
| 244 | // happens only for a Keymail server that cannot step up. |
| 245 | intent, freshIntent := a.readAssertionIntent(r, time.Now()) |
| 246 | if freshIntent && intent.MaxAge != 0 && next.Kind == signin.NextKeymail && !forcesLogin(next.Redirect) { |
| 247 | next, err = a.flow.Begin(r.Context(), address, clientIP(r), signin.MethodMagicLink) |
| 248 | if a.beginFailed(w, r, err) { |
| 249 | return |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if next.Kind == signin.NextKeymail { |
| 254 | a.setPending(w, next.Pending) |
| 255 | // rastrillo's Continue step: "Taking you to Keymail", a refresh |
| 256 | // and a link, both written through attribute escaping. |
| 257 | a.renderDoor(w, r, http.StatusOK, auth.SigninState{Step: auth.StepContinue, ContinueURL: next.Redirect, OneWay: true}) |
| 258 | return |
| 259 | } |
| 260 | http.Redirect(w, r, "/signin/sent", http.StatusSeeOther) |
| 261 | } |
| 262 | |
| 263 | // beginFailed answers a failed Begin and reports whether it did. |
| 264 | func (a *App) beginFailed(w http.ResponseWriter, r *http.Request, err error) bool { |
| 265 | switch { |
| 266 | case err == nil: |
| 267 | return false |
| 268 | case errors.Is(err, signin.ErrBadAddress): |
| 269 | a.renderSigninForm(w, r, http.StatusBadRequest, auth.ProblemAddress) |
| 270 | case errors.Is(err, signin.ErrRateLimited): |
| 271 | // 429 without saying which budget: which one was hit is itself |
| 272 | // information about whether that address has been tried. |
| 273 | a.renderSigninForm(w, r, http.StatusTooManyRequests, auth.ProblemRate) |
| 274 | default: |
| 275 | a.logger.Error("signin begin failed") |
| 276 | a.renderSigninForm(w, r, http.StatusInternalServerError, auth.ProblemGeneric) |
| 277 | } |
| 278 | return true |
| 279 | } |
| 280 | |
| 281 | // handleVerifyForm renders the confirmation page for a magic link. It |
| 282 | // TOUCHES NO STATE, and that is the whole point. |
| 283 | // |
| 284 | // Redeeming on GET breaks twice. Mail security scanners prefetch links, |
| 285 | // which burns the single-use token before the person clicks it — routine, |
| 286 | // not hypothetical. And it is login CSRF: any page the victim loads can |
| 287 | // embed <img src=".../signin/verify?token=…"> and silently sign them into |
| 288 | // an account the attacker controls, after which everything they do |
| 289 | // happens in the attacker's session. |
| 290 | // |
| 291 | // It must not look the token up either. A lookup would reintroduce the |
| 292 | // oracle a prefetch could probe: a valid token would render differently |
| 293 | // from an invalid one, which is exactly the difference this page exists |
| 294 | // not to have. |
| 295 | func (a *App) handleVerifyForm(w http.ResponseWriter, r *http.Request) { |
| 296 | a.render(w, r, http.StatusOK, "signin_confirm.html", |
| 297 | map[string]any{"Token": r.URL.Query().Get("token")}) |
| 298 | } |
| 299 | |
| 300 | // handleVerify redeems a magic link. Behind csrf.Protect, on POST. |
| 301 | func (a *App) handleVerify(w http.ResponseWriter, r *http.Request) { |
| 302 | if err := r.ParseForm(); err != nil { |
| 303 | a.signinFailed(w, r) |
| 304 | return |
| 305 | } |
| 306 | id, err := a.flow.CompleteMagicLink(r.Context(), r.PostFormValue("token")) |
| 307 | if err != nil { |
| 308 | // Unknown, expired and already-used are one answer. TakeLink |
| 309 | // makes them one answer at the store; this keeps them one |
| 310 | // answer at the door. |
| 311 | a.signinFailed(w, r) |
| 312 | return |
| 313 | } |
| 314 | a.completeSignIn(w, r, id) |
| 315 | } |
| 316 | |
| 317 | // handleCallback completes a keymail round trip. |
| 318 | // |
| 319 | // This one may mint on a GET where the magic link may not, and the |
| 320 | // difference is not arbitrary: the return is bound to the browser that |
| 321 | // began the flow, by the __Host- pending cookie plus state. An attacker |
| 322 | // cannot make a victim's browser complete a flow it never started. A |
| 323 | // magic link has no such binding — anyone holding the URL can replay it |
| 324 | // — which is why that one needs the form. |
| 325 | // |
| 326 | // It never renders anything derived from the return: the host is chosen |
| 327 | // by the address, and putting an account-influenced string on the |
| 328 | // credential origin is what §4 forbids. |
| 329 | // |
| 330 | // Both the success and the ordinary failure are redirects — a failed |
| 331 | // round trip goes to /signin?force=1. Rendering happens only on a |
| 332 | // downstream failure inside completeSignIn, of which there are three: |
| 333 | // find-or-mint, the passkey Gate, and minting the session. All three |
| 334 | // show the same fixed page, carrying nothing from the round trip. |
| 335 | // |
| 336 | // This sentence is on its third revision. It said "never renders", |
| 337 | // which was false; then implied rendering was the normal failure, which |
| 338 | // was also wrong; then listed two of the three paths as though that |
| 339 | // were all of them. |
| 340 | func (a *App) handleCallback(w http.ResponseWriter, r *http.Request) { |
| 341 | blob := a.takePending(w, r) |
| 342 | q := r.URL.Query() |
| 343 | |
| 344 | id, err := a.flow.CompleteKeymail(r.Context(), blob, q.Get("code"), q.Get("state")) |
| 345 | if err != nil { |
| 346 | // Not a dead end: back to the form with the override set, so the |
| 347 | // next attempt takes the magic link instead. |
| 348 | http.Redirect(w, r, "/signin?force=1", http.StatusSeeOther) |
| 349 | return |
| 350 | } |
| 351 | // In a fresh step-up the approval counts only if Keymail reports a |
| 352 | // gesture inside the window. Otherwise no session is minted and the |
| 353 | // person takes the email link instead, the intent cookie untouched. |
| 354 | // The check has to be here rather than left to resume: resume reads |
| 355 | // a zero AuthTime as the session's At, which is this request's own |
| 356 | // clock, so a standing Keymail approval would pass as fresh. |
| 357 | now := time.Now() |
| 358 | if intent, ok := a.readAssertionIntent(r, now); ok && intent.MaxAge != 0 && |
| 359 | (!freshGesture(id.AuthTime, now, time.Duration(intent.MaxAge)*time.Second) || (intent.Started != 0 && id.AuthTime.Unix() < intent.Started)) { |
| 360 | http.Redirect(w, r, "/signin?force=1", http.StatusSeeOther) |
| 361 | return |
| 362 | } |
| 363 | a.completeSignIn(w, r, id) |
| 364 | } |
| 365 | |
| 366 | // freshGesture reports whether Keymail's gesture time proves a gesture |
| 367 | // within maxAge of now. |
| 368 | // |
| 369 | // Zero is Keymail saying no gesture backed the approval. A time after |
| 370 | // now is refused with NO clock-skew allowance, deliberately: the |
| 371 | // assertion format refuses auth_time later than iat, and the issued |
| 372 | // assertion must carry Keymail's real AuthTime unaltered, so a future |
| 373 | // value can never be issued — crediting it here would only mint a |
| 374 | // session that resume then bounces back to sign-in. Keymail's |
| 375 | // auth_time is whole seconds, truncated, and the gesture precedes this |
| 376 | // request by at least the redirect, so synchronised clocks never trip |
| 377 | // this; a Keymail clock running ahead of Home's sends the person to the |
| 378 | // email link, which is the safe direction to fail in. |
| 379 | func freshGesture(at, now time.Time, maxAge time.Duration) bool { |
| 380 | return !at.IsZero() && !at.After(now) && now.Sub(at) <= maxAge |
| 381 | } |
| 382 | |
| 383 | // forcesLogin reports whether a Keymail authorize URL demands a new |
| 384 | // gesture: exactly one prompt parameter, and it is login. |
| 385 | func forcesLogin(authorize string) bool { |
| 386 | u, err := url.Parse(authorize) |
| 387 | if err != nil { |
| 388 | return false |
| 389 | } |
| 390 | prompt := u.Query()["prompt"] |
| 391 | return len(prompt) == 1 && prompt[0] == "login" |
| 392 | } |
| 393 | |
| 394 | // completeSignIn turns a verified identity into a session. |
| 395 | // |
| 396 | // The session's subject is the person ref, never the address. |
| 397 | // sessions.Session.Subject is an opaque string the app chooses, and it |
| 398 | // is stored in the clear — putting an address there would put every |
| 399 | // signed-in person's address at rest in a plaintext column, which is |
| 400 | // precisely what rastrillo/auth does and why this app does not use it. |
| 401 | func (a *App) completeSignIn(w http.ResponseWriter, r *http.Request, id signin.Identity) { |
| 402 | ref, err := a.findOrMintPerson(r.Context(), id.Address) |
| 403 | if err != nil { |
| 404 | a.logger.Error("find or mint person", "err", err) |
| 405 | a.signinFailed(w, r) |
| 406 | return |
| 407 | } |
| 408 | // The door remembers the way in and the address this browser just |
| 409 | // proved (ruling 26), in a sealed cookie on this browser only. Here, |
| 410 | // where the link or the Keymail return is proven, before the passkey |
| 411 | // gate: the gate's own finish keeps the address only for the same |
| 412 | // person (passkeyRemembrance). |
| 413 | a.rememberEmail(w, id) |
| 414 | sess := sessions.Session{ |
| 415 | Subject: ref, |
| 416 | Method: string(id.Method), |
| 417 | AuthTime: id.AuthTime, |
| 418 | At: id.At, |
| 419 | } |
| 420 | |
| 421 | if id.Method == signin.MethodMagicLink { |
| 422 | sess.AuthTime = id.At |
| 423 | } |
| 424 | |
| 425 | // The second factor, on BOTH sign-in paths — and on both because |
| 426 | // there is one place to put it, not because two call sites were |
| 427 | // remembered. handleVerify (the magic link) and handleCallback (the |
| 428 | // keymail return) both funnel through here, so an enrolled person |
| 429 | // cannot obtain a full session from one factor down either door. |
| 430 | // The bug this guards against is real: it is what happens when only |
| 431 | // the magic-link path is gated and a keymail-upgraded account walks |
| 432 | // in through the other one. |
| 433 | // |
| 434 | // The gate either stores the pending factor or mints the email-only |
| 435 | // session under the same writer transaction as its credential check. |
| 436 | target := readReturn(r) |
| 437 | writeReturn(w, "/") |
| 438 | gated, err := a.idGate(w, requestWithReturn(r, target), sess, id.Address) |
| 439 | if err != nil { |
| 440 | a.logger.Error("passkey gate", "person", ref, "err", err) |
| 441 | a.signinFailed(w, r) |
| 442 | return |
| 443 | } |
| 444 | if gated { |
| 445 | return |
| 446 | } |
| 447 | |
| 448 | } |
| 449 | |
| 450 | // signinFailed is the one answer every failed redemption gets. One page, |
| 451 | // one status, no detail: unknown, expired, already used and |
| 452 | // wrong-purpose must be indistinguishable from outside. |
| 453 | func (a *App) signinFailed(w http.ResponseWriter, r *http.Request) { |
| 454 | a.render(w, r, http.StatusBadRequest, "signin_failed.html", map[string]any{"Again": againHref(r)}) |
| 455 | } |
| 456 | |
| 457 | func (a *App) setPending(w http.ResponseWriter, blob string) { |
| 458 | http.SetCookie(w, &http.Cookie{ |
| 459 | Name: pendingCookie, |
| 460 | Value: blob, |
| 461 | Path: "/", |
| 462 | MaxAge: int(pendingTTL.Seconds()), |
| 463 | HttpOnly: true, |
| 464 | Secure: true, |
| 465 | SameSite: http.SameSiteLaxMode, |
| 466 | }) |
| 467 | } |
| 468 | |
| 469 | // takePending reads the pending blob and clears the cookie in the same |
| 470 | // breath. |
| 471 | // |
| 472 | // The clearing is hygiene, NOT single use — see the note on |
| 473 | // pendingCookie. It stops this browser re-submitting; it does not make |
| 474 | // the blob itself spent, because the blob is a self-contained value |
| 475 | // that stays valid until it expires. The authorization code is what is |
| 476 | // single use, at the federation server. An earlier version of this |
| 477 | // comment said "so one blob completes at most one callback", which is |
| 478 | // the claim the pendingCookie note retracts — and it survived here, |
| 479 | // verbatim, through the commit that retracted it. |
| 480 | func (a *App) takePending(w http.ResponseWriter, r *http.Request) string { |
| 481 | http.SetCookie(w, &http.Cookie{ |
| 482 | Name: pendingCookie, |
| 483 | Value: "", |
| 484 | Path: "/", |
| 485 | MaxAge: -1, |
| 486 | HttpOnly: true, |
| 487 | Secure: true, |
| 488 | SameSite: http.SameSiteLaxMode, |
| 489 | }) |
| 490 | c, err := r.Cookie(pendingCookie) |
| 491 | if err != nil { |
| 492 | return "" |
| 493 | } |
| 494 | return c.Value |
| 495 | } |
| 496 | |