Let a member link Oficina ID again after BASE_ORIGIN changes
connect binds each Oficina ID link to Base's origin (linkstore's person
binding), so after a move every link made at the old origin resolves
nobody. It also stayed in the way: Settings still said "connected",
and connect's confirm refuses while any link names the member or the
subject, so the member could not link again and Base had no unlink.
A link connect no longer resolves (asked through Bridge.ResolveOn, for a
member who exists) is now dead to Base:
- Settings offers "Connect your Oficina account" again.
- POST /account/home/confirm removes the member's own dead link just
before connect's confirm runs, so the new link takes its place. Only
the member's fresh email sign-in (connect.FreshLocal, what linking
itself demands) clears it, and connect's confirm then checks that
sign-in and a fresh Oficina proof as always.
- A link that still resolves is never touched, nor another member's
dead link, so no other Oficina ID can take a linked member over.
The removal is the app-side removal connect's README asks for ("delete
the account's home_identity_links rows", "address changes require
explicit removal and fresh linking"), as RemoveMember already does. No
connect change, and no new strings: the member links again the way an
unlinked member does (Sign in with Oficina, then email once; or from
Settings).
Tests simulate the move with connect's own Adopt under the old origin:
the member links again both ways and Oficina ID signs them in; a live
link is not replaced; another member's dead link stays; a signed-out,
cross-site or old sign-in clears nothing. README says what a move means.
make ci: 16 packages ran, 0 failed; test step 10.3s of 60s.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+309
−5
README.md+6 −0internal/store/oficina_links.go+30 −0internal/web/app.go+1 −1internal/web/oficina.go+78 −4internal/webtest/oficina_origin_test.go+194 −0
diff --git a/README.md b/README.md| index 9eeabb7..7b74326 100644 |
| --- a/README.md |
| +++ b/README.md |
| @@ -88,6 +88,12 @@ email and passkeys stay below it. Everyone signs in with their email once and |
| connects their Oficina account from Settings; after that, Oficina signs them |
| in. Only members get in, and inviting people is still by email. |
| +Each connection is tied to `BASE_ORIGIN`. If you move Base to a new address, |
| +Oficina ID stops signing people in until they connect again: each person |
| +signs in with their email once and connects their Oficina account, from |
| +Settings or straight from "Sign in with Oficina". The old connection is |
| +cleared then. Nothing else is needed. |
| + |
| Base's Mac and iPhone app signs in with Oficina ID too, through |
| `POST /native/oficina-id`; its session lasts seven days, and the app renews |
| it. Someone must have signed in to Base on the web with Oficina ID once |
diff --git a/internal/store/oficina_links.go b/internal/store/oficina_links.go| new file mode 100644 |
| index 0000000..e409e32 |
| --- /dev/null |
| +++ b/internal/store/oficina_links.go |
| @@ -0,0 +1,30 @@ |
| +package store |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| + "errors" |
| +) |
| + |
| +// Oficina ID links are connect's rows (home_identity_links). connect checks |
| +// them, and Base reads and removes them only as connect asks an app to: |
| +// removing a member removes their links (RemoveMember), and a link connect |
| +// no longer resolves is removed before the member links again |
| +// (web.oficina.dropDeadLink). Nothing here writes one. |
| + |
| +// OficinaLinkSubject is the Oficina account (subject at issuer) linked to |
| +// member number n; ok is false when there is none. |
| +func OficinaLinkSubject(ctx context.Context, q QueryRower, issuer string, n int64) (subject string, ok bool, err error) { |
| + err = q.QueryRowContext(ctx, `SELECT subject FROM home_identity_links WHERE issuer = ? AND person_id = ?`, issuer, n).Scan(&subject) |
| + if errors.Is(err, sql.ErrNoRows) { |
| + return "", false, nil |
| + } |
| + return subject, err == nil, err |
| +} |
| + |
| +// DeleteOficinaLink removes the link between subject at issuer and member |
| +// number n, if it is still that link. |
| +func DeleteOficinaLink(ctx context.Context, tx *sql.Tx, issuer, subject string, n int64) error { |
| + _, err := tx.ExecContext(ctx, `DELETE FROM home_identity_links WHERE issuer = ? AND subject = ? AND person_id = ?`, issuer, subject, n) |
| + return err |
| +} |
diff --git a/internal/web/app.go b/internal/web/app.go| index 945d507..986510c 100644 |
| --- a/internal/web/app.go |
| +++ b/internal/web/app.go |
| @@ -171,7 +171,7 @@ func App(d *db.DB, cfg config.Config, mailer mail.Sender, inst Installer, logger |
| r.With(sess.Require).Get("/account/home", o.routes.Account) |
| r.With(sess.Require).Post("/account/home", o.routes.LinkStart) |
| r.With(sess.Require).Get("/account/home/confirm", o.bridge.ConfirmPage) |
| - r.With(sess.Require).Post("/account/home/confirm", o.routes.ConfirmPost) |
| + r.With(sess.Require).Post("/account/home/confirm", a.oficinaConfirm) |
| r.Post("/auth/begin", o.routes.LocalGuard(a.auth.Begin)) |
| r.Get("/auth/callback", o.routes.LocalGuard(a.auth.Callback)) |
| r.Get("/auth/verify", o.routes.LocalGuard(a.auth.Verify)) |
diff --git a/internal/web/oficina.go b/internal/web/oficina.go| index c602275..7ee6b77 100644 |
| --- a/internal/web/oficina.go |
| +++ b/internal/web/oficina.go |
| @@ -6,6 +6,7 @@ import ( |
| "errors" |
| "net/http" |
| "net/url" |
| + "time" |
| "amadan.net/oficina/home/connect" |
| "amadan.net/oficina/home/connect/linkstore" |
| @@ -30,6 +31,7 @@ type oficina struct { |
| bridge *connect.Bridge |
| routes *connect.Routes |
| db *sql.DB |
| + issuer string |
| } |
| func (a *app) newOficina(home *connect.DecodedCredentials) (*oficina, error) { |
| @@ -90,7 +92,7 @@ func (a *app) newOficina(home *connect.DecodedCredentials) (*oficina, error) { |
| if err != nil { |
| return nil, err |
| } |
| - return &oficina{bridge: b, routes: routes, db: a.db}, nil |
| + return &oficina{bridge: b, routes: routes, db: a.db, issuer: home.Issuer}, nil |
| } |
| // formPolicy is the Content-Security-Policy rastrillo gives every Base |
| @@ -121,15 +123,87 @@ func (o *oficina) member(r *http.Request) (store.Member, bool, error) { |
| return m, true, nil |
| } |
| -// linked reports whether member m has an Oficina account connected. An |
| -// error counts as linked, so Settings offers nothing it cannot be sure of. |
| +// linked reports whether member m has an Oficina account connected: a |
| +// link connect resolves. A dead link (deadLink) is no connection. An error |
| +// counts as linked, so Settings offers nothing it cannot be sure of. |
| func (o *oficina) linked(ctx context.Context, m store.Member) bool { |
| n, err := store.MemberNumber(ctx, o.db, m.ID) |
| if err != nil { |
| return true |
| } |
| linked, err := o.bridge.Linked(ctx, n) |
| - return err != nil || linked |
| + if err != nil || !linked { |
| + return err != nil |
| + } |
| + _, dead, err := o.deadLink(ctx, o.db, n) |
| + return err != nil || !dead |
| +} |
| + |
| +// deadLink reports whether member number n has a link connect no longer |
| +// resolves, and its subject. connect binds each link to Base's origin (and |
| +// the member's address), so every link made before BASE_ORIGIN changed is |
| +// one: it signs nobody in, yet it would stop the member linking again. |
| +// Asked only for a member who exists, so a refusal is the binding's. |
| +func (o *oficina) deadLink(ctx context.Context, q store.QueryRower, n int64) (subject string, dead bool, err error) { |
| + subject, ok, err := store.OficinaLinkSubject(ctx, q, o.issuer, n) |
| + if err != nil || !ok { |
| + return "", false, err |
| + } |
| + switch _, err := o.bridge.ResolveOn(ctx, q, subject); { |
| + case err == nil: |
| + return subject, false, nil |
| + case errors.Is(err, linkstore.ErrRefused): |
| + return subject, true, nil |
| + default: |
| + return "", false, err |
| + } |
| +} |
| + |
| +// dropDeadLink removes member m's dead link, if they have one, so that the |
| +// link they are confirming can take its place. connect's confirm refuses |
| +// while any link names the member or the subject, and offers no way to |
| +// replace one; connect asks the app to remove links itself (as |
| +// RemoveMember does). A link connect still resolves is never touched, nor |
| +// anyone else's. |
| +func (o *oficina) dropDeadLink(ctx context.Context, m store.Member) error { |
| + tx, err := o.db.BeginTx(ctx, nil) |
| + if err != nil { |
| + return err |
| + } |
| + defer tx.Rollback() |
| + n, err := store.MemberNumber(ctx, tx, m.ID) |
| + if err != nil { |
| + return err |
| + } |
| + subject, dead, err := o.deadLink(ctx, tx, n) |
| + if err != nil || !dead { |
| + return err |
| + } |
| + if err := store.DeleteOficinaLink(ctx, tx, o.issuer, subject, n); err != nil { |
| + return err |
| + } |
| + return tx.Commit() |
| +} |
| + |
| +// oficinaConfirm is POST /account/home/confirm: connect's confirm, after |
| +// the member's dead link, if any, has made way. Only the member's own fresh |
| +// sign-in by email (connect.FreshLocal, what linking itself demands) clears |
| +// it; connect's confirm then checks that sign-in and the fresh Oficina |
| +// proof as always. |
| +func (a *app) oficinaConfirm(w http.ResponseWriter, r *http.Request) { |
| + s, ok := sessions.Current(r) |
| + if _, fresh := connect.FreshLocal(connect.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: s.At}, time.Now()); ok && fresh { |
| + m, found, err := store.MemberByEmail(r.Context(), a.db, s.Subject) |
| + if err == nil && found { |
| + err = a.oficina.dropDeadLink(r.Context(), m) |
| + } |
| + if err != nil { |
| + a.logger.Error("oficina: dead link", "err", err) |
| + http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| + return |
| + } |
| + } |
| + a.oficina.routes.ConfirmPost(w, r) |
| } |
| // oficinaSignin is GET /signin with Oficina ID on. Someone signed in as a |
diff --git a/internal/webtest/oficina_origin_test.go b/internal/webtest/oficina_origin_test.go| new file mode 100644 |
| index 0000000..5a51d89 |
| --- /dev/null |
| +++ b/internal/webtest/oficina_origin_test.go |
| @@ -0,0 +1,194 @@ |
| +package webtest |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "net/http" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect/linkstore" |
| + |
| + "amadan.net/carlos/base/internal/store" |
| +) |
| + |
| +// Moving Base to another BASE_ORIGIN. connect stamps each Oficina ID link |
| +// with Base's origin (its person binding), so a link made at the old origin |
| +// resolves nobody at the new one, and would block linking again. Base |
| +// treats such a link as gone: the member links again, proving who they are |
| +// by email as at first, and the dead link makes way for the new one. |
| + |
| +// oldOrigin is where Base ran before the move. |
| +const oldOrigin = "https://base.old.test" |
| + |
| +// linkAtOldOrigin stores the link connect would have made between email's |
| +// member and subject while Base ran at oldOrigin: the same key, issuer and |
| +// namespace, another origin. It is connect's own carry-over (Adopt), so the |
| +// row is exactly the one linking there wrote. |
| +func (a *testApp) linkAtOldOrigin(email, subject string) { |
| + a.t.Helper() |
| + ctx := context.Background() |
| + old, err := linkstore.New(a.DB, bytes.Repeat([]byte{9}, 32), oldOrigin, homeIssuer, "carlos/base", |
| + func(ctx context.Context, q linkstore.Queryer, n int64) (string, error) { |
| + return store.MemberAddress(ctx, q, n) |
| + }) |
| + if err != nil { |
| + a.t.Fatal(err) |
| + } |
| + m, ok, err := store.MemberByEmail(ctx, a.DB, email) |
| + if err != nil || !ok { |
| + a.t.Fatalf("member %s: %v", email, err) |
| + } |
| + n, err := store.MemberNumber(ctx, a.DB, m.ID) |
| + if err != nil { |
| + a.t.Fatal(err) |
| + } |
| + tx, err := a.DB.BeginTx(ctx, nil) |
| + if err != nil { |
| + a.t.Fatal(err) |
| + } |
| + defer tx.Rollback() |
| + if err := old.Adopt(ctx, tx, homeIssuer, subject, n); err != nil { |
| + a.t.Fatal(err) |
| + } |
| + if err := tx.Commit(); err != nil { |
| + a.t.Fatal(err) |
| + } |
| + // At the old origin it resolves; here it does not. |
| + if _, _, err := old.Resolve(ctx, homeIssuer, subject); err != nil { |
| + a.t.Fatalf("the old link does not resolve at the old origin: %v", err) |
| + } |
| +} |
| + |
| +// linkRows is how many Oficina ID links name email's member. |
| +func (a *testApp) linkRows(email string) int { |
| + a.t.Helper() |
| + var n int |
| + if err := a.DB.QueryRow(`SELECT count(*) FROM home_identity_links l JOIN member_numbers n ON n.number = l.person_id |
| + JOIN members m ON m.id = n.member_id WHERE m.email = ?`, email).Scan(&n); err != nil { |
| + a.t.Fatal(err) |
| + } |
| + return n |
| +} |
| + |
| +// The move itself: the old link refuses Oficina sign-in, as it must, and |
| +// the member links again the way an unlinked member does (Oficina, then |
| +// email once), after which Oficina ID signs them in. |
| +func TestOficinaLinkFromAnOldOriginLinksAgain(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| + |
| + unknown := a.signInWithOficina("subject-owner") |
| + if unknown.Code != http.StatusForbidden || !strings.Contains(unknown.Body, "Oficina account not connected") { |
| + t.Fatalf("Oficina sign-in on a link from the old origin: %d\n%s", unknown.Code, unknown.Body) |
| + } |
| + r := a.signIn(ownerAddress) |
| + resume := a.get(r.Location()) |
| + if resume.Code != 303 || !strings.HasPrefix(resume.Location(), "/auth/start") { |
| + t.Fatalf("local sign-in did not resume the link: %d %q", resume.Code, resume.Location()) |
| + } |
| + a.confirmLink(a.atOficina(a.get(resume.Location()), "subject-owner", 30*time.Second), ownerAddress) |
| + if n := a.linkRows(ownerAddress); n != 1 { |
| + t.Fatalf("links: %d", n) |
| + } |
| + b := a.newBrowser() |
| + if fin := b.signInWithOficina("subject-owner"); fin.Code != 303 || fin.Location() != "/" { |
| + t.Fatalf("Oficina sign-in after linking again: %d %q\n%s", fin.Code, fin.Location(), fin.Body) |
| + } |
| + if home := b.get("/"); home.Code != 200 { |
| + t.Fatalf("Home: %d", home.Code) |
| + } |
| + if body := a.get("/settings").Body; strings.Contains(body, `href="/account/home"`) { |
| + t.Errorf("Settings still offers Connect:\n%s", body) |
| + } |
| +} |
| + |
| +// Settings no longer says the dead link is connected: it offers Connect, |
| +// and connecting from there works too. |
| +func TestOficinaLinkFromAnOldOriginConnectsFromSettings(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| + a.link(ownerAddress, "subject-owner") // fails unless Settings offers Connect and the confirm goes through |
| + if n := a.linkRows(ownerAddress); n != 1 { |
| + t.Fatalf("links: %d", n) |
| + } |
| + if fin := a.newBrowser().signInWithOficina("subject-owner"); fin.Code != 303 || fin.Location() != "/" { |
| + t.Fatalf("Oficina sign-in: %d %q", fin.Code, fin.Location()) |
| + } |
| +} |
| + |
| +// A link that still resolves is never replaced: another Oficina ID can't |
| +// take a linked member over, even with their email. |
| +func TestOficinaLiveLinkIsNotReplaced(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.link(ownerAddress, "subject-owner") |
| + if body := a.get("/settings").Body; strings.Contains(body, `href="/account/home"`) { |
| + t.Fatalf("Settings offers Connect to a linked member") |
| + } |
| + toIssuer := a.get(a.post("/account/home", nil).Location()) |
| + finish := a.atOficina(toIssuer, "subject-intruder", 30*time.Second) |
| + if finish.Code != 303 || finish.Location() != "/account/home/confirm" { |
| + t.Fatalf("link finish: %d %q", finish.Code, finish.Location()) |
| + } |
| + if r := a.post("/account/home/confirm", nil); r.Code != http.StatusForbidden { |
| + t.Fatalf("confirming a second Oficina ID: %d %q", r.Code, r.Location()) |
| + } |
| + if fin := a.newBrowser().signInWithOficina("subject-owner"); fin.Code != 303 { |
| + t.Errorf("the live link stopped working: %d", fin.Code) |
| + } |
| + if fin := a.newBrowser().signInWithOficina("subject-intruder"); fin.Code != http.StatusForbidden { |
| + t.Errorf("the other Oficina ID signs in: %d", fin.Code) |
| + } |
| +} |
| + |
| +// Linking again clears only the member's own dead link: another member's |
| +// dead link to the same Oficina ID stays, and the confirm is refused. |
| +func TestOficinaOldLinkOfAnotherMemberStays(t *testing.T) { |
| + a := newHomeApp(t) |
| + if _, _, err := store.Invite(context.Background(), a.DB, "ana@acme.test"); err != nil { |
| + t.Fatal(err) |
| + } |
| + a.linkAtOldOrigin("ana@acme.test", "subject-shared") |
| + a.signedInOwner() |
| + toIssuer := a.get(a.post("/account/home", nil).Location()) |
| + a.atOficina(toIssuer, "subject-shared", 30*time.Second) |
| + if r := a.post("/account/home/confirm", nil); r.Code != http.StatusForbidden { |
| + t.Fatalf("the owner took Ana's Oficina ID: %d %q", r.Code, r.Location()) |
| + } |
| + if a.linkRows("ana@acme.test") != 1 || a.linkRows(ownerAddress) != 0 { |
| + t.Fatalf("links: ana %d, owner %d", a.linkRows("ana@acme.test"), a.linkRows(ownerAddress)) |
| + } |
| +} |
| + |
| +// Nothing but the member's own fresh email sign-in clears a dead link: |
| +// not a signed-out POST, nor a POST from another site, nor an old sign-in. |
| +func TestOficinaOldLinkClearedOnlyByTheMember(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| + if r := a.postFrom(homeIssuer, "/account/home/confirm", url.Values{}); r.Code != http.StatusForbidden { |
| + t.Fatalf("cross-site confirm: %d", r.Code) |
| + } |
| + if r := a.post("/account/home/confirm", nil); r.Code == http.StatusOK { |
| + t.Fatalf("signed-out confirm: %d", r.Code) |
| + } |
| + if n := a.linkRows(ownerAddress); n != 1 { |
| + t.Fatalf("a refused request cleared the link: %d", n) |
| + } |
| +} |
| + |
| +// An email sign-in older than connect's five minutes clears nothing. |
| +func TestOficinaOldLinkNeedsAFreshSignIn(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| + a.signedInOwner() |
| + old := time.Now().Add(-10 * time.Minute).UTC().Format(time.RFC3339) |
| + if _, err := a.DB.Exec(`UPDATE sessions SET created_at = ?, auth_time = ?`, old, old); err != nil { |
| + t.Fatal(err) |
| + } |
| + a.post("/account/home/confirm", nil) |
| + if n := a.linkRows(ownerAddress); n != 1 { |
| + t.Fatalf("an old sign-in cleared the link: %d", n) |
| + } |
| +} |