Use handoff's native derivation and Reserve; trim slow tests
Human
Prompt
Coordinator, 2026-11-14: Home main now has the connect/handoff work (e03a01a). Merge origin/main, then replace your copied nonce derivation and jti reservation with handoff.NativeChallenge/NativeNonce and handoff.Store.Verify/Reserve (repin handoff in the root module to e03a01a if needed), keep the shared vector test, and continue with the queued fixes and the CI race-step time.
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
Repins handoff to e03a01a (main, with the connect work). assert takes its nonce from handoff.NativeNonce; the directory exchange uses handoff.NativeChallenge/NativeNonce, Store.Verify and Store.Reserve (jti in handoff's own committed transaction, inbox-waiting jtis refused), then mints. The sign-out-all receiver verifies with Store.Verify but keeps its jti insert in the same transaction as the session delete, on purpose: the receipt is one-shot. The shared vector test now checks both Home's call and handoff's. The returning-key end_reason matrix moves to a unit test; the end-to-end daily-cap test keeps one case of each kind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+59
−66
go.mod+1 −1go.sum+2 −0internal/home/app.go+1 −0internal/home/directory.go+1 −1internal/home/directory_native.go+16 −38internal/home/native.go+0 −14internal/home/native_assert.go+5 −2internal/home/native_code_test.go+3 −2internal/home/native_grant_test.go+17 −2internal/home/native_review_test.go+8 −0internal/home/signout_all.go+5 −6
CI failed — run details
diff --git a/go.mod b/go.mod| index cc02403..a680404 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -7,7 +7,7 @@ toolchain go1.26.5 |
| require ( |
| amadan.net/oficina/estilo.git v0.0.0-20261025200559-260f98c42e2d |
| amadan.net/oficina/home/dashboard v0.0.0-20261024234842-3774eb68f816 |
| - amadan.net/oficina/home/handoff v0.0.0-20261027235000-69ea999ccbf7 |
| + amadan.net/oficina/home/handoff v0.0.0-20261114110939-e03a01a1881c |
| amadan.net/oficina/home/roster v0.0.0-20261028165624-d942439ac4e4 |
| amadan.net/rastrillo/rastrillo v0.26.1-0.20260908075707-29e4491631bd |
| filippo.io/age v1.3.2 |
diff --git a/go.sum b/go.sum| index e106f06..e9041b2 100644 |
| --- a/go.sum |
| +++ b/go.sum |
| @@ -6,6 +6,8 @@ amadan.net/oficina/home/dashboard v0.0.0-20261024234842-3774eb68f816 h1:AFDuOio0 |
| amadan.net/oficina/home/dashboard v0.0.0-20261024234842-3774eb68f816/go.mod h1:+7xsX+QyVFrKy5gDAGh+FZ5+75z4CibWTL0wl9DWAw0= |
| amadan.net/oficina/home/handoff v0.0.0-20261027235000-69ea999ccbf7 h1:F0wJpR9hBz5CBfCdw80TuBu0Ufioh+tmpODB9Y7ifH0= |
| amadan.net/oficina/home/handoff v0.0.0-20261027235000-69ea999ccbf7/go.mod h1:YISpzFJ+iMgrM0rUL032OSzsAQbP4/nozvXLCCOcn1k= |
| +amadan.net/oficina/home/handoff v0.0.0-20261114110939-e03a01a1881c h1:KxjjyIlEu3hY1G6l26IrI2PRcogOscpTeydRgNzimbA= |
| +amadan.net/oficina/home/handoff v0.0.0-20261114110939-e03a01a1881c/go.mod h1:YISpzFJ+iMgrM0rUL032OSzsAQbP4/nozvXLCCOcn1k= |
| amadan.net/oficina/home/roster v0.0.0-20261028165624-d942439ac4e4 h1:jg3sS7hy8Qaewrv/7CZ4kjIYE62FaIG4KMlfNs918dY= |
| amadan.net/oficina/home/roster v0.0.0-20261028165624-d942439ac4e4/go.mod h1:gqshuXyDtFZUCYbhuZyV3Epc51pUcsLbpqAqiA5UiXg= |
| amadan.net/rastrillo/rastrillo v0.26.1-0.20260908075707-29e4491631bd h1:jlFSt95M1ontOg+VePU+kIJpChtWqDQAbnmlxqCnbgc= |
diff --git a/internal/home/app.go b/internal/home/app.go| index 6695db6..609c733 100644 |
| --- a/internal/home/app.go |
| +++ b/internal/home/app.go |
| @@ -43,6 +43,7 @@ type App struct { |
| runtime *Runtime |
| directory *directoryFeatures |
| consumer *handoff.Consumer |
| + handoffStore *handoff.Store // the directory's: native exchange and sign-out-all |
| fresh *handoff.Consumer |
| op *operatorEndpoint |
| origins Origins |
diff --git a/internal/home/directory.go b/internal/home/directory.go| index 13a4553..1c694d7 100644 |
| --- a/internal/home/directory.go |
| +++ b/internal/home/directory.go |
| @@ -44,7 +44,7 @@ func newDirectory(cfg Runtime, d *db.DB, logger *slog.Logger) (*App, error) { |
| if err = store.Sweep(context.Background(), time.Now()); err != nil { |
| return nil, err |
| } |
| - a := &App{runtime: &cfg, origins: cfg.origins, credentialHost: strings.TrimPrefix(cfg.origins.Credential, "https://"), directoryHost: strings.TrimPrefix(cfg.origins.Directory, "https://"), secrets: cfg.secrets, db: d, logger: logger, renderer: renderer, sessions: sess} |
| + a := &App{handoffStore: store, runtime: &cfg, origins: cfg.origins, credentialHost: strings.TrimPrefix(cfg.origins.Credential, "https://"), directoryHost: strings.TrimPrefix(cfg.origins.Directory, "https://"), secrets: cfg.secrets, db: d, logger: logger, renderer: renderer, sessions: sess} |
| // One completion for both consumers: the directory's one session boundary. |
| complete := func(w http.ResponseWriter, r *http.Request, c assertion.Claims) error { |
| return a.sessions.SignIn(w, r, sessions.Session{Subject: c.Subject, Method: "assertion", AuthTime: time.Unix(c.AuthTime, 0)}) |
diff --git a/internal/home/directory_native.go b/internal/home/directory_native.go| index 7bfeeba..c0409da 100644 |
| --- a/internal/home/directory_native.go |
| +++ b/internal/home/directory_native.go |
| @@ -1,10 +1,8 @@ |
| package home |
| import ( |
| - "crypto/sha256" |
| "crypto/subtle" |
| "database/sql" |
| - "encoding/hex" |
| "errors" |
| "io" |
| "mime" |
| @@ -13,7 +11,6 @@ import ( |
| "amadan.net/oficina/home/handoff" |
| "amadan.net/oficina/home/internal/home/directorydata" |
| - "amadan.net/rastrillo/rastrillo/assertion" |
| "amadan.net/rastrillo/rastrillo/sessions" |
| ) |
| @@ -79,54 +76,35 @@ func (a *App) directoryNativeExchange(w http.ResponseWriter, r *http.Request) { |
| answer(http.StatusBadRequest, "invalid") |
| return |
| } |
| - if _, ok := decodeB64(in.Verifier, 32); !ok { |
| + challenge, ok := handoff.NativeChallenge(in.Verifier) |
| + if !ok { |
| answer(http.StatusBadRequest, "invalid") |
| return |
| } |
| - v, err := assertion.NewVerifier(a.origins.Credential, a.origins.Directory, a.runtime.keys) |
| - if err != nil { |
| - answer(http.StatusServiceUnavailable, "unavailable") |
| - return |
| - } |
| + nonce, _ := handoff.NativeNonce(challenge) |
| now := time.Now() |
| - claims, err := v.Verify(in.Assertion, now) |
| - // The challenge is SHA-256 of the verifier as sent, the base64url |
| - // string (rastrillo's PKCE convention, and the client's). |
| - challenge := sha256.Sum256([]byte(in.Verifier)) |
| - if err != nil || subtle.ConstantTimeCompare([]byte(claims.Nonce), []byte(nativeNonce(challenge[:]))) != 1 || !ValidPersonRef(claims.Subject) { |
| + claims, err := a.handoffStore.Verify(in.Assertion, now) |
| + if err != nil || subtle.ConstantTimeCompare([]byte(claims.Nonce), []byte(nonce)) != 1 || !ValidPersonRef(claims.Subject) { |
| answer(http.StatusUnauthorized, "failed") |
| return |
| } |
| - tx, err := a.db.Writer().BeginTx(r.Context(), nil) |
| - if err != nil { |
| - answer(http.StatusServiceUnavailable, "unavailable") |
| - return |
| - } |
| - defer tx.Rollback() |
| - // The same replay ledger, and the same key for it, as the browser |
| - // callback (handoff.Store): one jti is redeemable once, by either door. |
| - // Implemented as handoff.Store.Reserve does on the connect branch |
| - // (unlanded): a jti whose browser receipt is still waiting in the inbox |
| - // is refused too. Dedupe onto Reserve once that lands. |
| - jti := sha256.Sum256([]byte(claims.Issuer + "\x00" + claims.ID)) |
| - var waiting int |
| - if err = tx.QueryRow("SELECT count(*) FROM oficina_handoff_inbox WHERE jti_hash=?", hex.EncodeToString(jti[:])).Scan(&waiting); err != nil { |
| - answer(http.StatusServiceUnavailable, "unavailable") |
| - return |
| - } |
| - if waiting != 0 { |
| - answer(http.StatusUnauthorized, "failed") |
| + // The jti is reserved in handoff's own committed transaction, as |
| + // connect's NativeExchange does; a reserved jti that then mints nothing |
| + // costs the app one fresh assertion (spec §7, accepted non-atomicity). |
| + if _, err = a.handoffStore.Reserve(r.Context(), in.Assertion, now); err != nil { |
| + if errors.Is(err, handoff.ErrRefused) { |
| + answer(http.StatusUnauthorized, "failed") |
| + } else { |
| + answer(http.StatusServiceUnavailable, "unavailable") |
| + } |
| return |
| } |
| - res, err := tx.Exec("INSERT INTO oficina_handoff_replay(jti_hash,expires_at) VALUES(?,?) ON CONFLICT DO NOTHING", hex.EncodeToString(jti[:]), claims.ExpiresAt) |
| + tx, err := a.db.Writer().BeginTx(r.Context(), nil) |
| if err != nil { |
| answer(http.StatusServiceUnavailable, "unavailable") |
| return |
| } |
| - if n, err := res.RowsAffected(); err != nil || n != 1 { |
| - answer(http.StatusUnauthorized, "failed") |
| - return |
| - } |
| + defer tx.Rollback() |
| token, hash, err := sessions.NewToken() |
| if err != nil { |
| answer(http.StatusServiceUnavailable, "unavailable") |
diff --git a/internal/home/native.go b/internal/home/native.go| index 784a417..68abb17 100644 |
| --- a/internal/home/native.go |
| +++ b/internal/home/native.go |
| @@ -33,8 +33,6 @@ const nativePrefix = "/native/v1" |
| const ( |
| // nativeProofContext domain-separates the device-key proof (§3). |
| nativeProofContext = "oficina-id/native-proof/v1" |
| - // nativeNonceContext derives a native assertion's nonce (§7). |
| - nativeNonceContext = "oficina-id/native/v1" |
| nativeProofSkew = 30 // |iat − now| allowed, seconds |
| nativeReplayHold = 61 // seconds a jti stays in the replay table: see below |
| @@ -283,18 +281,6 @@ func nativeToken() (string, error) { |
| return nb64.EncodeToString(b), nil |
| } |
| -// nativeNonce is the only nonce a native assertion carries: |
| -// H("oficina-id/native/v1\0" ‖ challenge), cut to the 16 bytes the |
| -// assertion format requires. Home derives it; the caller never chooses it, |
| -// so a grant holder cannot obtain an assertion bearing a browser's nonce. |
| -func nativeNonce(challenge []byte) string { |
| - h := sha256.New() |
| - h.Write([]byte(nativeNonceContext)) |
| - h.Write([]byte{0}) |
| - h.Write(challenge) |
| - return nb64.EncodeToString(h.Sum(nil)[:16]) |
| -} |
| - |
| // nativeGrant is a live grant as loaded for one authenticated call. |
| type nativeGrant struct { |
| hash, person, identityID, deviceKey, platform, method string |
diff --git a/internal/home/native_assert.go b/internal/home/native_assert.go| index 61e36ca..5521024 100644 |
| --- a/internal/home/native_assert.go |
| +++ b/internal/home/native_assert.go |
| @@ -5,6 +5,7 @@ import ( |
| "errors" |
| "net/http" |
| + "amadan.net/oficina/home/handoff" |
| "amadan.net/rastrillo/rastrillo/assertion" |
| ) |
| @@ -30,7 +31,9 @@ func (a *App) nativeAssert(tx *sql.Tx, g nativeGrant, body []byte, now int64) (i |
| if !decodeNative(body, &in) { |
| return http.StatusBadRequest, map[string]string{"error": "request"} |
| } |
| - challenge, ok := decodeB64(in.Challenge, 32) |
| + // The nonce is handoff's native derivation, the one every |
| + // NativeExchange recomputes; the caller supplies only the challenge. |
| + nonce, ok := handoff.NativeNonce(in.Challenge) |
| if !ok { |
| return http.StatusBadRequest, map[string]string{"error": "request"} |
| } |
| @@ -58,7 +61,7 @@ func (a *App) nativeAssert(tx *sql.Tx, g nativeGrant, body []byte, now int64) (i |
| Subject: g.person, |
| KeyID: a.runtime.keyID, |
| ID: id, |
| - Nonce: nativeNonce(challenge), |
| + Nonce: nonce, |
| IssuedAt: now, |
| ExpiresAt: now + 120, |
| AuthTime: g.authTime, |
diff --git a/internal/home/native_code_test.go b/internal/home/native_code_test.go| index 140d848..6b6ad76 100644 |
| --- a/internal/home/native_code_test.go |
| +++ b/internal/home/native_code_test.go |
| @@ -220,8 +220,9 @@ func TestNativeCodeDailyCapAndReturningKeys(t *testing.T) { |
| reason string |
| returning bool |
| }{ |
| - {"", true}, {"expiry", true}, {"lru", true}, {"enrolment", true}, |
| - {"all_devices", false}, {"revoke", false}, {"replacement", false}, |
| + // End to end for one of each kind; TestNativeReturningKeyRule and |
| + // the end_reason CHECK cover the rest without a sign-in apiece. |
| + {"", true}, {"expiry", true}, {"all_devices", false}, |
| } { |
| t.Run("ended="+tc.reason, func(t *testing.T) { |
| app := newNativeApp(t) |
diff --git a/internal/home/native_grant_test.go b/internal/home/native_grant_test.go| index 44dcf3c..c14a53a 100644 |
| --- a/internal/home/native_grant_test.go |
| +++ b/internal/home/native_grant_test.go |
| @@ -9,6 +9,7 @@ import ( |
| "testing" |
| "time" |
| + "amadan.net/oficina/home/handoff" |
| "amadan.net/oficina/home/internal/home/sealtest" |
| ) |
| @@ -132,7 +133,7 @@ func TestNativeAssert(t *testing.T) { |
| token := decodeReply(t, w)["assertion"].(string) |
| c := verifyAssertion(t, app, token, teamDocs) |
| session := signInFully(t, app, sealtest.CanaryAddress) |
| - if c.Subject != subjectOf(t, app, session) || c.AuthTime != authTime || c.ExpiresAt-c.IssuedAt != 120 || c.Nonce != nativeNonce(challenge) { |
| + if c.Subject != subjectOf(t, app, session) || c.AuthTime != authTime || c.ExpiresAt-c.IssuedAt != 120 || c.Nonce != mustNativeNonce(t, challenge) { |
| t.Fatalf("claims %+v", c) |
| } |
| // The nonce is derived, never the caller's: no challenge yields a nonce |
| @@ -289,9 +290,14 @@ func TestNativeNonceVector(t *testing.T) { |
| if c := base64.RawURLEncoding.EncodeToString(challenge[:]); c != "6oZqdX5MOLq_qBJ8vppAnT4fk6AP8UiP9zX8-Rev_9A" { |
| t.Fatal("challenge", c) |
| } |
| - if n := nativeNonce(challenge[:]); n != "ZVVVj2zbBfhkAIwAOKNylg" { |
| + if n := mustNativeNonce(t, challenge[:]); n != "ZVVVj2zbBfhkAIwAOKNylg" { |
| t.Fatal("nonce", n) |
| } |
| + // And handoff's, which assert and every exchange use, agrees. |
| + c, ok := handoff.NativeChallenge(verifier) |
| + if n, _ := handoff.NativeNonce(c); !ok || n != "ZVVVj2zbBfhkAIwAOKNylg" { |
| + t.Fatal("handoff derivation", c, n) |
| + } |
| } |
| // The two 401 markers: the client wipes the identity only on grant_ended. |
| @@ -331,3 +337,12 @@ func TestNativeUnauthorizedMarkers(t *testing.T) { |
| } |
| } |
| } |
| + |
| +func mustNativeNonce(t *testing.T, challenge []byte) string { |
| + t.Helper() |
| + n, ok := handoff.NativeNonce(base64.RawURLEncoding.EncodeToString(challenge)) |
| + if !ok { |
| + t.Fatal("challenge") |
| + } |
| + return n |
| +} |
diff --git a/internal/home/native_review_test.go b/internal/home/native_review_test.go| index 977cb98..7e70228 100644 |
| --- a/internal/home/native_review_test.go |
| +++ b/internal/home/native_review_test.go |
| @@ -108,6 +108,14 @@ func TestNativeReturningKeyRule(t *testing.T) { |
| insert("e", 100, 300, "replacement") |
| insert("f", 200, 500, "lru") |
| check(true) |
| + // Each end_reason on its own. |
| + for reason, want := range map[string]bool{"expiry": true, "lru": true, "enrolment": true, "signout": false, "all_devices": false, "revoke": false, "replacement": false} { |
| + if _, err := app.db.Writer().Exec("DELETE FROM home_id_grants"); err != nil { |
| + t.Fatal(err) |
| + } |
| + insert("g", 100, 300, reason) |
| + check(want) |
| + } |
| } |
| // Review finding 3: expired sealed addresses are swept at boot and by |
diff --git a/internal/home/signout_all.go b/internal/home/signout_all.go| index 42c5527..54c649e 100644 |
| --- a/internal/home/signout_all.go |
| +++ b/internal/home/signout_all.go |
| @@ -124,12 +124,7 @@ func (a *App) handleSignedOutAll(w http.ResponseWriter, r *http.Request) { |
| a.failDirectoryHandoff(w, r, http.StatusBadRequest) |
| return |
| } |
| - v, err := assertion.NewVerifier(a.origins.Credential, a.origins.Directory, a.runtime.keys) |
| - if err != nil { |
| - a.failDirectoryHandoff(w, r, http.StatusServiceUnavailable) |
| - return |
| - } |
| - claims, err := v.Verify(r.PostForm.Get("assertion"), time.Now()) |
| + claims, err := a.handoffStore.Verify(r.PostForm.Get("assertion"), time.Now()) |
| if err != nil || subtle.ConstantTimeCompare([]byte(claims.Nonce), []byte(signoutAllNonce(claims.ID))) != 1 || !ValidPersonRef(claims.Subject) { |
| a.failDirectoryHandoff(w, r, http.StatusForbidden) |
| return |
| @@ -140,6 +135,10 @@ func (a *App) handleSignedOutAll(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| defer tx.Rollback() |
| + // Reserved here rather than through handoff.Store.Reserve, on purpose: |
| + // the receipt is one-shot, so its jti and the sessions it ends commit |
| + // together. The key is handoff's jtiHash, SHA-256(iss ‖ 0 ‖ jti), so the |
| + // one ledger still refuses it at every other door. |
| jti := sha256.Sum256([]byte(claims.Issuer + "\x00" + claims.ID)) |
| res, err := tx.Exec("INSERT INTO oficina_handoff_replay(jti_hash,expires_at) VALUES(?,?) ON CONFLICT DO NOTHING", hex.EncodeToString(jti[:]), claims.ExpiresAt) |
| if err != nil { |