oficina / home Public

Home: fresh step-up uses Keymail prompt=login where it proves a gesture

A fresh-intent (max_age=300) begin no longer always forces the magic
link. If classification yields a Keymail continuation whose authorize
URL carries prompt=login (the server advertised "reauth": true), Home
starts Keymail and sends no mail. A Keymail continuation without it is
discarded unsent and the magic link is forced, as before; a non-Keymail
address gets mail, as before.

On the Keymail return during a fresh flow, the approval counts only if
AuthTime is non-zero, not after now, and at most MaxAge old. Otherwise
no session is minted and the browser goes to /signin?force=1 with the
intent intact; under a fresh intent that form now posts to
/signin?force=1 so the fallback really emails. No skew allowance: the
assertion refuses auth_time > iat and AuthTime is never altered, so a
future value could never be issued.

Ordinary SSO and Keymail sign-in, subject binding, second-factor gates
and sealed-intent bounds are unchanged. implementation/fresh-assertion.md
records the decision.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 7132f9527683db8ace28a564213b9aa7e257db00 parent 5c9b457
3 files changed, +116 −16
  • implementation/fresh-assertion.md +21 −0
  • internal/home/signin.go +94 −15
  • internal/home/templates/signin.html +1 −1

CI passed — run details

diff --git a/implementation/fresh-assertion.md b/implementation/fresh-assertion.md
index ae38981..1365de1 100644
--- a/implementation/fresh-assertion.md
+++ b/implementation/fresh-assertion.md
@@ -18,3 +18,24 @@ Acceptance: normal exchange retains old AuthTime; max_age with stale session pro
Implementation decisions after review: resolve the standing session explicitly with a.sessions.From at authorize because this router has no sessions.Middleware. A completed Keymail first factor plus recovery-code redemption qualifies as fresh within five minutes: recovery is a current credential and requires the gated first factor, while a half-session alone never qualifies. This is covered separately from stale-session forced mail. MaxAge=0 never forces mail. During rollback an old binary rejects a new nonzero-age sealed intent as an unknown field; the user must restart that in-flight exchange. No persistent data format changes.
+
+
+## Keymail step-up — 2026-10-24
+
+Supersedes the forced-mail paragraph above for Keymail addresses whose server can demand a gesture; for every other address that paragraph still holds.
+
+keymaildev/signin v0.1.1 sends prompt=login when the federation server's /.well-known/keymail advertises "reauth": true (keymail serve answers prompt=login with 403 otherwise), and returns Identity.AuthTime, Keymail's report of the authenticator gesture, zero when none was reported. A forced login with a reported gesture time is a fresh gesture, which is what the forced mail existed to obtain, so a fresh intent no longer has to email a Keymail address on such a server.
+
+Begin. A fresh-intent begin classifies as usual. If the result is a Keymail continuation whose authorize URL carries exactly prompt=login, Home starts Keymail and sends no mail. If it is a Keymail continuation without prompt=login (a server that does not advertise reauth), Home discards it unsent and calls Begin again with the magic link forced: today's behaviour. A non-Keymail address gets the magic link, as before. The check is on the URL actually handed to the browser rather than on a separate classification, so it tests the fact that matters instead of predicting it, and classification still runs after the per-IP budget. The cost is a second address-budget token on the no-reauth path only.
+
+Callback. During a fresh flow (a valid sealed intent with MaxAge set) the Keymail return is credited only if AuthTime is non-zero, no later than now, and now - AuthTime <= MaxAge (300 s). Otherwise no session is minted and the browser goes to /signin?force=1, the existing Keymail fallback, with the intent and the /authorize/resume navigation cookie untouched. Under a fresh intent the form rendered at /signin?force=1 posts to /signin?force=1, because the browser submits the form's action and not the URL that rendered it, and without that the fallback would classify again and loop back to Keymail. The flag is convenience, not security: dropping it buys another Keymail round trip held to the same check. The ordinary form is unchanged.
+
+This check cannot be left to resume. Resume reads a zero AuthTime as the session's At, which is the callback's own clock, so a standing Keymail approval with no reported gesture would pass resume's freshness test. Resume's existing checks (sessions.Fresh, verified <= now, subject binding) still run afterwards, unchanged.
+
+No clock-skew allowance. A future AuthTime is refused outright. rastrillo's assertion validation refuses auth_time > iat, and the issued assertion must carry Keymail's AuthTime unaltered, so no future value can ever be issued. Allowing skew at the callback would only mint a session that resume bounces back to sign-in. Keymail's auth_time is whole seconds, truncated, and the gesture precedes the callback by at least one redirect, so synchronised clocks never trip it. A Keymail clock running ahead of Home's degrades to the email link, which is the safe direction.
+
+Unchanged: ordinary SSO and ordinary Keymail sign-in, including whatever prompt the library chooses there, with no gesture check on the return; subject binding (a different person's fresh Keymail gesture still gets 400 at resume); the passkey and recovery gates, which run in completeSignIn on both doors; and every sealed-intent bound. AuthTime is never advanced: the assertion carries Keymail's gesture time, tested to the second.
+
+Acceptance (keymail_fresh_test.go, against the existing keymail stub with "reauth" added): fresh intent plus a reauth server gives an authorize URL with exactly prompt=login and no mail; a gesture 90 s old yields an assertion whose auth_time equals Keymail's; zero, 301 s, one hour, +5 s and +1 h AuthTime give no session and fall back to email, and that email link completes the step-up through the form a browser would submit; a server without reauth gets forced mail; a non-Keymail address gets mail; ordinary Keymail with zero AuthTime still signs in; subject switching through Keymail is refused. Mutation: replacing the window with true fails all five fallback cases, as do removing the future bound or the age bound alone; dropping the prompt=login requirement fails the no-reauth tests; dropping the form's override fails the fallback test. Removing only the zero check survives, because a zero time is also more than 300 s old; the explicit check stays as documentation of the case Keymail defines.
+
+Residual, not changed here: outside a fresh flow, a Keymail session with zero AuthTime still has its At used as its verified time, so if one is minted by an ordinary sign-in and a fresh intent arrives within five minutes, resume credits it without mail ("fresh session succeeds without unnecessary mail"). That predates this change and is the standing-session gap in a different door.
diff --git a/internal/home/signin.go b/internal/home/signin.go
index adee4bc..befa0c6 100644
--- a/internal/home/signin.go
+++ b/internal/home/signin.go
@@ -160,9 +160,24 @@ func clientIP(r *http.Request) string {
}
// handleSigninForm renders the address form.
+//
+// In a fresh step-up the email override has to survive into the POST,
+// because the browser submits the form's action and not the URL that
+// rendered it: without this, a Keymail return that proved no fresh
+// gesture would fall back to a form that classifies the address again
+// and sends the person straight back to Keymail. Only the fresh flow
+// carries it, so the ordinary form and its fallback are unchanged.
+// Dropping the flag from the action is harmless: it buys another
+// Keymail round trip, held to the same gesture check.
func (a *App) handleSigninForm(w http.ResponseWriter, r *http.Request) {
rememberReturn(w, r)
- a.render(w, r, http.StatusOK, "signin.html", map[string]any{})
+ data := map[string]any{}
+ if r.URL.Query().Get("force") == "1" {
+ if intent, ok := a.readAssertionIntent(r, time.Now()); ok && intent.MaxAge != 0 {
+ data["Action"] = "/signin?force=1"
+ }
+ }
+ a.render(w, r, http.StatusOK, "signin.html", data)
}
// handleSigninSent is the check-your-mail page. It is reached by
@@ -201,36 +216,60 @@ func (a *App) handleSigninBegin(w http.ResponseWriter, r *http.Request) {
// The force override carries a failed keymail round trip back to the
// magic link, so a failed approval is not a dead end.
var force signin.Method
- intent, freshIntent := a.readAssertionIntent(r, time.Now())
- if r.URL.Query().Get("force") == "1" || (freshIntent && intent.MaxAge == 300) {
+ if r.URL.Query().Get("force") == "1" {
force = signin.MethodMagicLink
}
-
next, err := a.flow.Begin(r.Context(), address, clientIP(r), force)
+ if a.beginFailed(w, r, err) {
+ return
+ }
+
+ // A fresh step-up (max_age, implementation/fresh-assertion.md) may
+ // take the Keymail door only when Keymail will demand a new gesture.
+ // A standing Keymail inbox session approves without one, so a plain
+ // authorize URL proves nothing about freshness. The test is on the
+ // URL actually handed to the browser: the library adds prompt=login
+ // exactly when the server advertised "reauth": true (keymail serve
+ // answers it with 403 otherwise), so its presence is the fact that
+ // matters, not a prediction of it. Without it, nothing has been sent
+ // yet and the email link is what runs, as it always did here. That
+ // second Begin spends a second rate-limit token for the address; it
+ // happens only for a Keymail server that cannot step up.
+ intent, freshIntent := a.readAssertionIntent(r, time.Now())
+ if freshIntent && intent.MaxAge != 0 && next.Kind == signin.NextKeymail && !forcesLogin(next.Redirect) {
+ next, err = a.flow.Begin(r.Context(), address, clientIP(r), signin.MethodMagicLink)
+ if a.beginFailed(w, r, err) {
+ return
+ }
+ }
+
+ if next.Kind == signin.NextKeymail {
+ a.setPending(w, next.Pending)
+ a.renderContinuation(w, r, next.Redirect, "Continue to Keymail")
+ return
+ }
+ http.Redirect(w, r, "/signin/sent", http.StatusSeeOther)
+}
+
+// beginFailed answers a failed Begin and reports whether it did.
+func (a *App) beginFailed(w http.ResponseWriter, r *http.Request, err error) bool {
switch {
+ case err == nil:
+ return false
case errors.Is(err, signin.ErrBadAddress):
a.render(w, r, http.StatusBadRequest, "signin.html",
map[string]any{"Error": "That does not look like an email address."})
- return
case errors.Is(err, signin.ErrRateLimited):
// 429 without saying which budget: which one was hit is itself
// information about whether that address has been tried.
a.render(w, r, http.StatusTooManyRequests, "signin.html",
map[string]any{"Error": "Too many attempts. Try again shortly."})
- return
- case err != nil:
+ default:
a.logger.Error("signin begin", "err", err)
a.render(w, r, http.StatusInternalServerError, "signin.html",
map[string]any{"Error": "Something went wrong. Try again."})
- return
- }
-
- if next.Kind == signin.NextKeymail {
- a.setPending(w, next.Pending)
- a.renderContinuation(w, r, next.Redirect, "Continue to Keymail")
- return
}
- http.Redirect(w, r, "/signin/sent", http.StatusSeeOther)
+ return true
}
// handleVerifyForm renders the confirmation page for a magic link. It
@@ -303,9 +342,49 @@ func (a *App) handleCallback(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/signin?force=1", http.StatusSeeOther)
return
}
+ // In a fresh step-up the approval counts only if Keymail reports a
+ // gesture inside the window. Otherwise no session is minted and the
+ // person takes the email link instead, the intent cookie untouched.
+ // The check has to be here rather than left to resume: resume reads
+ // a zero AuthTime as the session's At, which is this request's own
+ // clock, so a standing Keymail approval would pass as fresh.
+ now := time.Now()
+ if intent, ok := a.readAssertionIntent(r, now); ok && intent.MaxAge != 0 &&
+ !freshGesture(id.AuthTime, now, time.Duration(intent.MaxAge)*time.Second) {
+ http.Redirect(w, r, "/signin?force=1", http.StatusSeeOther)
+ return
+ }
a.completeSignIn(w, r, id)
}
+// freshGesture reports whether Keymail's gesture time proves a gesture
+// within maxAge of now.
+//
+// Zero is Keymail saying no gesture backed the approval. A time after
+// now is refused with NO clock-skew allowance, deliberately: the
+// assertion format refuses auth_time later than iat, and the issued
+// assertion must carry Keymail's real AuthTime unaltered, so a future
+// value can never be issued — crediting it here would only mint a
+// session that resume then bounces back to sign-in. Keymail's
+// auth_time is whole seconds, truncated, and the gesture precedes this
+// request by at least the redirect, so synchronised clocks never trip
+// this; a Keymail clock running ahead of Home's sends the person to the
+// email link, which is the safe direction to fail in.
+func freshGesture(at, now time.Time, maxAge time.Duration) bool {
+ return !at.IsZero() && !at.After(now) && now.Sub(at) <= maxAge
+}
+
+// forcesLogin reports whether a Keymail authorize URL demands a new
+// gesture: exactly one prompt parameter, and it is login.
+func forcesLogin(authorize string) bool {
+ u, err := url.Parse(authorize)
+ if err != nil {
+ return false
+ }
+ prompt := u.Query()["prompt"]
+ return len(prompt) == 1 && prompt[0] == "login"
+}
+
// completeSignIn turns a verified identity into a session.
//
// The session's subject is the person ref, never the address.
diff --git a/internal/home/templates/signin.html b/internal/home/templates/signin.html
index b871679..2c2f816 100644
--- a/internal/home/templates/signin.html
+++ b/internal/home/templates/signin.html
@@ -2,7 +2,7 @@
{{define "content"}}{{with signinDoor}}
<h1>{{.Heading}}</h1>
{{if $.Data.Error}}<p role="alert" rst-callout rst-tone="negative"><span rst-callout-body>{{$.Data.Error}}</span></p>{{end}}
-<form method="post" action="/signin" rst-form estilo-signin-form>
+<form method="post" action="{{with $.Data.Action}}{{.}}{{else}}/signin{{end}}" rst-form estilo-signin-form>
<div rst-field>
<label rst-field-label for="address">{{.AddressLabel}}</label>
<input id="address" name="address" type="email" autocomplete="email" required autofocus rst-input aria-describedby="address-help">