Native Oficina ID sign-in: mount connect's NativeExchange at POST /native/oficina-id
Human
Prompt
Wire the connect half of slice A into Docs: the NativeExchange mount (spec 2026-11-14 §7).
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
Docs mints the native app's session through connect.Config.MintNativeSession (the same session store and cookie name, no cookie set; method home-assertion-native) and mounts Bridge.NativeExchange, which connect's middleware lets past CSRF because it refuses anything a browser sends. Router tests: a linked account's native session opens the JSON list; it ends seven days after minting; a browser POST is refused; in team mode a member gets 409 choose, then choice new signs in; non-members get the identical 403; an undecidable roster is 503.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+198
−0
internal/docs/home_connect.go+9 −0internal/docs/home_native_test.go+189 −0
diff --git a/internal/docs/home_connect.go b/internal/docs/home_connect.go| index 10925da..f8ee48a 100644 |
| --- a/internal/docs/home_connect.go |
| +++ b/internal/docs/home_connect.go |
| @@ -99,6 +99,12 @@ func newHome(settings *homeSettings, origin string, db *sql.DB, store *Store, te |
| MintHomeSession: func(w http.ResponseWriter, r *http.Request, s connect.Session) error { |
| return sess.SignIn(w, r, sessions.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime}) |
| }, |
| + // The native app's session (connect.NativeExchange): the same |
| + // store and cookie name, minted without a cookie and handed back |
| + // in the answer. Its own DB handle, never connect's transaction. |
| + MintNativeSession: func(_ context.Context, s connect.Session) (string, error) { |
| + return sess.Mint(sessions.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime}) |
| + }, |
| LookupPerson: linkedAddress, |
| LookupLocalSession: localPerson(store), |
| Render: h.render, |
| @@ -285,6 +291,9 @@ func (h *homeRoutes) mountPublic(r chi.Router) { |
| // inside the app-wide CSRF check like any form. |
| r.Get(connect.ChoosePath, h.bridge.ChoosePage) |
| r.Post(connect.ChoosePath, h.bridge.ChoosePost) |
| + // The native app's Oficina ID exchange. connect's Middleware lets this |
| + // one path past the CSRF check: it refuses anything a browser sends. |
| + r.Post(connect.NativeExchangePath, h.bridge.NativeExchange) |
| } |
| // mountAccount is connecting an Oficina account: connect's link flow, |
diff --git a/internal/docs/home_native_test.go b/internal/docs/home_native_test.go| new file mode 100644 |
| index 0000000..2301de6 |
| --- /dev/null |
| +++ b/internal/docs/home_native_test.go |
| @@ -0,0 +1,189 @@ |
| +package docs |
| + |
| +import ( |
| + "bytes" |
| + "crypto/rand" |
| + "encoding/base64" |
| + "encoding/json" |
| + "net/http" |
| + "net/http/httptest" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect" |
| + "amadan.net/oficina/home/handoff" |
| + "amadan.net/rastrillo/rastrillo/sessions" |
| +) |
| + |
| +// The native app's Oficina ID sign-in (Home spec |
| +// 2026-11-14-oficina-id-native.md §7): POST /native/oficina-id trades an |
| +// assertion Home made for the app's verifier for a Docs session, with no |
| +// browser and no cookie. |
| + |
| +type nativeAnswer struct { |
| + State string `json:"state"` |
| + Name string `json:"name"` |
| + Value string `json:"value"` |
| + ExpiresAt int64 `json:"expires_at"` |
| +} |
| + |
| +// nativeExchange posts what a native app posts: JSON, no Origin, no |
| +// Sec-Fetch-Site, no cookies. |
| +func (f *routerHomeFixture) nativeExchange(t *testing.T, subject string, extra map[string]string) (*httptest.ResponseRecorder, nativeAnswer) { |
| + t.Helper() |
| + raw := make([]byte, 32) |
| + if _, err := rand.Read(raw); err != nil { |
| + t.Fatal(err) |
| + } |
| + verifier := base64.RawURLEncoding.EncodeToString(raw) |
| + challenge, _ := handoff.NativeChallenge(verifier) |
| + nonce, _ := handoff.NativeNonce(challenge) |
| + body := map[string]string{"assertion": f.token(t, nonce, subject, time.Now().Add(-time.Minute), nil), "verifier": verifier} |
| + for k, v := range extra { |
| + body[k] = v |
| + } |
| + encoded, err := json.Marshal(body) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r := httptest.NewRequest("POST", bridgeOrigin+connect.NativeExchangePath, bytes.NewReader(encoded)) |
| + r.Header.Set("Content-Type", "application/json") |
| + rec := f.browser().serve(r) |
| + if len(rec.Result().Cookies()) != 0 { |
| + t.Fatal("the native exchange set a cookie") |
| + } |
| + var answer nativeAnswer |
| + if err := json.Unmarshal(rec.Body.Bytes(), &answer); err != nil { |
| + t.Fatalf("answer %d %q: %v", rec.Code, rec.Body.String(), err) |
| + } |
| + return rec, answer |
| +} |
| + |
| +// nativeList is the native app's document list, with the minted session. |
| +func (f *routerHomeFixture) nativeList(t *testing.T, a nativeAnswer) *httptest.ResponseRecorder { |
| + t.Helper() |
| + r := httptest.NewRequest("GET", bridgeOrigin+"/", nil) |
| + r.Header.Set("Accept", "application/json") |
| + r.AddCookie(&http.Cookie{Name: a.Name, Value: a.Value}) |
| + return f.browser().serve(r) |
| +} |
| + |
| +func TestNativeOficinaIDOpensALinkedAccount(t *testing.T) { |
| + f := newRouterHomeFixture(t) |
| + alice, err := f.app.store.EnsurePerson(t.Context(), "alice@example.com") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err = f.app.store.createDocument(t.Context(), alice, "Alice original", "", VisibilityPrivate); err != nil { |
| + t.Fatal(err) |
| + } |
| + b := f.browser() |
| + b.localSignin(t, "alice@example.com") |
| + b.linkProof(t, "pr_alice", "alice@example.com") |
| + routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| + |
| + rec, answer := f.nativeExchange(t, "pr_alice", nil) |
| + if rec.Code != 200 || answer.State != "ok" || answer.Name != f.sessions.CookieName() || answer.Value == "" { |
| + t.Fatalf("exchange=%d %s", rec.Code, rec.Body.String()) |
| + } |
| + s, ok := f.sessions.From(sessionRequest(answer)) |
| + if !ok || s.Method != connect.NativeMethod || s.Subject != "pr_alice" { |
| + t.Fatalf("minted %+v", s) |
| + } |
| + list := f.nativeList(t, answer) |
| + if list.Code != 200 || !strings.Contains(list.Body.String(), "Alice original") { |
| + t.Fatalf("native list=%d %s", list.Code, list.Body.String()) |
| + } |
| + |
| + // A subject with no account here: unknown, and the same for everyone. |
| + stranger, _ := f.nativeExchange(t, "pr_stranger", nil) |
| + chose, _ := f.nativeExchange(t, "pr_other", map[string]string{"choice": "new"}) |
| + for _, r := range []*httptest.ResponseRecorder{stranger, chose} { |
| + if r.Code != 403 || r.Body.String() != `{"state":"unknown"}` { |
| + t.Fatalf("unknown=%d %s", r.Code, r.Body.String()) |
| + } |
| + } |
| +} |
| + |
| +// Seven days after it was minted, however recently used, the native |
| +// session opens nothing: the app re-mints long before. |
| +func TestNativeSessionEndsAfterSevenDays(t *testing.T) { |
| + f := newRouterHomeFixture(t) |
| + b := f.browser() |
| + b.localSignin(t, "alice@example.com") |
| + b.linkProof(t, "pr_alice", "alice@example.com") |
| + routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| + _, answer := f.nativeExchange(t, "pr_alice", nil) |
| + if f.nativeList(t, answer).Code != 200 { |
| + t.Fatal("a fresh native session was refused") |
| + } |
| + old := time.Now().Add(-connect.NativeLifetime - time.Hour).UTC().Format(time.RFC3339) |
| + if _, err := f.app.db.Writer().Exec("UPDATE sessions SET created_at=? WHERE token_hash=?", old, sessions.HashToken(answer.Value)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := f.nativeList(t, answer); r.Code == 200 { |
| + t.Fatal("a native session older than seven days opened the list") |
| + } |
| +} |
| + |
| +// The exchange is not a browser route: a request carrying a browser's |
| +// Origin is refused by the exchange itself, not waved through. |
| +func TestNativeExchangeRefusesABrowser(t *testing.T) { |
| + f := newRouterHomeFixture(t) |
| + r := httptest.NewRequest("POST", bridgeOrigin+connect.NativeExchangePath, strings.NewReader(`{"assertion":"x","verifier":"y"}`)) |
| + r.Header.Set("Content-Type", "application/json") |
| + r.Header.Set("Origin", bridgeOrigin) |
| + rec := f.browser().serve(r) |
| + if rec.Code != 400 || rec.Body.String() != `{"state":"invalid"}` { |
| + t.Fatalf("browser POST=%d %s", rec.Code, rec.Body.String()) |
| + } |
| + form := url.Values{"assertion": {"x"}}.Encode() |
| + r = httptest.NewRequest("POST", bridgeOrigin+connect.NativeExchangePath, strings.NewReader(form)) |
| + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + if rec = f.browser().serve(r); rec.Code != 400 { |
| + t.Fatalf("form POST=%d", rec.Code) |
| + } |
| +} |
| + |
| +// In team mode a roster member with no account here chooses: 409, nothing |
| +// made, then choice "new" makes the Home-only principal and signs in. |
| +func TestTeamNativeMemberChooses(t *testing.T) { |
| + f := newTeamRouterFixture(t) |
| + rec, answer := f.nativeExchange(t, teamSubject('b'), nil) |
| + if rec.Code != 409 || answer.State != "choose" { |
| + t.Fatalf("member without a person=%d %s", rec.Code, rec.Body.String()) |
| + } |
| + if f.app.store.team.principals == nil { |
| + t.Fatal("no principal store") |
| + } |
| + if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('b')); e == nil { |
| + t.Fatal("409 allocated a principal") |
| + } |
| + rec, answer = f.nativeExchange(t, teamSubject('b'), map[string]string{"choice": "new"}) |
| + if rec.Code != 200 || answer.State != "ok" { |
| + t.Fatalf("choice new=%d %s", rec.Code, rec.Body.String()) |
| + } |
| + if list := f.nativeList(t, answer); list.Code != 200 { |
| + t.Fatalf("native list=%d", list.Code) |
| + } |
| + // Non-members get the identical unknown, whatever they send. |
| + for _, extra := range []map[string]string{nil, {"choice": "new"}} { |
| + if rec, _ := f.nativeExchange(t, teamSubject('z'), extra); rec.Code != 403 || rec.Body.String() != `{"state":"unknown"}` { |
| + t.Fatalf("non-member=%d %s", rec.Code, rec.Body.String()) |
| + } |
| + } |
| + // The roster unavailable: 503, nothing made. |
| + f.directory.change(nil, true) |
| + f.clock.step(61 * time.Second) |
| + if rec, _ := f.nativeExchange(t, teamSubject('c'), map[string]string{"choice": "new"}); rec.Code != 503 { |
| + t.Fatalf("undecided=%d %s", rec.Code, rec.Body.String()) |
| + } |
| +} |
| + |
| +func sessionRequest(a nativeAnswer) *http.Request { |
| + r := httptest.NewRequest("GET", bridgeOrigin+"/", nil) |
| + r.AddCookie(&http.Cookie{Name: a.Name, Value: a.Value}) |
| + return r |
| +} |