connect/linkstore: Adopt, for moving an app's existing links onto connect
An app moving its own Home link table onto connect cannot copy rows in SQL: every home_identity_links row carries person_binding, an HMAC under the storage key over namespace, origin, issuer, person ID and address, and Resolve refuses a mismatch. Store.Adopt(ctx, tx, issuer, subject, personID) writes the row Confirm would write, computing the binding from LookupPerson inside the caller's transaction; Bridge.Adopt delegates. Idempotent for an identical row; refuses, writing nothing, a foreign issuer, an invalid subject or person ID, a person the lookup does not return, the subject or the person already linked otherwise, and a stale or unbound existing row (never repaired). No schema change; the address is never written. README documents the boot-time move; implementation/linkstore-adopt.md records the design and 13 hand mutations (one equivalent survivor). Requested by Fable's review on oficina/docs #100. No existing caller changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+548
−0
connect/README.md+45 −0connect/adopt_test.go+49 −0connect/api.go+8 −0connect/linkstore/adopt_test.go+314 −0connect/linkstore/store.go+48 −0implementation/linkstore-adopt.md+84 −0
CI passed — run details
diff --git a/connect/README.md b/connect/README.md| index 8f5d3bb..6c185a8 100644 |
| --- a/connect/README.md |
| +++ b/connect/README.md |
| @@ -120,6 +120,51 @@ who has never had a local account (`home/teammode`, |
| `Bridge.Linked` knows nothing of Home-only people, so an app should not offer |
| "Connect your Oficina account" to one: there is no local account to connect. |
| +## Moving existing links onto connect |
| + |
| +An app that already links Home identities in its own table (Docs' |
| +`docs_identity_links`) carries them over with `Bridge.Adopt(ctx, tx, issuer, |
| +subject, personID)`. A plain SQL copy does not work. Every |
| +`home_identity_links` row carries `person_binding`, a keyed hash of the |
| +person's address under the storage key, and `Resolve` refuses a row whose |
| +binding does not match. Copied rows would stop resolving at once. `Adopt` |
| +asks `LookupPerson` for the address inside your transaction and writes the |
| +row that linking here would have written. The address is hashed and never |
| +stored. `Adopt` checks nothing about the subject: your table vouches for it. |
| + |
| +Do it once, at boot, after the migrations and `New` and before serving: |
| + |
| +1. Begin a transaction on `Config.DB`. |
| +2. Read your old rows and call `Adopt` for each. |
| +3. Retire the old table in the same transaction, then commit. |
| + |
| +If the old table is absent, there is nothing to do. A repeat of an identical |
| +row is not an error, so a boot that stops partway can simply run again. |
| +`ErrRefused` means nothing was written. Its causes are: |
| + |
| +- a foreign issuer; |
| +- an invalid subject; |
| +- a person `LookupPerson` does not return now (that link could not resolve |
| + anyway); |
| +- the subject already linked to another person; |
| +- the person already linked under another subject; |
| +- an existing row with a stale or missing binding. Adopt never repairs one. |
| + |
| +The app decides whether a refusal aborts the move or drops that row, and |
| +should say which. `ErrStorage` means roll back. |
| + |
| +`LookupPerson` must read through the `Queryer` it is given, as it must |
| +already. A lookup on its own connection deadlocks against the open |
| +transaction on a single-connection pool. |
| + |
| +Carry the old link clock in the same transaction with |
| +`UPDATE home_link_clock SET high_water = MAX(high_water, ?) WHERE id = 1`. |
| +Do not carry in-flight link flows. They last five minutes, and a person |
| +mid-link starts again. |
| + |
| +`Adopt` does not consult `Config.HomeOnly`. An app with Home-only people |
| +keeps their subjects disjoint from links itself, as Docs does with triggers. |
| + |
| ## Extraction record |
| The link proof protocol, bridge, freshness rules and navigation originate in |
diff --git a/connect/adopt_test.go b/connect/adopt_test.go| new file mode 100644 |
| index 0000000..e850753 |
| --- /dev/null |
| +++ b/connect/adopt_test.go |
| @@ -0,0 +1,49 @@ |
| +package connect |
| + |
| +import ( |
| + "errors" |
| + "net/http" |
| + "net/http/httptest" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect/linkstore" |
| +) |
| + |
| +// TestAdoptedLinkSignsInAsTheLinkedPerson is the move an app makes once at |
| +// boot: carry its own link row over inside one transaction, then serve. An |
| +// ordinary Home sign-in for that subject is the same local person, exactly |
| +// as after linking here. |
| +func TestAdoptedLinkSignsInAsTheLinkedPerson(t *testing.T) { |
| + f := newBridgeFixture(t) |
| + f.local(t, "one@example.com", "magiclink", time.Time{}) |
| + person := f.personID(t, "one@example.com") |
| + ctx := t.Context() |
| + tx, err := f.db.BeginTx(ctx, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := f.b.Adopt(ctx, tx, "https://other.example", "opaque-subject-one", person); !errors.Is(err, linkstore.ErrRefused) { |
| + t.Fatal("foreign issuer adopted", err) |
| + } |
| + if err := f.b.Adopt(ctx, tx, bridgeIssuer, "opaque-subject-one", person); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := tx.Commit(); err != nil { |
| + t.Fatal(err) |
| + } |
| + if linked, err := f.b.Linked(ctx, person); err != nil || !linked { |
| + t.Fatal("adopted person not linked", linked, err) |
| + } |
| + pending, nonce := f.start(t, nil, "/d/existing") |
| + inbox := f.callback(t, "opaque-subject-one", nonce, time.Now().Add(-time.Minute)) |
| + finish := httptest.NewRecorder() |
| + f.b.Finish(finish, bridgeRequest(http.MethodGet, "/auth/finish", []*http.Cookie{pending, inbox})) |
| + if finish.Code != 303 || finish.Header().Get("Location") != "/d/existing" { |
| + t.Fatalf("ordinary finish=%d %q", finish.Code, finish.Header().Get("Location")) |
| + } |
| + identity, err := f.b.ResolveSession(bridgeRequest(http.MethodGet, "/", []*http.Cookie{bridgeCookie(t, finish, f.b.sessionCookie)})) |
| + if err != nil || identity.ID != person || identity.Label != "one@example.com" { |
| + t.Fatal("adopted link resolved to", identity, err) |
| + } |
| +} |
diff --git a/connect/api.go b/connect/api.go| index abefd56..3adc220 100644 |
| --- a/connect/api.go |
| +++ b/connect/api.go |
| @@ -108,6 +108,14 @@ func homeOnlyPerson(p Person, err error) (Person, error) { |
| func (b *Bridge) Linked(ctx context.Context, person int64) (bool, error) { |
| return b.links.Linked(ctx, person) |
| } |
| + |
| +// Adopt carries one link from an app's own link table into connect's, inside |
| +// the app's transaction, so the row resolves exactly as one made by linking |
| +// here would. It exists for moving an app onto connect, once, at boot and |
| +// before serving; see linkstore.Store.Adopt for what it refuses. |
| +func (b *Bridge) Adopt(ctx context.Context, tx *sql.Tx, issuer, subject string, person int64) error { |
| + return b.links.Adopt(ctx, tx, issuer, subject, person) |
| +} |
| func (b *Bridge) ResolveSession(r *http.Request) (Person, error) { |
| session, err := b.actualSession(r) |
| if err != nil || session == nil { |
diff --git a/connect/linkstore/adopt_test.go b/connect/linkstore/adopt_test.go| new file mode 100644 |
| index 0000000..ecb9277 |
| --- /dev/null |
| +++ b/connect/linkstore/adopt_test.go |
| @@ -0,0 +1,314 @@ |
| +package linkstore |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "database/sql" |
| + "errors" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// adopt runs Adopt in its own transaction and commits whatever it wrote. |
| +func adopt(t *testing.T, s *Store, issuer, subject string, person int64) error { |
| + t.Helper() |
| + tx, err := s.db.BeginTx(ctx, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer tx.Rollback() |
| + err = s.Adopt(ctx, tx, issuer, subject, person) |
| + if err != nil && !errors.Is(err, ErrRefused) { |
| + return err |
| + } |
| + if cerr := tx.Commit(); cerr != nil { |
| + t.Fatal(cerr) |
| + } |
| + return err |
| +} |
| + |
| +func links(t *testing.T, s *Store) string { |
| + t.Helper() |
| + var all sql.NullString |
| + if err := s.db.QueryRow("SELECT group_concat(issuer || '|' || subject || '|' || person_id || '|' || person_binding, ';') FROM (SELECT * FROM home_identity_links ORDER BY issuer, subject)").Scan(&all); err != nil { |
| + t.Fatal(err) |
| + } |
| + return all.String |
| +} |
| + |
| +func storedBinding(t *testing.T, s *Store, subject string) string { |
| + t.Helper() |
| + var binding string |
| + if err := s.db.QueryRow("SELECT person_binding FROM home_identity_links WHERE issuer = ? AND subject = ?", issuer, subject).Scan(&binding); err != nil { |
| + t.Fatal(err) |
| + } |
| + return binding |
| +} |
| + |
| +// TestAdoptedLinkIsTheLinkConfirmWouldWrite: the adopted row resolves, and |
| +// its binding is byte for byte the one the link flow stores. |
| +func TestAdoptedLinkIsTheLinkConfirmWouldWrite(t *testing.T) { |
| + s, _ := fixture(t) |
| + l := local(1) |
| + if _, err := s.Confirm(ctx, complete(t, s, l, "subject-one", 1), l, testNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + linked := storedBinding(t, s, "subject-one") |
| + if _, err := s.db.Exec("DELETE FROM home_identity_links"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := adopt(t, s, issuer, "subject-one", 1); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := storedBinding(t, s, "subject-one"); got != linked || got == "" { |
| + t.Fatalf("adopted binding %q, linked %q", got, linked) |
| + } |
| + id, address, err := s.Resolve(ctx, issuer, "subject-one") |
| + if err != nil || id != 1 || address != "one@example.com" { |
| + t.Fatalf("resolve=%d,%q,%v", id, address, err) |
| + } |
| + if linked, err := s.Linked(ctx, 1); err != nil || !linked { |
| + t.Fatal("adopted person not reported linked", linked, err) |
| + } |
| +} |
| + |
| +func TestAdoptIsIdempotent(t *testing.T) { |
| + s, _ := fixture(t) |
| + if err := adopt(t, s, issuer, "subject-one", 1); err != nil { |
| + t.Fatal(err) |
| + } |
| + once := links(t, s) |
| + if err := adopt(t, s, issuer, "subject-one", 1); err != nil { |
| + t.Fatal("second adoption of the same link", err) |
| + } |
| + tx, err := s.db.BeginTx(ctx, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for range 2 { |
| + if err := s.Adopt(ctx, tx, issuer, "subject-two", 2); err != nil { |
| + t.Fatal("repeat inside one transaction", err) |
| + } |
| + } |
| + if err := tx.Commit(); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := links(t, s); !strings.HasPrefix(got, once+";") || strings.Count(got, ";") != 1 { |
| + t.Fatalf("links after repeats: %q", got) |
| + } |
| + // A link made by the flow is the same link: adopting it again is a no-op. |
| + l := local(1) |
| + s2, _ := fixture(t) |
| + if _, err := s2.Confirm(ctx, complete(t, s2, l, "subject-one", 1), l, testNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + before := links(t, s2) |
| + if err := adopt(t, s2, issuer, "subject-one", 1); err != nil || links(t, s2) != before { |
| + t.Fatal("adopting a confirmed link changed it", err) |
| + } |
| +} |
| + |
| +func TestAdoptRefusesAndWritesNothing(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + setup func(*testing.T, *Store) |
| + issuer string |
| + subject string |
| + person int64 |
| + }{ |
| + {name: "foreign-issuer", issuer: "https://other.example", subject: "subject-one", person: 1}, |
| + {name: "issuer-trailing-slash", issuer: issuer + "/", subject: "subject-one", person: 1}, |
| + {name: "empty-subject", issuer: issuer, subject: "", person: 1}, |
| + {name: "subject-with-space", issuer: issuer, subject: "subject one", person: 1}, |
| + {name: "subject-too-long", issuer: issuer, subject: strings.Repeat("s", 513), person: 1}, |
| + {name: "zero-person", issuer: issuer, subject: "subject-one", person: 0}, |
| + {name: "negative-person", issuer: issuer, subject: "subject-one", person: -1}, |
| + {name: "zero-person-despite-permissive-lookup", issuer: issuer, subject: "subject-one", person: 0, setup: func(t *testing.T, s *Store) { |
| + s.lookup = func(context.Context, Queryer, int64) (string, error) { return "anyone@example.com", nil } |
| + }}, |
| + {name: "unknown-person", issuer: issuer, subject: "subject-one", person: 99}, |
| + {name: "person-without-address", issuer: issuer, subject: "subject-one", person: 1, setup: func(t *testing.T, s *Store) { |
| + s.lookup = func(context.Context, Queryer, int64) (string, error) { return "", nil } |
| + }}, |
| + {name: "person-not-admitted", issuer: issuer, subject: "subject-one", person: 1, setup: func(t *testing.T, s *Store) { |
| + s.lookup = func(context.Context, Queryer, int64) (string, error) { return "", sql.ErrNoRows } |
| + }}, |
| + {name: "subject-linked-to-another-person", issuer: issuer, subject: "subject-two", person: 1, setup: func(t *testing.T, s *Store) { |
| + if err := adopt(t, s, issuer, "subject-two", 2); err != nil { |
| + t.Fatal(err) |
| + } |
| + }}, |
| + {name: "person-linked-under-another-subject", issuer: issuer, subject: "subject-one", person: 2, setup: func(t *testing.T, s *Store) { |
| + if err := adopt(t, s, issuer, "subject-two", 2); err != nil { |
| + t.Fatal(err) |
| + } |
| + }}, |
| + {name: "unbound-legacy-row-not-repaired", issuer: issuer, subject: "legacy", person: 1, setup: func(t *testing.T, s *Store) { |
| + if _, err := s.db.Exec("INSERT INTO home_identity_links(issuer,subject,person_id) VALUES(?,?,?)", issuer, "legacy", 1); err != nil { |
| + t.Fatal(err) |
| + } |
| + }}, |
| + {name: "old-address-row-not-repaired", issuer: issuer, subject: "subject-one", person: 1, setup: func(t *testing.T, s *Store) { |
| + if err := adopt(t, s, issuer, "subject-one", 1); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := s.db.Exec("UPDATE people SET email = 'new@example.com' WHERE id = 1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + }}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + s, _ := fixture(t) |
| + if tc.setup != nil { |
| + tc.setup(t, s) |
| + } |
| + before := links(t, s) |
| + refused(t, adopt(t, s, tc.issuer, tc.subject, tc.person)) |
| + if got := links(t, s); got != before { |
| + t.Fatalf("refusal wrote: %q -> %q", before, got) |
| + } |
| + if tc.name == "unbound-legacy-row-not-repaired" || tc.name == "old-address-row-not-repaired" { |
| + _, _, err := s.Resolve(ctx, issuer, tc.subject) |
| + refused(t, err) |
| + } |
| + }) |
| + } |
| + s, _ := fixture(t) |
| + refused(t, s.Adopt(ctx, nil, issuer, "subject-one", 1)) |
| +} |
| + |
| +func TestAdoptStorageFailuresAreGeneric(t *testing.T) { |
| + s, _ := fixture(t) |
| + s.lookup = func(context.Context, Queryer, int64) (string, error) { |
| + return "", errors.New("private-address@example.invalid") |
| + } |
| + tx, err := s.db.BeginTx(ctx, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + err = s.Adopt(ctx, tx, issuer, "subject-one", 1) |
| + tx.Rollback() |
| + if err != ErrStorage || strings.Contains(err.Error(), "private") { |
| + t.Fatal("lookup error escaped", err) |
| + } |
| + s.lookup = testLookup |
| + if _, err := s.db.Exec(`CREATE TRIGGER block_link BEFORE INSERT ON home_identity_links BEGIN SELECT RAISE(ABORT, 'private internal storage detail'); END`); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := adopt(t, s, issuer, "subject-one", 1); err != ErrStorage { |
| + t.Fatal("insert failure", err) |
| + } |
| +} |
| + |
| +// TestAdoptRunsInTheCallersTransaction: the lookup reads through tx (so a |
| +// person created in the same transaction is found), and a rollback leaves |
| +// nothing behind. |
| +func TestAdoptRunsInTheCallersTransaction(t *testing.T) { |
| + s, _ := fixture(t) |
| + tx, err := s.db.BeginTx(ctx, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + s.lookup = func(ctx context.Context, q Queryer, id int64) (string, error) { |
| + if q != tx { |
| + t.Fatal("adoption lookup escaped the caller's transaction") |
| + } |
| + return testLookup(ctx, q, id) |
| + } |
| + if _, err := tx.Exec("INSERT INTO people VALUES (3, 'three@example.com')"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.Adopt(ctx, tx, issuer, "subject-three", 3); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := tx.Rollback(); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := links(t, s); got != "" { |
| + t.Fatalf("rolled-back adoption stored %q", got) |
| + } |
| +} |
| + |
| +// TestAdoptedBindingStillFailsClosed: Resolve's binding check covers adopted |
| +// rows exactly as it covers linked ones. |
| +func TestAdoptedBindingStillFailsClosed(t *testing.T) { |
| + for _, which := range []string{"tampered", "copied-from-another-person", "other-key", "address-changed"} { |
| + t.Run(which, func(t *testing.T) { |
| + s, _ := fixture(t) |
| + if err := adopt(t, s, issuer, "subject-one", 1); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := adopt(t, s, issuer, "subject-two", 2); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, _, err := s.Resolve(ctx, issuer, "subject-one"); err != nil { |
| + t.Fatal(err) |
| + } |
| + switch which { |
| + case "tampered": |
| + b := []byte(storedBinding(t, s, "subject-one")) |
| + b[0] ^= 1 |
| + if _, err := s.db.Exec("UPDATE home_identity_links SET person_binding = ? WHERE subject = 'subject-one'", string(b)); err != nil { |
| + t.Fatal(err) |
| + } |
| + case "copied-from-another-person": |
| + if _, err := s.db.Exec("UPDATE home_identity_links SET person_binding = ? WHERE subject = 'subject-one'", storedBinding(t, s, "subject-two")); err != nil { |
| + t.Fatal(err) |
| + } |
| + case "other-key": |
| + var err error |
| + s, err = New(s.db, bytes.Repeat([]byte{43}, 32), origin, issuer, "oficina/docs", testLookup) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + case "address-changed": |
| + if _, err := s.db.Exec("UPDATE people SET email = 'new@example.com' WHERE id = 1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + _, _, err := s.Resolve(ctx, issuer, "subject-one") |
| + refused(t, err) |
| + }) |
| + } |
| +} |
| + |
| +// TestAdoptWritesNoAddress: the address LookupPerson returns reaches neither |
| +// the database nor its WAL, and Adopt adds no schema. |
| +func TestAdoptWritesNoAddress(t *testing.T) { |
| + s, path := fixture(t) |
| + const canary = "adopt-canary-5f1e@example.invalid" |
| + s.lookup = func(ctx context.Context, q Queryer, id int64) (string, error) { |
| + if _, err := testLookup(ctx, q, id); err != nil { |
| + return "", err |
| + } |
| + return canary, nil |
| + } |
| + schema := func() string { |
| + var all string |
| + if err := s.db.QueryRow("SELECT group_concat(sql, ';') FROM (SELECT sql FROM sqlite_master ORDER BY name)").Scan(&all); err != nil { |
| + t.Fatal(err) |
| + } |
| + return all |
| + } |
| + before := schema() |
| + if err := adopt(t, s, issuer, "subject-one", 1); err != nil { |
| + t.Fatal(err) |
| + } |
| + if id, address, err := s.Resolve(ctx, issuer, "subject-one"); err != nil || id != 1 || address != canary { |
| + t.Fatalf("resolve=%d,%q,%v", id, address, err) |
| + } |
| + if schema() != before { |
| + t.Fatal("adoption changed the schema") |
| + } |
| + for _, file := range []string{path, path + "-wal"} { |
| + raw, err := os.ReadFile(file) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if bytes.Contains(raw, []byte(canary)) { |
| + t.Fatalf("address in %s", filepath.Base(file)) |
| + } |
| + } |
| +} |
diff --git a/connect/linkstore/store.go b/connect/linkstore/store.go| index e0422ca..b3179f2 100644 |
| --- a/connect/linkstore/store.go |
| +++ b/connect/linkstore/store.go |
| @@ -222,6 +222,54 @@ func (s *Store) Linked(ctx context.Context, person int64) (bool, error) { |
| return true, nil |
| } |
| +// Adopt records a link an app already holds in its own table, for a one-time |
| +// move onto this one. The row is the row Confirm would write for the same |
| +// person and subject: its person binding is computed here, from the address |
| +// LookupPerson returns for personID inside tx, so the address is hashed under |
| +// the key and never written. It proves nothing about the subject; the caller |
| +// vouches that its own table bound this person to it after an explicit link. |
| +// |
| +// tx must be a transaction on the Store's database. Adopt never commits or |
| +// rolls it back, so an app can carry every row and retire its old table in |
| +// one transaction. A link already stored for the same person and subject |
| +// with a matching binding is not an error. ErrRefused means nothing was |
| +// written: a foreign issuer, an invalid subject, a person LookupPerson does |
| +// not return (missing or not admitted now, so the link would not resolve |
| +// anyway), the subject linked to another person, the person linked under |
| +// another subject, or a stored row whose binding does not match (unbound or |
| +// for an old address; Adopt never repairs one). ErrStorage means tx should |
| +// be rolled back. Adopt does not consult Config.HomeOnly: an app with |
| +// Home-only people keeps their subjects disjoint from links itself. |
| +func (s *Store) Adopt(ctx context.Context, tx *sql.Tx, issuer, subject string, personID int64) error { |
| + if tx == nil || issuer != s.issuer || !validSubject(subject) || personID <= 0 { |
| + return ErrRefused |
| + } |
| + binding, err := s.currentBinding(ctx, tx, personID) |
| + if err != nil { |
| + return err |
| + } |
| + if binding == "" { |
| + return ErrRefused |
| + } |
| + _, err = tx.ExecContext(ctx, "INSERT INTO home_identity_links (issuer, subject, person_id, person_binding) VALUES (?, ?, ?, ?) ON CONFLICT DO NOTHING", s.issuer, subject, personID, binding) |
| + if err != nil { |
| + return ErrStorage |
| + } |
| + var person int64 |
| + var stored string |
| + err = tx.QueryRowContext(ctx, "SELECT person_id, person_binding FROM home_identity_links WHERE issuer = ? AND subject = ?", s.issuer, subject).Scan(&person, &stored) |
| + if errors.Is(err, sql.ErrNoRows) { |
| + return ErrRefused |
| + } |
| + if err != nil { |
| + return ErrStorage |
| + } |
| + if person != personID || !hmac.Equal([]byte(stored), []byte(binding)) { |
| + return ErrRefused |
| + } |
| + return nil |
| +} |
| + |
| func safePath(v string) string { |
| if handoff.SafePath(v) { |
| return v |
diff --git a/implementation/linkstore-adopt.md b/implementation/linkstore-adopt.md| new file mode 100644 |
| index 0000000..63030f8 |
| --- /dev/null |
| +++ b/implementation/linkstore-adopt.md |
| @@ -0,0 +1,84 @@ |
| +# connect/linkstore: Adopt, for moving an app's existing links |
| + |
| +Status: implemented on branch `linkstore-adopt` by Claude Opus 5.5. Awaiting |
| +review by Claude Fable in the branch discussion. Asked for by Fable's review |
| +of oficina/docs #100 ("Second branch … assessment"). |
| + |
| +**Why.** Docs will move from its private `homeBridge` onto `home/connect`, so |
| +its `docs_identity_links` rows must become `home_identity_links` rows. Each |
| +connect row carries `person_binding`: an HMAC under connect's storage key |
| +over a version tag, the namespace, origin, issuer, person ID and the person's |
| +address. `Resolve` refuses a row whose binding does not match. Copying |
| +(issuer, subject, person_id) in SQL would leave every link dead. The HMAC |
| +layout is connect's unexported contract, so connect computes it. |
| + |
| +**API.** `linkstore.Store.Adopt(ctx, tx *sql.Tx, issuer, subject string, |
| +personID int64) error`, with `connect.Bridge.Adopt` delegating to it. Apps |
| +hold a Bridge, not a Store. A second Store built by hand with slightly |
| +different parameters would write rows that never resolve. |
| + |
| +**Decisions.** |
| +- **It takes the caller's `*sql.Tx` and never commits.** The move has to be |
| + atomic with retiring the old table. The app's migration runner applies SQL |
| + files, so this is a boot step in Go, not a migration file. The lookup |
| + reads through that tx, as everywhere else in linkstore. |
| +- **The row is the one Confirm writes.** The binding comes from |
| + `currentBinding`, the helper every flow stage uses. The insert is Confirm's |
| + `ON CONFLICT DO NOTHING` followed by a read-back by (issuer, subject). A |
| + stored row with the same person and an equal binding counts as success. |
| + Anything else is `ErrRefused`. |
| +- **Refusals write nothing.** Adopt refuses: |
| + - a foreign issuer; |
| + - an invalid subject; |
| + - a person ID of 0 or less, even if the app's lookup would accept it; |
| + - a person the lookup does not return, or returns with an empty address; |
| + - the subject linked to another person; |
| + - the person linked under another subject (`UNIQUE (issuer, person_id)`); |
| + - an existing row whose binding is missing or was made for an old address. |
| + |
| + The last case keeps the README rule that "old or unbound links fail |
| + closed": Adopt is not a way to rebind a link. |
| +- **The clock and the flows stay out of scope.** `home_link_clock` holds no |
| + key material, so the README tells the app to carry its high-water mark in |
| + plain SQL in the same transaction. In-flight flows last five minutes and |
| + are not carried. |
| +- **Adopt does not ask `Config.HomeOnly`.** That hook reads outside the tx, |
| + and on a single-connection pool it would deadlock. The app's own |
| + disjointness constraint, Docs' triggers, stays the guard. |
| + |
| +**Privacy.** No column and no schema change. The address goes only into the |
| +HMAC. `TestAdoptWritesNoAddress` uses a canary address that is nowhere in |
| +the database, and checks that the database and WAL never contain it and the |
| +schema is unchanged. |
| + |
| +**Tests.** `connect/linkstore/adopt_test.go` and `connect/adopt_test.go`: |
| +- the adopted binding equals the one Confirm wrote, and the row resolves; |
| +- an ordinary Home sign-in through the Bridge resolves to the adopted person; |
| +- idempotence across transactions, within one transaction, and over a |
| + confirmed link; |
| +- each refusal, with the table unchanged; |
| +- generic storage errors; |
| +- the lookup reads through the caller's transaction, and rolling back |
| + leaves nothing; |
| +- tampered, copied, wrong-key and changed-address bindings are still |
| + refused by `Resolve`. |
| + |
| +**Hand mutations.** 13 in all, one survivor. |
| + |
| +Killed: |
| +- binding altered; |
| +- issuer check dropped; |
| +- subject check dropped; |
| +- person ≤ 0 check dropped (killed by the permissive-lookup case); |
| +- empty-binding check dropped; |
| +- `INSERT OR REPLACE`; |
| +- missing read-back treated as success; |
| +- binding comparison dropped; |
| +- lookup outside the tx; |
| +- Bridge passing its own issuer; |
| +- nil-tx check dropped; |
| +- the lookup error passed through. |
| + |
| +Survived, and equivalent: dropping the `person != personID` comparison. The |
| +binding covers the ID, so the HMAC comparison catches the same rows. Confirm |
| +has the same pair. |