Remove a member's Oficina links with it; hide passkey mid-link
store.RemoveMember deletes a member and its home_identity_links rows in one transaction, as connect requires. Before, a link left behind kept that Oficina account from linking to the same person invited back. It works with Oficina ID off, when the links table does not exist. There is no UI for removing members yet. While connect's link marker is present, /signin/local hides the passkey and says to sign in with email: a passkey sign-in counts as no proof for linking, so it would quietly drop the link.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+186
−3
docs/copy-pending.md+1 −0internal/store/remove.go+33 −0internal/store/remove_test.go+68 −0internal/store/store_test.go+1 −0internal/web/signin.go+24 −2internal/web/templates/signin.html+1 −0internal/webtest/oficina_test.go+58 −1
diff --git a/docs/copy-pending.md b/docs/copy-pending.md| index 938648a..9d3c571 100644 |
| --- a/docs/copy-pending.md |
| +++ b/docs/copy-pending.md |
| @@ -177,6 +177,7 @@ Not yet reviewed. New strings Base wrote itself: |
| |---|---|---| |
| | internal/web/templates/signin.html | Sign-in page with Oficina ID on, the lead button | Sign in with Oficina | |
| | internal/web/templates/signin.html | Sign-in page with Oficina ID on, the line between that button and the email form (replaces "Use your work email." there) | Or use your work email. | |
| +| internal/web/templates/signin.html | Local sign-in page (/signin/local) while an Oficina link waits on it; replaces "Use your work email." and the passkey button is hidden | To connect your Oficina account, sign in with your email. | |
| | internal/web/templates/settings.html | Settings label, shown only while no Oficina account is connected | Oficina account | |
| | internal/web/templates/settings.html | Oficina account row, value | Not connected | |
| | internal/web/templates/settings.html | Line under the sign-out button with Oficina ID on (instead of "This signs you out of Base only. [...]") | This signs you out of Base and your Oficina account. Apps you've opened stay signed in until you sign out of each one. | |
diff --git a/internal/store/remove.go b/internal/store/remove.go| new file mode 100644 |
| index 0000000..a63fb6d |
| --- /dev/null |
| +++ b/internal/store/remove.go |
| @@ -0,0 +1,33 @@ |
| +package store |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| +) |
| + |
| +// RemoveMember removes the member with id and, in the same transaction, the |
| +// Oficina ID links that name its number, as connect requires: a link left |
| +// behind would hold its Oficina account so it could not link again. The |
| +// number itself stays taken, so it can never name anyone else. With Oficina |
| +// ID off the links table may not exist, and there is nothing to delete. |
| +func RemoveMember(ctx context.Context, db *sql.DB, id string) error { |
| + tx, err := db.BeginTx(ctx, nil) |
| + if err != nil { |
| + return err |
| + } |
| + defer tx.Rollback() |
| + var links int |
| + if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'home_identity_links'`).Scan(&links); err != nil { |
| + return err |
| + } |
| + if links > 0 { |
| + if _, err := tx.ExecContext(ctx, `DELETE FROM home_identity_links |
| + WHERE person_id IN (SELECT number FROM member_numbers WHERE member_id = ?)`, id); err != nil { |
| + return err |
| + } |
| + } |
| + if _, err := tx.ExecContext(ctx, `DELETE FROM members WHERE id = ?`, id); err != nil { |
| + return err |
| + } |
| + return tx.Commit() |
| +} |
diff --git a/internal/store/remove_test.go b/internal/store/remove_test.go| new file mode 100644 |
| index 0000000..81759ee |
| --- /dev/null |
| +++ b/internal/store/remove_test.go |
| @@ -0,0 +1,68 @@ |
| +package store |
| + |
| +import ( |
| + "context" |
| + "testing" |
| + |
| + "amadan.net/rastrillo/rastrillo/dbtest" |
| +) |
| + |
| +// homeTemplate is the database with Oficina ID's tables too. |
| +var homeTemplate = dbtest.FromSet(Boot(true)) |
| + |
| +func countLinks(t *testing.T, q QueryRower, n int64) int { |
| + t.Helper() |
| + var c int |
| + if err := q.QueryRowContext(context.Background(), `SELECT count(*) FROM home_identity_links WHERE person_id = ?`, n).Scan(&c); err != nil { |
| + t.Fatal(err) |
| + } |
| + return c |
| +} |
| + |
| +// Removing a member removes its Oficina links in the same transaction, as |
| +// connect requires, so the Oficina account is free to link again. |
| +func TestRemoveMemberDeletesItsLinks(t *testing.T) { |
| + ctx, db := context.Background(), homeTemplate.Open(t) |
| + ana, _, err := Invite(ctx, db, "ana@example.com") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + bo, _, err := Invite(ctx, db, "bo@example.com") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + na, _ := MemberNumber(ctx, db, ana.ID) |
| + nb, _ := MemberNumber(ctx, db, bo.ID) |
| + if _, err := db.Exec(`INSERT INTO home_identity_links (issuer, subject, person_id, person_binding) VALUES |
| + ('https://id.example', 'subject-ana', ?, 'x'), ('https://id.example', 'subject-bo', ?, 'y')`, na, nb); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := RemoveMember(ctx, db, ana.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, ok, _ := MemberByEmail(ctx, db, "ana@example.com"); ok { |
| + t.Error("ana is still a member") |
| + } |
| + if c := countLinks(t, db, na); c != 0 { |
| + t.Errorf("ana's links: %d left", c) |
| + } |
| + if c := countLinks(t, db, nb); c != 1 { |
| + t.Errorf("bo's links: %d, want 1", c) |
| + } |
| +} |
| + |
| +// With Oficina ID off its tables don't exist, and removing a member still |
| +// works. |
| +func TestRemoveMemberWithoutOficinaTables(t *testing.T) { |
| + ctx, db := context.Background(), open(t) |
| + ana, _, err := Invite(ctx, db, "ana@example.com") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := RemoveMember(ctx, db, ana.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, ok, _ := MemberByEmail(ctx, db, "ana@example.com"); ok { |
| + t.Error("ana is still a member") |
| + } |
| +} |
diff --git a/internal/store/store_test.go b/internal/store/store_test.go| index 6156579..cf5b569 100644 |
| --- a/internal/store/store_test.go |
| +++ b/internal/store/store_test.go |
| @@ -16,6 +16,7 @@ var template = dbtest.FromSet(BootSchema) |
| func TestMain(m *testing.M) { |
| code := budget.Main(m) |
| template.Remove() |
| + homeTemplate.Remove() |
| os.Exit(code) |
| } |
diff --git a/internal/web/signin.go b/internal/web/signin.go| index 8a5f4df..23ab35b 100644 |
| --- a/internal/web/signin.go |
| +++ b/internal/web/signin.go |
| @@ -15,6 +15,9 @@ type signinPage struct { |
| // Oficina is where "Sign in with Oficina" goes (/auth/start), set only |
| // on /signin with Oficina ID on. |
| Oficina string |
| + // Linking says an Oficina link is waiting on this local sign-in, which |
| + // only an emailed link or Keymail can give: the passkey is hidden. |
| + Linking bool |
| } |
| var problemText = map[auth.SigninProblem]string{ |
| @@ -35,8 +38,11 @@ func (a *app) signin(w http.ResponseWriter, r *http.Request) { a.signinWith(w, r |
| // when that is set. |
| func (a *app) signinWith(w http.ResponseWriter, r *http.Request, oficina string) { |
| state := a.auth.SigninState(r) |
| - door := passkeyDoor |
| - state.Passkey = &door |
| + linking := a.oficina != nil && linkPending(r) |
| + if !linking { |
| + door := passkeyDoor |
| + state.Passkey = &door |
| + } |
| a.auth.PrepareSigninResponse(w, state) |
| w.Header().Set("Cache-Control", "no-store") |
| a.render(w, "signin", signinPage{ |
| @@ -44,6 +50,7 @@ func (a *app) signinWith(w http.ResponseWriter, r *http.Request, oficina string) |
| Refused: r.URL.Query().Get("refused") == "1", |
| ProblemText: problemText[state.Problem], |
| Oficina: oficina, |
| + Linking: linking, |
| }) |
| } |
| @@ -60,3 +67,18 @@ func (a *app) render(w http.ResponseWriter, name string, data any) { |
| http.Error(w, "something went wrong", http.StatusInternalServerError) |
| } |
| } |
| + |
| +// linkNavigationCookie is the sealed marker connect leaves when it sends |
| +// someone here to sign in locally on the way to linking: from "Connect your |
| +// Oficina account" without a fresh sign-in, or after an Oficina sign-in with |
| +// no link. TestOficinaLocalSigninDuringLinkHidesPasskey goes through the |
| +// second for real, so it fails if connect renames the cookie. |
| +const linkNavigationCookie = "__Host-oficina-local-link-navigation" |
| + |
| +// linkPending reports whether connect's marker is present. Base cannot open |
| +// it, and need not: at worst a stale marker hides the passkey for the ten |
| +// minutes it lives. |
| +func linkPending(r *http.Request) bool { |
| + c, err := r.Cookie(linkNavigationCookie) |
| + return err == nil && c.Value != "" |
| +} |
diff --git a/internal/web/templates/signin.html b/internal/web/templates/signin.html| index b8b4d19..fc1961f 100644 |
| --- a/internal/web/templates/signin.html |
| +++ b/internal/web/templates/signin.html |
| @@ -12,6 +12,7 @@ |
| {{else}}<h2>Sign in to {{.Name}}</h2> |
| {{if .Refused}}<p role="status">Sign-in isn't available for that address. Ask whoever runs {{.Name}} to invite you, or try another address.</p> |
| {{else if $s.Problem}}<p role="status">{{.ProblemText}}</p> |
| +{{else if .Linking}}<p>To connect your Oficina account, sign in with your email.</p> |
| {{else if not .Oficina}}<p>Use your work email.</p> |
| {{end}}{{with .Oficina}}<a class="btn full" href="{{.}}">Sign in with Oficina</a> |
| <p class="or">Or use your work email.</p> |
diff --git a/internal/webtest/oficina_test.go b/internal/webtest/oficina_test.go| index a0c804b..77d28b5 100644 |
| --- a/internal/webtest/oficina_test.go |
| +++ b/internal/webtest/oficina_test.go |
| @@ -192,7 +192,8 @@ func TestOficinaRemovedMemberIsRefused(t *testing.T) { |
| if r := b.get("/"); r.Code != 200 { |
| t.Fatalf("ana's Home: %d", r.Code) |
| } |
| - if _, err := a.DB.Exec(`DELETE FROM members WHERE email = 'ana@acme.test'`); err != nil { |
| + ana, _, _ := store.MemberByEmail(ctx, a.DB, "ana@acme.test") |
| + if err := store.RemoveMember(ctx, a.DB, ana.ID); err != nil { |
| t.Fatal(err) |
| } |
| for _, p := range []string{"/", "/settings", "/people"} { |
| @@ -291,3 +292,59 @@ func TestOficinaConnectOfferedOnlyUntilLinked(t *testing.T) { |
| t.Errorf("still offered after linking from another browser:\n%s", body) |
| } |
| } |
| + |
| +// A removed member's Oficina account is free again: invited back, the same |
| +// person links the same Oficina account to their new membership, which has a |
| +// new number. |
| +func TestOficinaRemovedMemberCanBeReinvitedAndRelinked(t *testing.T) { |
| + a := newHomeApp(t) |
| + ctx := context.Background() |
| + old, _, err := store.Invite(ctx, a.DB, "ana@acme.test") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + a.link("ana@acme.test", "subject-ana") |
| + oldNumber, _ := store.MemberNumber(ctx, a.DB, old.ID) |
| + if err := store.RemoveMember(ctx, a.DB, old.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + again, _, err := store.Invite(ctx, a.DB, "ana@acme.test") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if n, _ := store.MemberNumber(ctx, a.DB, again.ID); n == oldNumber { |
| + t.Fatalf("re-invited member reuses number %d", n) |
| + } |
| + a.newBrowser().link("ana@acme.test", "subject-ana") |
| + b := a.newBrowser() |
| + if fin := b.signInWithOficina("subject-ana"); fin.Code != 303 || fin.Location() != "/" { |
| + t.Fatalf("Oficina sign-in after re-linking: %d %q", fin.Code, fin.Location()) |
| + } |
| + if r := b.get("/settings"); r.Code != 200 || !strings.Contains(r.Body, "ana@acme.test") { |
| + t.Fatalf("settings as the new member: %d", r.Code) |
| + } |
| +} |
| + |
| +// While a link waits on a local sign-in, a passkey would sign the person in |
| +// without connecting anything (connect accepts only an emailed link or |
| +// Keymail), so the local page hides it and says to use email. |
| +func TestOficinaLocalSigninDuringLinkHidesPasskey(t *testing.T) { |
| + a := newHomeApp(t) |
| + const line = "To connect your Oficina account, sign in with your email." |
| + if page := a.get("/signin/local"); strings.Contains(page.Body, line) || !strings.Contains(page.Body, "data-rst-passkey") { |
| + t.Fatalf("no link pending, but the page changed:\n%s", page.Body) |
| + } |
| + if r := a.signInWithOficina("subject-owner"); r.Code != http.StatusForbidden { |
| + t.Fatalf("unlinked arrival: %d", r.Code) |
| + } |
| + page := a.get("/signin/local") |
| + if page.Code != 200 || strings.Contains(page.Body, "data-rst-passkey") || !strings.Contains(page.Body, line) || !strings.Contains(page.Body, `action="/auth/begin"`) { |
| + t.Fatalf("local sign-in while linking: %d\n%s", page.Code, page.Body) |
| + } |
| + // Once the local sign-in has picked the link up, the page is as before. |
| + r := a.signIn(ownerAddress) |
| + a.get(r.Location()) |
| + if page := a.get("/signin/local"); strings.Contains(page.Body, line) { |
| + t.Errorf("still says to use email after the link began:\n%s", page.Body) |
| + } |
| +} |