oficina / base Public

Merge branch 'origin-move' into main

Merge branch 'origin-move' into main

Oficina ID links made under a former BASE_ORIGIN count as dead; the member
re-links after a fresh email sign-in, replacing only their own dead link
(runbook P2 for the move to base.oficina.works). Codex Astra approved at
ebd3b8f; its test nit applied in 7e998c1. make ci green.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev c49f6b4cf52a2b60eae9965b4f009875f9081b05 parent 3f8e6d1parent 7e998c1
5 files changed, +337 −5
  • README.md +6 −0
  • internal/store/oficina_links.go +30 −0
  • internal/web/app.go +1 −1
  • internal/web/oficina.go +78 −4
  • internal/webtest/oficina_origin_test.go +222 −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..74c6fba
--- /dev/null
+++ b/internal/webtest/oficina_origin_test.go
@@ -0,0 +1,222 @@
+package webtest
+
+import (
+ "bytes"
+ "context"
+ "fmt"
+ "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")
+ before := a.linkRow(ownerAddress)
+ unchanged := func(what string) {
+ t.Helper()
+ if after := a.linkRow(ownerAddress); after != before {
+ t.Fatalf("%s changed the dead link: %q, was %q", what, after, before)
+ }
+ }
+ if r := a.postFrom(homeIssuer, "/account/home/confirm", url.Values{}); r.Code != http.StatusForbidden {
+ t.Fatalf("signed-out cross-site confirm: %d", r.Code)
+ }
+ unchanged("a signed-out cross-site confirm")
+ if r := a.post("/account/home/confirm", nil); r.Code == http.StatusOK {
+ t.Fatalf("signed-out confirm: %d", r.Code)
+ }
+ unchanged("a signed-out confirm")
+ // The owner freshly signed in by email, and a page on another site
+ // posting the confirm in their browser: refused before anything is
+ // cleared.
+ a.signedInOwner()
+ if r := a.postFrom(homeIssuer, "/account/home/confirm", url.Values{}); r.Code != http.StatusForbidden {
+ t.Fatalf("signed-in cross-site confirm: %d", r.Code)
+ }
+ unchanged("a signed-in cross-site confirm")
+}
+
+// linkRow is the Oficina ID link naming email's member, as stored.
+func (a *testApp) linkRow(email string) string {
+ a.t.Helper()
+ var subject, binding string
+ var person int64
+ if err := a.DB.QueryRow(`SELECT l.subject, l.person_id, l.person_binding 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(&subject, &person, &binding); err != nil {
+ a.t.Fatalf("link row for %s: %v", email, err)
+ }
+ return fmt.Sprintf("%s/%d/%s", subject, person, binding)
+}
+
+// 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)
+ }
+}