oficina / meet Public

Calls: offer "Connect your Oficina account" only while no link is stored

The account menu offered the item to any member whose session came from
an emailed link or Keymail, so an account already linked to Oficina, but
signed in with the app's own link this time, was still asked to connect
(Paul hit this). offersHomeLink now also asks the bridge whether the
person has a stored link (one lookup on the UNIQUE (issuer, person_id)
index) and offers nothing on an error.

Bumps home/connect to 08bbcf3 (fix/connect-item-when-linked), which
brings estilo 0b375d5: an Oficina sign-in with no link here now resumes
as a link after the next fresh local sign-in, Connect accounts lands on
"Accounts connected", the confirm step has its own title and copy, and
the connect pages' buttons share one rst-form-foot row, primary first.
The Home page renders with connect.PageFor, so "Back to the app" on the
success page returns to the page the link began from.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 0a5ef4dac5618fca48ceb03735037fdac722d349 parent 39eb304
5 files changed, +229 −12
  • go.mod +2 −2
  • go.sum +4 −4
  • internal/calls/chrome.go +12 −5
  • internal/calls/home.go +1 −1
  • internal/calls/home_connect_item_test.go +210 −0

CI failed — run details

diff --git a/go.mod b/go.mod
index d260432..c4f2d4f 100644
--- a/go.mod
+++ b/go.mod
@@ -3,8 +3,8 @@ module amadan.net/oficina/calls
go 1.26.0
require (
- amadan.net/oficina/estilo.git v0.0.0-20261001093750-530cb932e768
- amadan.net/oficina/home/connect v0.0.0-20261001093757-2bec942ddd0c
+ amadan.net/oficina/estilo.git v0.0.0-20261024215431-0b375d59ca32
+ amadan.net/oficina/home/connect v0.0.0-20261024220045-08bbcf31e483
amadan.net/oficina/home/handoff v0.0.0-20260908102137-481d2d57f489
amadan.net/rastrillo/rastrillo v0.27.1-0.20261006204347-fb806a287060
github.com/go-chi/chi/v5 v5.3.2
diff --git a/go.sum b/go.sum
index ed9096b..3fe1e3c 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,7 @@
-amadan.net/oficina/estilo.git v0.0.0-20261001093750-530cb932e768 h1:bVscoa9fH5LdLDjlyecZhrX7JzWPT0blb57MH0FIm2U=
-amadan.net/oficina/estilo.git v0.0.0-20261001093750-530cb932e768/go.mod h1:4u0jNBtIdMLOsJyrScfqoEuYDJF7BBM3qM9YWbzt6aw=
-amadan.net/oficina/home/connect v0.0.0-20261001093757-2bec942ddd0c h1:3yxO2D8BxDLMGfFV2o6Sp9Z91yLnjDCIuRKHVWORa8A=
-amadan.net/oficina/home/connect v0.0.0-20261001093757-2bec942ddd0c/go.mod h1:YroavXe43Wietec+po8mtSk4a9Bir2h31Q3iAf4MWxI=
+amadan.net/oficina/estilo.git v0.0.0-20261024215431-0b375d59ca32 h1:YJhvKpW6eTejjvXRbb+CHrgmF6Pms7N2q2/73dbm3iA=
+amadan.net/oficina/estilo.git v0.0.0-20261024215431-0b375d59ca32/go.mod h1:4u0jNBtIdMLOsJyrScfqoEuYDJF7BBM3qM9YWbzt6aw=
+amadan.net/oficina/home/connect v0.0.0-20261024220045-08bbcf31e483 h1:AX1ATw/weILU5pjHHo1XD01gz9EJAp/9xq3J4OftR3E=
+amadan.net/oficina/home/connect v0.0.0-20261024220045-08bbcf31e483/go.mod h1:MWe3bFg0xGr1btEom2snMLhEZeRIfw0Iv6vKUyOsu18=
amadan.net/oficina/home/handoff v0.0.0-20260908102137-481d2d57f489 h1:RPKtyry/71i06o8L+gjAaUyexbmG56kO1YPs601URAM=
amadan.net/oficina/home/handoff v0.0.0-20260908102137-481d2d57f489/go.mod h1:YISpzFJ+iMgrM0rUL032OSzsAQbP4/nozvXLCCOcn1k=
amadan.net/rastrillo/rastrillo v0.27.1-0.20261006204347-fb806a287060 h1:K2PXcCKfm+AYrqgH1ea6cNsFs4sN7MKLvq+uBY11NZE=
diff --git a/internal/calls/chrome.go b/internal/calls/chrome.go
index 5f1169f..d2a5f89 100644
--- a/internal/calls/chrome.go
+++ b/internal/calls/chrome.go
@@ -212,7 +212,7 @@ func (a *app) account(r *http.Request, v viewer) *chrome.Account {
Help: []estilomenu.Item{{Href: "/help/open-source", Label: "Open source"}},
SignOut: chrome.SignOut{Action: "/signout", Label: "Sign out"},
}
- if a.offersHomeLink(v) {
+ if a.offersHomeLink(r, v) {
acc.Items = []estilomenu.Item{{Href: "/account/home", Label: "Connect your Oficina account"}}
}
return acc
@@ -220,10 +220,17 @@ func (a *app) account(r *http.Request, v viewer) *chrome.Account {
// offersHomeLink is whether the account menu offers to connect the
// person's Oficina account: only with Home configured, only to a member
-// (the page is behind requireMember), and never to somebody who signed
-// in through Home, whose account already is the connection.
-func (a *app) offersHomeLink(v viewer) bool {
- return a.home != nil && v.signedIn() && a.isMember(v.person) && (v.method == "magiclink" || v.method == "keymail")
+// (the page is behind requireMember), never to somebody who signed in
+// through Home, whose account already is the connection, and never once
+// the account has a stored link, however this session signed in (an
+// emailed link still signs in a linked account). The link lookup is one
+// query on the UNIQUE (issuer, person_id) index; an error offers nothing.
+func (a *app) offersHomeLink(r *http.Request, v viewer) bool {
+ if a.home == nil || !v.signedIn() || !a.isMember(v.person) || (v.method != "magiclink" && v.method != "keymail") {
+ return false
+ }
+ linked, err := a.home.bridge.Linked(r.Context(), v.person.ID)
+ return err == nil && !linked
}
// switcher is the app switcher, or nil when no hub directory is
diff --git a/internal/calls/home.go b/internal/calls/home.go
index d158a4f..647b4d5 100644
--- a/internal/calls/home.go
+++ b/internal/calls/home.go
@@ -161,7 +161,7 @@ func (a *app) endRemovedSession(w http.ResponseWriter, r *http.Request) {
}
func (a *app) renderHome(w http.ResponseWriter, r *http.Request, state, label string, status int) {
- home := connect.Page(state, label)
+ home := connect.PageFor(r, state, label)
if state == "signedout" {
// Where Sign out lands: the Oficina door, in the door's words
// (connect's name the hub); its Sign in goes to connect's action.
diff --git a/internal/calls/home_connect_item_test.go b/internal/calls/home_connect_item_test.go
new file mode 100644
index 0000000..93d68fd
--- /dev/null
+++ b/internal/calls/home_connect_item_test.go
@@ -0,0 +1,210 @@
+package calls
+
+import (
+ "bytes"
+ "encoding/base64"
+ "encoding/json"
+ "net/http"
+ "regexp"
+ "strings"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/home/connect"
+ "amadan.net/oficina/home/handoff/seal"
+)
+
+// connectItem is the account menu's "Connect your Oficina account".
+const connectItem = `<a estilo-menu-item href="/account/home"><span estilo-menu-label>Connect your Oficina account</span></a>`
+
+// localComplete signs alice in with an emailed link in this browser and
+// returns where GET /auth/local/complete sends her, without following it.
+func (f *homeRouterFixture) localComplete(t *testing.T) string {
+ t.Helper()
+ homeRedirect(t, f.do("POST", "/signin", "address=alice%40example.test&force=1"))
+ link := regexp.MustCompile(`https://[^\s<>\"]+/auth/verify\?[^\s<>\"]+`).FindString(f.mailBody())
+ if link == "" {
+ t.Fatal("no captured local verification link")
+ }
+ if landing := homeRedirect(t, f.followLink(t, link)); landing != connect.LocalCompletePath {
+ t.Fatal("wrong local landing", landing)
+ }
+ return homeRedirect(t, f.do("GET", connect.LocalCompletePath, ""))
+}
+
+// existingAccount gives alice the account an unknown Oficina sign-in asks
+// her to sign in to: one earlier emailed-link sign-in (which creates her
+// persons row on /continue), then sign out. Without the row the resume
+// cannot bind, and connect falls back to the Connect page.
+func (f *homeRouterFixture) existingAccount(t *testing.T) {
+ t.Helper()
+ f.local(t)
+ homeSignout(t, f.do("POST", "/signout", ""))
+}
+
+// linkedRows is how many stored links alice's persons row has.
+func (f *homeRouterFixture) linkedRows(t *testing.T) int {
+ t.Helper()
+ var n int
+ if err := f.app.db.Writer().QueryRow("SELECT count(*) FROM home_identity_links l JOIN persons p ON p.id = l.person_id WHERE p.email = 'alice@example.test'").Scan(&n); err != nil {
+ t.Fatal(err)
+ }
+ return n
+}
+
+// confirmAndLand presses Connect accounts on the confirm page and follows
+// the answer to the page it leads to, which must be "Accounts connected"
+// whose one button goes back to want.
+func (f *homeRouterFixture) confirmAndLand(t *testing.T, want string) {
+ t.Helper()
+ next := homeRedirect(t, f.do("POST", "/account/home/confirm", ""))
+ if !strings.HasPrefix(next, "/account/home/confirm?") {
+ t.Fatalf("the confirm POST leads to %q, not the connected page", next)
+ }
+ w := f.do("GET", next, "")
+ body := w.Body.String()
+ if w.Code != http.StatusOK || !strings.Contains(body, "<h1>Accounts connected</h1>") {
+ t.Fatalf("after the confirm POST: %d, want Accounts connected:\n%s", w.Code, body)
+ }
+ if button := `<a rst-btn="primary" href="` + want + `">Back to the app</a>`; !strings.Contains(body, button) {
+ t.Fatalf("Accounts connected does not lead back with %q:\n%s", button, body)
+ }
+}
+
+// Paul's bug: a linked account signed in with the app's own emailed link
+// was still offered "Connect your Oficina account". The offer follows the
+// stored link, not how this session signed in.
+func TestTheConnectItemFollowsTheStoredLinkNotTheSessionMethod(t *testing.T) {
+ f := newHomeRouterFixture(t)
+ f.local(t)
+ if body := f.do("GET", "/", "").Body.String(); !strings.Contains(body, connectItem) {
+ t.Fatalf("an unlinked local session is not offered to connect:\n%s", body)
+ }
+ f.link(t) // ends signed in through Home
+ if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, "/account/home") {
+ t.Fatal("a Home session is offered to connect the account it already is")
+ }
+ if f.linkedRows(t) != 1 {
+ t.Fatal("the link flow stored no link")
+ }
+ homeSignout(t, f.do("POST", "/signout", ""))
+ f.local(t)
+ if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, connectItem) {
+ t.Fatal("a linked account signed in by emailed link is still offered to connect")
+ }
+}
+
+// Right after Connect accounts, in the same (emailed-link) session: the
+// answer is "Accounts connected", the link row exists, and the next page's
+// menu no longer offers to connect.
+func TestConfirmingTheLinkSaysSoAndRetiresTheMenuItem(t *testing.T) {
+ f := newHomeRouterFixture(t)
+ f.local(t)
+ if w := f.do("GET", "/account/home", ""); w.Code != 200 {
+ t.Fatal("account", w.Code)
+ }
+ start := homeRedirect(t, f.do("POST", "/account/home", ""))
+ if next := homeRedirect(t, f.exchange(t, start, "opaque-home-alice")); next != "/account/home/confirm" {
+ t.Fatal("confirmation", next)
+ }
+ w := f.do("GET", "/account/home/confirm", "")
+ if w.Code != 200 || !strings.Contains(w.Body.String(), "<h1>Connect these two accounts?</h1>") || !strings.Contains(w.Body.String(), "alice@example.test") {
+ t.Fatalf("confirm page: %d\n%s", w.Code, w.Body.String())
+ }
+ f.confirmAndLand(t, "/")
+ if f.linkedRows(t) != 1 {
+ t.Fatal("Connect accounts stored no link")
+ }
+ if body := f.do("GET", "/", "").Body.String(); strings.Contains(body, connectItem) {
+ t.Fatal("the menu still offers to connect right after connecting")
+ }
+}
+
+// An Oficina sign-in with no link here shows the unknown page, whose one
+// button is the local sign-in. Signing in locally in the same browser then
+// begins the link itself: the Home freshness start, a fresh assertion, the
+// confirm page, and Connect accounts stores the link, landing back on the
+// page the sign-in was for.
+func TestAnUnknownOficinaSigninResumesAsALinkAfterLocalSignin(t *testing.T) {
+ f := newHomeRouterFixture(t)
+ f.existingAccount(t)
+ start := homeRedirect(t, f.do("GET", "/signin?return_to=%2Fsettings", ""))
+ w := f.exchange(t, start, "opaque-home-alice")
+ if w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), `<a rst-btn="primary" href="/signin/local">Sign in to your existing account</a>`) {
+ t.Fatalf("unknown assertion: %d, want the unknown page:\n%s", w.Code, w.Body.String())
+ }
+ resume := f.localComplete(t)
+ if resume != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" {
+ t.Fatalf("local sign-in after an unknown assertion lands on %q, not the Home freshness start", resume)
+ }
+ if _, ok := f.cookies["__Host-oficina-home-init"]; !ok {
+ t.Fatal("no link initiation cookie")
+ }
+ if next := homeRedirect(t, f.exchange(t, resume, "opaque-home-alice")); next != "/account/home/confirm" {
+ t.Fatal("after the fresh assertion", next)
+ }
+ if w := f.do("GET", "/account/home/confirm", ""); w.Code != 200 || !strings.Contains(w.Body.String(), "<h1>Connect these two accounts?</h1>") {
+ t.Fatalf("confirm page: %d\n%s", w.Code, w.Body.String())
+ }
+ if f.linkedRows(t) != 0 {
+ t.Fatal("linked before the confirm click")
+ }
+ f.confirmAndLand(t, "/settings")
+ if f.linkedRows(t) != 1 {
+ t.Fatal("Connect accounts stored no link")
+ }
+ homeSignout(t, f.do("POST", "/signout", ""))
+ start = homeRedirect(t, f.do("GET", "/signin", ""))
+ if w := f.exchange(t, start, "opaque-home-alice"); w.Code != 303 {
+ t.Fatalf("Oficina sign-in after linking: %d\n%s", w.Code, w.Body.String())
+ }
+}
+
+// homeMarker seals connect's local-link navigation marker as connect
+// does, for the fixture's Home DEK, issued at issued.
+func homeMarker(t *testing.T, action string, issued time.Time) *http.Cookie {
+ t.Helper()
+ s, err := seal.New(bytes.Repeat([]byte{7}, 32), "oficina/calls")
+ if err != nil {
+ t.Fatal(err)
+ }
+ payload, _ := json.Marshal(struct {
+ Version int `json:"v"`
+ Action string `json:"action"`
+ Issued int64 `json:"issued"`
+ Expires int64 `json:"expires"`
+ }{1, action, issued.Unix(), issued.Unix() + 600})
+ sealed, err := s.Seal("oficina/calls.local-link-navigation.v1:"+homeTestOrigin, payload)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return &http.Cookie{Name: "__Host-oficina-local-link-navigation", Value: base64.RawURLEncoding.EncodeToString(sealed)}
+}
+
+// An expired unknown-assertion marker (10 minutes) is ignored: the local
+// sign-in lands on /continue as any other. The same marker, current, does
+// resume, which shows the forged marker is the one connect writes.
+func TestAnExpiredUnknownMarkerLandsAsBefore(t *testing.T) {
+ f := newHomeRouterFixture(t)
+ f.existingAccount(t)
+ f.cookies["__Host-oficina-local-link-navigation"] = homeMarker(t, "link-resume", time.Now().Add(-11*time.Minute))
+ if got := f.localComplete(t); got != "/continue" {
+ t.Fatalf("expired marker: local sign-in lands on %q, want /continue", got)
+ }
+ homeSignout(t, f.do("POST", "/signout", ""))
+ f.cookies["__Host-oficina-local-link-navigation"] = homeMarker(t, "link-resume", time.Now().Add(-time.Minute))
+ if got := f.localComplete(t); got != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" {
+ t.Fatalf("current marker: local sign-in lands on %q, want the Home freshness start", got)
+ }
+}
+
+// The connect pages' two buttons share one form-foot row, primary first.
+func TestTheConnectPagesButtonsShareOneRowPrimaryFirst(t *testing.T) {
+ f := newHomeRouterFixture(t)
+ f.local(t)
+ body := f.do("GET", "/account/home", "").Body.String()
+ want := `<div rst-form-foot><form method="post" action="/account/home"><button rst-btn="primary" type="submit">Connect</button></form><a rst-btn="ghost" href="/">Back</a></div>`
+ if !strings.Contains(body, want) {
+ t.Fatalf("the connect page's buttons are not one row, primary first; want %s in:\n%s", want, body)
+ }
+}