oficina / sheets Public

Sheets: Connect your Oficina account only when it isn't, and finishes itself

The account menu offers Connect only when no Oficina link is stored. A
sign-in through id with an unlinked identity now leads, after one local
sign-in, straight to the confirm step; the card's buttons sit in one
row and a stored link shows Accounts connected. Pins estilo f14458a and
home connect ff16bf5.  Reviewed by GPT-6-Astra via Codex (#48).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from fix/connect-item-when-linked into main.
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 530ca67791e58c2690244925570178e445f84bb8 parent 2586c65
6 files changed, +267 −18
  • go.mod +3 −3
  • go.sum +6 −6
  • internal/sheets/chrome.go +20 −7
  • internal/sheets/home.go +15 −1
  • internal/sheets/home_connect_item_test.go +222 −0
  • internal/sheets/home_test.go +1 −1

CI passed — run details

landed from fix/connect-item-when-linked

diff --git a/go.mod b/go.mod
index 7603c81..707f028 100644
--- a/go.mod
+++ b/go.mod
@@ -3,9 +3,9 @@ module amadan.net/oficina/sheets
go 1.25.0
require (
- amadan.net/oficina/estilo.git v0.0.0-20261024225350-d53b8e9533a1
- amadan.net/oficina/home/connect v0.0.0-20261001093757-2bec942ddd0c
- amadan.net/oficina/home/handoff v0.0.0-20260908102137-481d2d57f489
+ amadan.net/oficina/estilo.git v0.0.0-20261024233714-f14458a4683c
+ amadan.net/oficina/home/connect v0.0.0-20261024233715-ff16bf56bc4d
+ amadan.net/oficina/home/handoff v0.0.0-20261024233715-ff16bf56bc4d
amadan.net/rastrillo/rastrillo v0.27.1-0.20261006204347-fb806a287060
github.com/carlosframework/rastrillo v0.23.0
github.com/chromedp/cdproto v0.0.0-20250724212937-08a3db8b4327
diff --git a/go.sum b/go.sum
index 659544f..a703876 100644
--- a/go.sum
+++ b/go.sum
@@ -1,9 +1,9 @@
-amadan.net/oficina/estilo.git v0.0.0-20261024225350-d53b8e9533a1 h1:Xaidrm9rzFyne6PujFYXiY7nGRy+/KzH7kGBM2+D5LQ=
-amadan.net/oficina/estilo.git v0.0.0-20261024225350-d53b8e9533a1/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/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/oficina/estilo.git v0.0.0-20261024233714-f14458a4683c h1:jPbFJwPi7lHe+UNe+Gbj0ivVsy+pLqMSq2EP2Xff/q0=
+amadan.net/oficina/estilo.git v0.0.0-20261024233714-f14458a4683c/go.mod h1:4u0jNBtIdMLOsJyrScfqoEuYDJF7BBM3qM9YWbzt6aw=
+amadan.net/oficina/home/connect v0.0.0-20261024233715-ff16bf56bc4d h1:9sVZBph2xUuejZt73DYaoCfoyvrvZCnuPHcyMsg562E=
+amadan.net/oficina/home/connect v0.0.0-20261024233715-ff16bf56bc4d/go.mod h1:ix81eFV87yE9f2SXiRuxYCVM4lAgTRC5YoQMiH3+TDk=
+amadan.net/oficina/home/handoff v0.0.0-20261024233715-ff16bf56bc4d h1:8RTZkLQnyxvH2g0RMovhN/m2LN9D8qb+kBfqdBeqq+s=
+amadan.net/oficina/home/handoff v0.0.0-20261024233715-ff16bf56bc4d/go.mod h1:YISpzFJ+iMgrM0rUL032OSzsAQbP4/nozvXLCCOcn1k=
amadan.net/rastrillo/native/server v0.0.0-20260923173546-2a5d07b2c8d3 h1:5EFx7BLJj/+G/rt8bTmeyMWVfF5s5Sns7ajTvX4w/I8=
amadan.net/rastrillo/native/server v0.0.0-20260923173546-2a5d07b2c8d3/go.mod h1:ZaaxeE278dJltdrLwmwpxUjQ5wuKzqDAVSNRet+WupY=
amadan.net/rastrillo/rastrillo v0.27.1-0.20261006204347-fb806a287060 h1:K2PXcCKfm+AYrqgH1ea6cNsFs4sN7MKLvq+uBY11NZE=
diff --git a/internal/sheets/chrome.go b/internal/sheets/chrome.go
index 7018e94..50a4519 100644
--- a/internal/sheets/chrome.go
+++ b/internal/sheets/chrome.go
@@ -203,23 +203,33 @@ func (a *App) account(r *http.Request, address, ret string) *chrome.Account {
if address != "" {
acc.TriggerLabel = "Account: " + address
}
- if a.offersHomeLink(r) {
+ if a.offersHomeLink(r, address) {
acc.Items = []estilomenu.Item{{Href: "/account/home", Label: homeCopy["menu_action"]}}
}
return acc
}
// offersHomeLink is whether the account menu offers to connect the
-// person's Oficina account: only when Home is configured, and never to
-// somebody who signed in through Home, whose account is already the
-// connection (spec §12.1 item 4; before this Sheets offered it to Home
-// sessions too).
-func (a *App) offersHomeLink(r *http.Request) bool {
+// person's Oficina account: only when Home is configured, to somebody
+// signed in locally whose member has no stored link yet. The stored link
+// decides, not how this session signed in: a linked person who used the
+// email link is already connected (spec §12.1 item 4). One lookup on
+// home_identity_links' unique (issuer, person_id) index per render; an
+// error means unknown, so nothing is offered.
+func (a *App) offersHomeLink(r *http.Request, address string) bool {
if a.home == nil {
return false
}
s, ok := a.homeSessions.From(r)
- return ok && s.Method != connect.HomeMethod
+ if !ok || s.Method == connect.HomeMethod {
+ return false
+ }
+ id, err := a.localMemberID(r.Context(), address)
+ if err != nil {
+ return false
+ }
+ linked, err := a.home.Linked(r.Context(), id)
+ return err == nil && !linked
}
// switcher is the app switcher, or nil when no hub directory is
@@ -248,6 +258,9 @@ var homeCopy = func() map[string]string {
}
out["start_body"] = "Use your Oficina account to sign in to this account. Your sheets and sharing settings stay as they are."
out["documents_action"] = "Back to Sheets"
+ out["confirm_title"] = "Connect these two accounts?"
+ out["confirm_body"] = "Your Oficina account is confirmed. Connect it to the Sheets account below, and you can sign in to Sheets with Oficina from now on."
+ out["unknown_body"] = "Sign in to your Sheets account once to connect it to your Oficina account."
return out
}()
diff --git a/internal/sheets/home.go b/internal/sheets/home.go
index e744ad8..edff9ce 100644
--- a/internal/sheets/home.go
+++ b/internal/sheets/home.go
@@ -55,7 +55,7 @@ func (a *App) ConfigureHome(raw *connect.Credentials) error {
a.renderSignedOut(w, r, status)
return
}
- p := connect.Page(state, label, func(k string) string { return homeCopy[k] })
+ p := connect.PageFor(r, state, label, func(k string) string { return homeCopy[k] })
a.render(w, r, "home_link", titled(p.Title), status, p)
}
bridge, err := connect.New(connect.Config{DB: d, Origin: a.origin, Issuer: settings.Issuer, ConsumerID: settings.ConsumerID, Namespace: "oficina/sheets", Keys: settings.Keys, DEK: settings.DEK, SessionCookie: sess.CookieName(), ReadSession: read,
@@ -110,6 +110,20 @@ func (a *App) homePerson(ctx context.Context, q linkstore.Queryer, id int64) (st
return address, nil
}
+// localMemberID is the member a local session's address names, read
+// without creating one: the person ID Home links are stored against.
+func (a *App) localMemberID(ctx context.Context, address string) (int64, error) {
+ if a.db == nil {
+ return 0, errNoDB
+ }
+ var id int64
+ address = strings.ToLower(strings.TrimSpace(address))
+ if err := a.db.WithContext(ctx).Raw("SELECT id FROM members WHERE address=?", address).Row().Scan(&id); err != nil {
+ return 0, err
+ }
+ return id, nil
+}
+
func (a *App) homeRequire(next http.Handler) http.Handler {
return a.homeSessions.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, ok := a.homeSessions.From(r)
diff --git a/internal/sheets/home_connect_item_test.go b/internal/sheets/home_connect_item_test.go
new file mode 100644
index 0000000..83d89bd
--- /dev/null
+++ b/internal/sheets/home_connect_item_test.go
@@ -0,0 +1,222 @@
+package sheets
+
+import (
+ "html"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "testing"
+
+ "amadan.net/oficina/home/connect"
+)
+
+// 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>`
+
+// offersConnect renders the list and reports whether its account menu
+// offers the connection. It fails unless the page is a signed-in one, so
+// a missing item can never be a missing menu.
+func offersConnect(t *testing.T, f *homeFixture) bool {
+ t.Helper()
+ page := f.request(t, "GET", "/sheets", nil, false)
+ body := page.Body.String()
+ if page.Code != http.StatusOK || !strings.Contains(body, "<details estilo-account") {
+ t.Fatalf("/sheets is not a signed-in page: %d", page.Code)
+ }
+ return strings.Contains(body, connectItem)
+}
+
+// linkRows counts the stored Home links for one member.
+func linkRows(t *testing.T, f *homeFixture, member Member) int64 {
+ t.Helper()
+ var n int64
+ if err := f.app.db.Table("home_identity_links").Where("person_id = ?", member.ID).Count(&n).Error; err != nil {
+ t.Fatal(err)
+ }
+ return n
+}
+
+// emailSignIn is a real local sign-in through the emailed link, in this
+// fixture's browser: request the link, open it, press Sign in. It returns
+// the response to the local completion GET, which decides the landing.
+func emailSignIn(t *testing.T, f *homeFixture, address string) *httptest.ResponseRecorder {
+ t.Helper()
+ if w := f.request(t, "POST", "/signin", url.Values{"address": {address}, "force": {"1"}}, false); w.Code != http.StatusSeeOther {
+ t.Fatal("request link", w.Code)
+ }
+ u, err := url.Parse(f.app.testMailer(t).lastLink(t))
+ if err != nil {
+ t.Fatal(err)
+ }
+ open := f.request(t, "GET", u.RequestURI(), nil, false)
+ m := confirmForm.FindStringSubmatch(open.Body.String())
+ if open.Code != http.StatusOK || m == nil {
+ t.Fatalf("open link: %d, want the confirm door", open.Code)
+ }
+ verify := f.request(t, "POST", html.UnescapeString(m[1]), url.Values{"token": {html.UnescapeString(m[2])}}, false)
+ if verify.Code != http.StatusSeeOther || verify.Header().Get("Location") != connect.LocalCompletePath {
+ t.Fatal("local callback", verify.Code, verify.Header().Get("Location"))
+ }
+ return f.request(t, "GET", connect.LocalCompletePath, nil, false)
+}
+
+// The menu item follows the stored link, not how this session signed in.
+// Paul hit the bug in Docs: linked, signed in with the email link, and
+// still offered "Connect your Oficina account".
+func TestConnectItemFollowsTheStoredLink(t *testing.T) {
+ f := newHomeFixture(t)
+ member := f.local(t, signedInAddress)
+ if !offersConnect(t, f) {
+ t.Fatal("an unlinked local session is not offered the connection")
+ }
+ f.link(t, "linked-subject")
+ if linkRows(t, f, member) != 1 {
+ t.Fatal("the link flow stored no link")
+ }
+ if offersConnect(t, f) {
+ t.Error("the same session is still offered the connection after linking")
+ }
+ // A later sign-in with the app's own email link, by the linked person.
+ f.jar = map[string]*http.Cookie{}
+ f.local(t, signedInAddress)
+ if offersConnect(t, f) {
+ t.Error("a linked person signed in by email link is offered the connection")
+ }
+ g := newHomeFixture(t)
+ homeSignedIn(t, g)
+ if offersConnect(t, g) {
+ t.Error("a Home session is offered the connection")
+ }
+}
+
+// After the confirm POST, in the same session: the answer leads to
+// "Accounts connected", the link row exists, and the next render's menu
+// no longer offers the item. The confirm page itself is Sheets' copy and
+// its two buttons share one form-foot row, primary first.
+func TestConfirmLeadsToAccountsConnected(t *testing.T) {
+ f := newHomeFixture(t)
+ member := f.local(t, signedInAddress)
+ start := f.request(t, "POST", "/account/home", nil, false)
+ if start.Code != http.StatusSeeOther {
+ t.Fatal("link start", start.Code)
+ }
+ authStart := f.request(t, "GET", start.Header().Get("Location"), nil, false)
+ if finish := f.exchange(t, authStart, "confirm-subject"); finish.Header().Get("Location") != "/account/home/confirm" {
+ t.Fatal("finish link", finish.Code, finish.Header().Get("Location"))
+ }
+ confirm := f.request(t, "GET", "/account/home/confirm", nil, false)
+ body := confirm.Body.String()
+ if confirm.Code != http.StatusOK || !strings.Contains(body, "<h1>Connect these two accounts?</h1>") ||
+ !strings.Contains(body, "Connect it to the Sheets account below, and you can sign in to Sheets with Oficina from now on.") {
+ t.Fatalf("confirm page: %d, want Sheets' confirm copy\n%s", confirm.Code, body)
+ }
+ if strings.Contains(body, "Home account") {
+ t.Error("the confirm page names the hub")
+ }
+ row := strings.Index(body, "<div rst-form-foot>")
+ if row < 0 {
+ t.Fatal("the confirm page's buttons are not in one rst-form-foot row")
+ }
+ end := row + strings.Index(body[row:], "</div>")
+ primary := strings.Index(body[row:end], `<button rst-btn="primary" type="submit">Connect accounts</button>`)
+ ghost := strings.Index(body[row:end], `<a rst-btn="ghost" href="/">`)
+ if primary < 0 || ghost < 0 || primary > ghost {
+ t.Fatalf("form-foot row is not primary then ghost: %q", body[row:end])
+ }
+ if linkRows(t, f, member) != 0 {
+ t.Fatal("a link was stored before the confirm click")
+ }
+ posted := f.request(t, "POST", "/account/home/confirm", nil, false)
+ if posted.Code != http.StatusSeeOther || !strings.HasPrefix(posted.Header().Get("Location"), "/account/home/confirm?connected=1") {
+ t.Fatal("confirm POST", posted.Code, posted.Header().Get("Location"))
+ }
+ done := f.request(t, "GET", posted.Header().Get("Location"), nil, false)
+ if done.Code != http.StatusOK || !strings.Contains(done.Body.String(), "<h1>Accounts connected</h1>") {
+ t.Fatalf("after confirm: %d, want Accounts connected\n%s", done.Code, done.Body.String())
+ }
+ if linkRows(t, f, member) != 1 {
+ t.Fatal("the confirm POST stored no link")
+ }
+ if offersConnect(t, f) {
+ t.Error("the menu still offers the connection right after confirming")
+ }
+}
+
+// An Oficina arrival with no link shows the unknown page; a local sign-in
+// in the same browser then begins the link itself, through a fresh Home
+// assertion to the confirm page, and the confirm click stores the link.
+// "Accounts connected" leads back to where sign-in began (PageFor).
+func TestUnknownArrivalResumesTheLinkAfterLocalSignIn(t *testing.T) {
+ f := newHomeFixture(t)
+ signin := f.request(t, "GET", "/signin?return_to=%2Fsheets", nil, false)
+ unknown := f.exchange(t, f.request(t, "GET", signin.Header().Get("Location"), nil, false), "unknown-subject")
+ body := unknown.Body.String()
+ if unknown.Code != http.StatusForbidden || !strings.Contains(body, `href="/signin/local"`) ||
+ !strings.Contains(body, "Sign in to your Sheets account once to connect it to your Oficina account.") {
+ t.Fatalf("unknown arrival: %d, want the unknown page\n%s", unknown.Code, body)
+ }
+ completed := emailSignIn(t, f, signedInAddress)
+ if completed.Code != http.StatusSeeOther || completed.Header().Get("Location") != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" {
+ t.Fatal("local sign-in after unknown arrival", completed.Code, completed.Header().Get("Location"))
+ }
+ if _, ok := f.jar["__Host-oficina-home-init"]; !ok {
+ t.Fatal("the local sign-in began no link")
+ }
+ authStart := f.request(t, "GET", completed.Header().Get("Location"), nil, false)
+ if finish := f.exchange(t, authStart, "unknown-subject"); finish.Code != http.StatusSeeOther || finish.Header().Get("Location") != "/account/home/confirm" {
+ t.Fatal("fresh assertion", finish.Code, finish.Header().Get("Location"))
+ }
+ member, ok := f.app.memberByID(1)
+ if !ok || member.Address != signedInAddress {
+ t.Fatal("local member missing")
+ }
+ if page := f.request(t, "GET", "/account/home/confirm", nil, false); page.Code != http.StatusOK || !strings.Contains(page.Body.String(), "Connect these two accounts?") {
+ t.Fatal("confirm page", page.Code)
+ }
+ if linkRows(t, f, member) != 0 {
+ t.Fatal("a link was stored before the confirm click")
+ }
+ posted := f.request(t, "POST", "/account/home/confirm", nil, false)
+ if posted.Code != http.StatusSeeOther {
+ t.Fatal("confirm POST", posted.Code)
+ }
+ if linkRows(t, f, member) != 1 {
+ t.Fatal("the confirm POST stored no link")
+ }
+ done := f.request(t, "GET", posted.Header().Get("Location"), nil, false)
+ if done.Code != http.StatusOK || !strings.Contains(done.Body.String(), "<h1>Accounts connected</h1>") ||
+ !strings.Contains(done.Body.String(), `<a rst-btn="primary" href="/sheets">Back to Sheets</a>`) {
+ t.Fatalf("after confirm: %d, want Accounts connected leading back to /sheets\n%s", done.Code, done.Body.String())
+ }
+ if offersConnect(t, f) {
+ t.Error("the menu still offers the connection after the resumed link")
+ }
+}
+
+// Without the remembered arrival the local sign-in lands as before. The
+// marker lives ten minutes in the browser (its Max-Age), so a browser
+// that has dropped it is the app-level expiry; the sealed in-marker
+// expiry is checked upstream (home/connect TestUnknownMarkerExpiresToTheMenuPath),
+// since this fixture cannot move connect's navigation clock.
+func TestUnknownArrivalForgottenLandsAsBefore(t *testing.T) {
+ f := newHomeFixture(t)
+ signin := f.request(t, "GET", "/signin?return_to=%2Fsheets", nil, false)
+ unknown := f.exchange(t, f.request(t, "GET", signin.Header().Get("Location"), nil, false), "unknown-subject")
+ if unknown.Code != http.StatusForbidden {
+ t.Fatal("unknown arrival", unknown.Code)
+ }
+ const marker = "__Host-oficina-local-link-navigation"
+ c, ok := f.jar[marker]
+ if !ok || c.MaxAge != 600 {
+ t.Fatal("the unknown arrival left no ten-minute marker", ok)
+ }
+ delete(f.jar, marker)
+ completed := emailSignIn(t, f, signedInAddress)
+ if completed.Code != http.StatusSeeOther || completed.Header().Get("Location") != "/sheets" {
+ t.Fatal("local sign-in without the marker", completed.Code, completed.Header().Get("Location"))
+ }
+ if !offersConnect(t, f) {
+ t.Error("the unlinked person is not offered the connection")
+ }
+}
diff --git a/internal/sheets/home_test.go b/internal/sheets/home_test.go
index 4803564..43b0be5 100644
--- a/internal/sheets/home_test.go
+++ b/internal/sheets/home_test.go
@@ -140,7 +140,7 @@ func (f *homeFixture) link(t *testing.T, subject string) {
t.Fatal("finish link", finish.Code, finish.Body.String())
}
page := f.request(t, "GET", "/account/home/confirm", nil, false)
- if page.Code != 200 || !strings.Contains(page.Body.String(), "Confirm account connection") {
+ if page.Code != 200 || !strings.Contains(page.Body.String(), "Connect these two accounts?") {
t.Fatal("confirm page", page.Code, page.Body.String())
}
confirmed := f.request(t, "POST", "/account/home/confirm", nil, false)