Directory exchange speaks connect's NativeExchange answers
Human
Prompt
Implement the Home (id.oficina.works) half of slice A of the approved Oficina ID native spec. Paul approved the spec and building on 2026-11-14.
Read first: native-levelup/BRIEF.md (own worktree from fresh origin/main, repo AGENTS.md, amadan workflow, agent account, never -as-human, pinned-toolchain make gates, no deploys, do not merge). Then in the Home repo: docs/superpowers/specs/2026-11-14-oficina-id-native.md (binding; slice table §14), 2026-11-13-oficina-id-slice-1.md (binding base), Home AGENTS.md.
Scope — slice A, Home side only (another agent does connect/NativeExchange/choose page/IsHomeDerived in parallel on its own branch; a third does the Swift client; coordinate only through the spec's wire formats, and if the spec is ambiguous about a wire detail, pick the reading most consistent with it, write it into a short "wire notes" section of your branch description, and flag it):
- the native grant + device-key proof (§3), persistent replay table, per-grant rate limit, server Date for clock offset;
- email code start/resend/verify with every limit in §4 (per-(address, device key) caps, daily cap, returning-key exemption by end_reason, per-IP-bucket live starts, 503 when full, shared email budget with the web, sealed addresses, no codes/addresses in logs; Keymail addresses get the code in slice A);
- native passkey sign-in (§5): passkey/begin, passkey/finish against RP id.oficina.works, plus the static /.well-known/apple-app-site-association with webcredentials for the five App IDs 9U4A84C4WK.works.oficina.{calls,docs,sheets,calendar,memoria} (no applinks);
- assert with the shared mintPolicy (empty in slice A), native nonce derivation;
- signout, "Sign out all devices" on the web, replacement revoking every grant, first passkey enrolment (native or web) revoking passkey-less grants;
- directory teams listing + zero-teams help;
- every slice-A gate in §13 that belongs to Home, as tests.
Not in scope: A2 (multiple identities UI, Devices page, Keymail sheet), B (native recovery/enrolment), C–E.
Process: small reviewable commits; make ci green; push; describe the branch on the hub with tasks; stop for review. Report: branch, tip, what's done vs deferred, gate output, wire notes, open questions.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
The directory's POST /native/oficina-id now answers exactly as connect.Bridge.NativeExchange does on the connect branch: JSON {state} with ok (name, value, expires_at, target echoed) / invalid 400 / failed 401 / unavailable 503, a browser's request is invalid, choice must be absent or "new", target must be a safe local path. Pins the shared nonce vector (verifier 0x00..0x1f -> challenge Yw3N…EN0 -> nonce cU9jQRCTNby9G46AOrt78A) that the connect branch's handoff test also pins.Agent notes
Read the connect branch (feat/oficina-id-native-connect, 6204570) with git show only. Once both land, nativeNonce can call handoff.NativeNonce and the directory exchange can use handoff.Store.Reserve; until then the vector test is what keeps them one derivation.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+88
−25
internal/home/directory_native.go+48 −21internal/home/directory_native_test.go+20 −4internal/home/native_grant_test.go+20 −0
diff --git a/internal/home/directory_native.go b/internal/home/directory_native.go| index c73a8ce..0070998 100644 |
| --- a/internal/home/directory_native.go |
| +++ b/internal/home/directory_native.go |
| @@ -5,9 +5,12 @@ import ( |
| "crypto/subtle" |
| "encoding/hex" |
| "errors" |
| + "io" |
| + "mime" |
| "net/http" |
| "time" |
| + "amadan.net/oficina/home/handoff" |
| "amadan.net/oficina/home/internal/home/directorydata" |
| "amadan.net/rastrillo/rastrillo/assertion" |
| "amadan.net/rastrillo/rastrillo/sessions" |
| @@ -33,16 +36,36 @@ const ( |
| // nativeTeamApps are the native apps; the list is filtered to them (§9). |
| var nativeTeamApps = map[string]bool{"calls": true, "docs": true, "sheets": true, "calendar": true, "memoria": true} |
| -// directoryNativeExchange is POST /native/oficina-id {assertion, verifier}. |
| -// It verifies the assertion against the pinned keys with the exact issuer |
| -// and the directory as audience, recomputes the native nonce from the |
| -// verifier, reserves the jti, and mints a seven-day directory session whose |
| -// token goes back in the body, never in a cookie. target and choice are |
| -// accepted for the shape instances share and ignored: the directory links |
| -// nobody and allocates nothing, so a person ref is the whole answer. |
| +// directoryNativeExchange is POST /native/oficina-id {assertion, verifier, |
| +// target?, choice?}. It verifies the assertion against the pinned keys with |
| +// the exact issuer and the directory as audience, recomputes the native |
| +// nonce from the verifier, reserves the jti, and mints a seven-day directory |
| +// session whose token goes back in the body, never in a cookie. |
| +// |
| +// The request and every answer are connect.Bridge.NativeExchange's, so one |
| +// client speaks to the directory and to every team instance alike: JSON |
| +// {"state": …}, ok / invalid / failed / unavailable. choice is accepted and |
| +// ignored and target is echoed: the directory links nobody and allocates |
| +// nothing, so a person ref is the whole answer, and the unknown and choose |
| +// states never arise here. |
| func (a *App) directoryNativeExchange(w http.ResponseWriter, r *http.Request) { |
| - body, ok := nativeIntake(w, r) |
| - if !ok { |
| + answer := func(status int, state string) { nativeReply(w, status, map[string]string{"state": state}) } |
| + if _, ok := r.Header["Origin"]; ok { |
| + answer(http.StatusBadRequest, "invalid") |
| + return |
| + } |
| + if _, ok := r.Header["Sec-Fetch-Site"]; ok { |
| + answer(http.StatusBadRequest, "invalid") |
| + return |
| + } |
| + media, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) |
| + if err != nil || media != "application/json" || r.URL.RawQuery != "" { |
| + answer(http.StatusBadRequest, "invalid") |
| + return |
| + } |
| + body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 12*1024)) |
| + if err != nil { |
| + answer(http.StatusBadRequest, "invalid") |
| return |
| } |
| var in struct { |
| @@ -51,30 +74,30 @@ func (a *App) directoryNativeExchange(w http.ResponseWriter, r *http.Request) { |
| Target string `json:"target,omitzero"` |
| Choice string `json:"choice,omitzero"` |
| } |
| - if !decodeNative(body, &in) { |
| - nativeFail(w, http.StatusBadRequest, "request") |
| + if !decodeNative(body, &in) || in.Assertion == "" || (in.Choice != "" && in.Choice != "new") || (in.Target != "" && !handoff.SafePath(in.Target)) { |
| + answer(http.StatusBadRequest, "invalid") |
| return |
| } |
| verifierRaw, ok := decodeB64(in.Verifier, 32) |
| if !ok { |
| - nativeFail(w, http.StatusBadRequest, "request") |
| + answer(http.StatusBadRequest, "invalid") |
| return |
| } |
| v, err := assertion.NewVerifier(a.origins.Credential, a.origins.Directory, a.runtime.keys) |
| if err != nil { |
| - nativeFail(w, http.StatusServiceUnavailable, "unavailable") |
| + answer(http.StatusServiceUnavailable, "unavailable") |
| return |
| } |
| now := time.Now() |
| claims, err := v.Verify(in.Assertion, now) |
| challenge := sha256.Sum256(verifierRaw) |
| if err != nil || subtle.ConstantTimeCompare([]byte(claims.Nonce), []byte(nativeNonce(challenge[:]))) != 1 || !ValidPersonRef(claims.Subject) { |
| - nativeUnauthorized(w, "assertion") |
| + answer(http.StatusUnauthorized, "failed") |
| return |
| } |
| tx, err := a.db.Writer().BeginTx(r.Context(), nil) |
| if err != nil { |
| - nativeFail(w, http.StatusServiceUnavailable, "unavailable") |
| + answer(http.StatusServiceUnavailable, "unavailable") |
| return |
| } |
| defer tx.Rollback() |
| @@ -83,29 +106,33 @@ func (a *App) directoryNativeExchange(w http.ResponseWriter, r *http.Request) { |
| 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 { |
| - nativeFail(w, http.StatusServiceUnavailable, "unavailable") |
| + answer(http.StatusServiceUnavailable, "unavailable") |
| return |
| } |
| if n, err := res.RowsAffected(); err != nil || n != 1 { |
| - nativeUnauthorized(w, "replay") |
| + answer(http.StatusUnauthorized, "failed") |
| return |
| } |
| token, hash, err := sessions.NewToken() |
| if err != nil { |
| - nativeFail(w, http.StatusServiceUnavailable, "unavailable") |
| + answer(http.StatusServiceUnavailable, "unavailable") |
| return |
| } |
| expires := now.Unix() + nativeSessionLife |
| if _, err = tx.Exec("INSERT INTO sessions(token_hash,subject,method,auth_time,created_at,expires_at) VALUES(?,?,?,?,?,?)", |
| hash, claims.Subject, nativeSessionMethod, time.Unix(claims.AuthTime, 0).UTC().Format(time.RFC3339), now.UTC().Format(time.RFC3339), time.Unix(expires, 0).UTC().Format(time.RFC3339)); err != nil { |
| - nativeFail(w, http.StatusServiceUnavailable, "unavailable") |
| + answer(http.StatusServiceUnavailable, "unavailable") |
| return |
| } |
| if tx.Commit() != nil { |
| - nativeFail(w, http.StatusServiceUnavailable, "unavailable") |
| + answer(http.StatusServiceUnavailable, "unavailable") |
| return |
| } |
| - nativeReply(w, http.StatusOK, map[string]any{"name": a.sessions.CookieName(), "value": token, "expires_at": expires}) |
| + reply := map[string]any{"state": "ok", "name": a.sessions.CookieName(), "value": token, "expires_at": expires} |
| + if in.Target != "" { |
| + reply["target"] = in.Target |
| + } |
| + nativeReply(w, http.StatusOK, reply) |
| } |
| type nativeTeamApp struct { |
diff --git a/internal/home/directory_native_test.go b/internal/home/directory_native_test.go| index cac7033..ab0ee93 100644 |
| --- a/internal/home/directory_native_test.go |
| +++ b/internal/home/directory_native_test.go |
| @@ -27,8 +27,12 @@ func directoryCall(t *testing.T, f chooserFixture, method, path, body string, co |
| return w |
| } |
| -func exchangeBody(assertion, verifier string) string { |
| - b, _ := json.Marshal(map[string]string{"assertion": assertion, "verifier": verifier}) |
| +func exchangeBody(assertion, verifier string, extra ...string) string { |
| + m := map[string]string{"assertion": assertion, "verifier": verifier} |
| + for i := 0; i+1 < len(extra); i += 2 { |
| + m[extra[i]] = extra[i+1] |
| + } |
| + b, _ := json.Marshal(m) |
| return string(b) |
| } |
| @@ -53,10 +57,10 @@ func TestNativeDirectoryTeams(t *testing.T) { |
| token, verifier := mint(10) |
| // A wrong verifier, a browser and a non-JSON body are refused. |
| wrongVerifier, _ := verifierFor(11) |
| - if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, wrongVerifier), nil, nil); w.Code != 401 { |
| + if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, wrongVerifier), nil, nil); w.Code != 401 || decodeReply(t, w)["state"] != "failed" { |
| t.Fatal("wrong verifier", w.Code) |
| } |
| - if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, map[string]string{"Origin": directoryOrigin}); w.Code != 403 { |
| + if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, map[string]string{"Origin": directoryOrigin}); w.Code != 400 || decodeReply(t, w)["state"] != "invalid" { |
| t.Fatal("a browser exchanged", w.Code) |
| } |
| w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, nil) |
| @@ -64,10 +68,22 @@ func TestNativeDirectoryTeams(t *testing.T) { |
| t.Fatal("exchange", w.Code, w.Body.String(), w.Header()) |
| } |
| out := decodeReply(t, w) |
| + if out["state"] != "ok" { |
| + t.Fatal("state", out) |
| + } |
| cookie := &http.Cookie{Name: out["name"].(string), Value: out["value"].(string)} |
| if exp := int64(out["expires_at"].(float64)); exp > time.Now().Unix()+7*day || exp < time.Now().Unix()+7*day-60 { |
| t.Fatal("native directory session is not seven days", exp) |
| } |
| + // target is echoed, choice ignored, as connect's exchange does. |
| + t2, v2 := mint(12) |
| + w2 := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(t2, v2, "target", "/teams", "choice", "new"), nil, nil) |
| + if w2.Code != 200 || decodeReply(t, w2)["target"] != "/teams" { |
| + t.Fatal("target", w2.Code, w2.Body.String()) |
| + } |
| + if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(t2, v2, "choice", "old"), nil, nil); w.Code != 400 { |
| + t.Fatal("unknown choice", w.Code) |
| + } |
| // The same assertion twice is a replay. |
| if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, nil); w.Code != 401 { |
| t.Fatal("replayed exchange", w.Code) |
diff --git a/internal/home/native_grant_test.go b/internal/home/native_grant_test.go| index 424d4b7..8d1c998 100644 |
| --- a/internal/home/native_grant_test.go |
| +++ b/internal/home/native_grant_test.go |
| @@ -271,3 +271,23 @@ func TestNativeSignout(t *testing.T) { |
| t.Fatal("a signed-out grant asserted", w.Code) |
| } |
| } |
| + |
| +// The derivation's shared vector (verifier = 0x00..0x1f), the same bytes the |
| +// connect branch's handoff.NativeNonce test pins, so Home's assert, every |
| +// instance's NativeExchange and the Swift client agree. |
| +func TestNativeNonceVector(t *testing.T) { |
| + raw := make([]byte, 32) |
| + for i := range raw { |
| + raw[i] = byte(i) |
| + } |
| + if v := base64.RawURLEncoding.EncodeToString(raw); v != "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8" { |
| + t.Fatal(v) |
| + } |
| + challenge := sha256.Sum256(raw) |
| + if c := base64.RawURLEncoding.EncodeToString(challenge[:]); c != "Yw3NKWbEM2aRElRIu7JbT_QSpJxzLbLIq8G4WBvXEN0" { |
| + t.Fatal("challenge", c) |
| + } |
| + if n := nativeNonce(challenge[:]); n != "cU9jQRCTNby9G46AOrt78A" { |
| + t.Fatal("nonce", n) |
| + } |
| +} |