oficina / slides Public

Connect item follows the stored link, not the session method

The account menu offered "Connect your Oficina account" to anybody whose
session was not a Home session, so a person already linked who signed in
with the emailed link still saw it. offersHomeLink now asks connect's
Bridge.Linked for the viewer's person ID (one lookup on the unique
(issuer, person_id) index per menu render) and offers the item only on
(false, nil); an error offers nothing.

Bumps home/connect to 08bbcf3 (home fix/connect-item-when-linked), which
brings estilo 0b375d5: the unknown-assertion flow (a local sign-in after
an unlinked Oficina arrival resumes the link through a fresh assertion
and the explicit confirm), the "Accounts connected" page after the
confirm POST, and the connect page's buttons in one rst-form-foot row.
The Home pages render through connect.PageFor so the success page leads
back to where the link began. The confirm and unknown copy say what
those steps now do, naming Slides.

Tests (home_menu_test.go) run the real router and each was seen to fail
without its fix.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 0d5f359f90520735904fb747dc8498efdea6e669 parent 16b762d
6 files changed, +338 −17
  • go.mod +2 −2
  • go.sum +4 −4
  • internal/slides/chrome.go +14 −7
  • internal/slides/home.go +1 −1
  • internal/slides/home_menu_test.go +314 −0
  • internal/slides/locales/en.toml +3 −3

CI passed — run details

diff --git a/go.mod b/go.mod
index 67f5bab..8503a68 100644
--- a/go.mod
+++ b/go.mod
@@ -5,8 +5,8 @@ go 1.25.0
toolchain go1.26.5
require (
- amadan.net/oficina/estilo.git v0.0.0-20261001084202-0b4d92c3dc16
- 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/chromedp/cdproto v0.0.0-20250724212937-08a3db8b4327
diff --git a/go.sum b/go.sum
index 5373acd..38b51d8 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,7 @@
-amadan.net/oficina/estilo.git v0.0.0-20261001084202-0b4d92c3dc16 h1:F5v3x25dEj1rJNnBm71G7shN+fM+T2actUlaGPvoeH8=
-amadan.net/oficina/estilo.git v0.0.0-20261001084202-0b4d92c3dc16/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/slides/chrome.go b/internal/slides/chrome.go
index e7cc6fa..2568c02 100644
--- a/internal/slides/chrome.go
+++ b/internal/slides/chrome.go
@@ -227,19 +227,26 @@ func (a *app) account(r *http.Request, v viewerState, ret string) *chrome.Accoun
// Agents (agents.go): the tokens that let a program drive this
// person's decks. A page, so a link and no ellipsis.
acc.Items = []estilomenu.Item{{Href: "/agents", Label: rastrillo.T(r, "slides.account.agents")}}
- if a.offersHomeLink(v) {
+ if a.offersHomeLink(r, v) {
acc.Items = append(acc.Items, estilomenu.Item{Href: "/account/home", Label: rastrillo.T(r, "slides.home.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 already is the
-// connection (spec §12.1 item 4; before this Slides offered it to Home
-// sessions too).
-func (a *app) offersHomeLink(v viewerState) bool {
- return a.home != nil && v.ok && v.method != connect.HomeMethod
+// person's Oficina account: only when Home is configured and this
+// person's account has no stored link yet. The stored link decides, not
+// how this session signed in: somebody already linked who signed in with
+// the emailed link is still linked, and used to be offered it anyway
+// (Paul hit this). One lookup on the links table's unique (issuer,
+// person_id) index, on each render that draws the menu; an error means
+// unknown, and offers nothing.
+func (a *app) offersHomeLink(r *http.Request, v viewerState) bool {
+ if a.home == nil || !v.ok || v.user.ID == 0 {
+ return false
+ }
+ linked, err := a.home.Linked(r.Context(), v.user.ID)
+ return err == nil && !linked
}
// switcher is the app switcher, or nil when no hub directory is
diff --git a/internal/slides/home.go b/internal/slides/home.go
index d995a05..5c70636 100644
--- a/internal/slides/home.go
+++ b/internal/slides/home.go
@@ -103,7 +103,7 @@ func (a *app) renderHome(w http.ResponseWriter, r *http.Request, state, label st
a.renderSignedOut(w, r, status)
return
}
- page := connect.Page(state, label, func(key string) string { return rastrillo.T(r, "slides.home."+key) })
+ page := connect.PageFor(r, state, label, func(key string) string { return rastrillo.T(r, "slides.home."+key) })
var buf bytes.Buffer
if err := pages["home_link"].ExecuteTemplate(&buf, "layout", homeLinkView{shell: a.publicShell(r, titled(page.Title)), PageData: page}); err != nil {
a.logger.Error("slides: render Home account page", "err", err)
diff --git a/internal/slides/home_menu_test.go b/internal/slides/home_menu_test.go
new file mode 100644
index 0000000..08bf757
--- /dev/null
+++ b/internal/slides/home_menu_test.go
@@ -0,0 +1,314 @@
+package slides
+
+import (
+ "html"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "regexp"
+ "strings"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/home/connect"
+ "amadan.net/oficina/home/handoff"
+ "amadan.net/rastrillo/rastrillo"
+ "amadan.net/rastrillo/rastrillo/assertion"
+ assertioncrypto "amadan.net/rastrillo/rastrillo/crypto"
+)
+
+// The account menu's "Connect your Oficina account" follows the stored
+// link, not how this session signed in (Paul hit the item while already
+// linked, signed in with the emailed link). These run the app's real
+// router under the locale middleware, with one browser's cookie jar.
+
+const menuItem = `href="/account/home"`
+const menuLabel = "Connect your Oficina account"
+
+// homeBrowser is one browser against the Home-configured app.
+type homeBrowser struct {
+ t *testing.T
+ a *app
+ key *assertioncrypto.Keypair
+ h http.Handler
+ jar map[string]*http.Cookie
+}
+
+func newHomeBrowser(t *testing.T) (*homeBrowser, User) {
+ t.Helper()
+ mux, a, u, key := homeFixtureKey(t)
+ loc, err := rastrillo.NewLocales(Locales, DefaultLocale, rastrillo.BaseCatalog(), LocaleFS)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return &homeBrowser{t: t, a: a, key: key, h: loc.Middleware(mux), jar: map[string]*http.Cookie{}}, u
+}
+
+func (b *homeBrowser) set(c *http.Cookie) { b.jar[c.Name] = c }
+
+func (b *homeBrowser) do(method, path string, form url.Values, origin string) *httptest.ResponseRecorder {
+ b.t.Helper()
+ var body *strings.Reader
+ if form != nil {
+ body = strings.NewReader(form.Encode())
+ } else {
+ body = strings.NewReader("")
+ }
+ r := httptest.NewRequest(method, homeTestOrigin+path, body)
+ r.Header.Set("Accept", "text/html")
+ if method == http.MethodPost {
+ r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ if origin == "" {
+ origin = homeTestOrigin
+ }
+ }
+ if origin != "" {
+ r.Header.Set("Origin", origin)
+ }
+ for _, c := range b.jar {
+ r.AddCookie(c)
+ }
+ w := httptest.NewRecorder()
+ b.h.ServeHTTP(w, r)
+ for _, c := range w.Result().Cookies() {
+ if c.MaxAge < 0 {
+ delete(b.jar, c.Name)
+ } else {
+ b.jar[c.Name] = c
+ }
+ }
+ return w
+}
+
+func (b *homeBrowser) get(path string) *httptest.ResponseRecorder {
+ return b.do(http.MethodGet, path, nil, "")
+}
+
+// home answers the Home request at loc, as Oficina would, with a signed
+// assertion for subject, and follows the callback to the finish redirect.
+func (b *homeBrowser) home(loc, subject string) *httptest.ResponseRecorder {
+ b.t.Helper()
+ target, err := url.Parse(loc)
+ if err != nil || target.Query().Get("nonce") == "" {
+ b.t.Fatalf("not a Home request: %q", loc)
+ }
+ id, _ := assertion.RandomID()
+ now := time.Now().Unix()
+ token, err := assertion.Sign(b.key, assertion.Claims{Version: 1, Issuer: b.a.cfg.Home.Issuer, Audience: homeTestOrigin, Subject: subject, KeyID: "test", ID: id, Nonce: target.Query().Get("nonce"), IssuedAt: now, ExpiresAt: now + 90, AuthTime: now})
+ if err != nil {
+ b.t.Fatal(err)
+ }
+ cb := b.do(http.MethodPost, handoff.CallbackPath, url.Values{"assertion": {token}}, b.a.cfg.Home.Issuer)
+ if cb.Code != 303 {
+ b.t.Fatal("callback", cb.Code, cb.Body.String())
+ }
+ return b.get(handoff.FinishPath)
+}
+
+// localSignIn signs in with the emailed link through the real form, mail
+// and confirm door, and returns the verify POST's response.
+func (b *homeBrowser) localSignIn(address string) *httptest.ResponseRecorder {
+ b.t.Helper()
+ sent := b.do(http.MethodPost, "/signin", url.Values{"address": {address}}, "")
+ if sent.Code != 303 {
+ b.t.Fatal("send", sent.Code, sent.Body.String())
+ }
+ b.a.FlushMail()
+ link := regexp.MustCompile(`https://[^\s<>]+/auth/verify\?token=[A-Za-z0-9_-]+`).FindString(b.a.cfg.Mailer.(*homeTestMail).body)
+ if link == "" {
+ b.t.Fatal("no sign-in link mailed")
+ }
+ parsed, _ := url.Parse(link)
+ open := b.get(parsed.RequestURI())
+ m := confirmForm.FindStringSubmatch(open.Body.String())
+ if open.Code != 200 || m == nil {
+ b.t.Fatal("confirm door", open.Code, open.Body.String())
+ }
+ return b.do(http.MethodPost, html.UnescapeString(m[1]), url.Values{"token": {html.UnescapeString(m[2])}}, "")
+}
+
+func (b *homeBrowser) linkRows() int64 {
+ var n int64
+ if err := b.a.db.G.Table("home_identity_links").Count(&n).Error; err != nil {
+ b.t.Fatal(err)
+ }
+ return n
+}
+
+// menuOffers renders the deck list and reports whether its account menu
+// offers the connection.
+func (b *homeBrowser) menuOffers() bool {
+ b.t.Helper()
+ w := b.get("/")
+ if w.Code != 200 || !strings.Contains(w.Body.String(), "Sign out") {
+ b.t.Fatalf("deck list not rendered signed in: %d\n%s", w.Code, w.Body.String())
+ }
+ body := w.Body.String()
+ return strings.Contains(body, menuItem) && strings.Contains(body, menuLabel)
+}
+
+func TestHomeMenuOffersConnectToUnlinkedLocalSession(t *testing.T) {
+ b, u := newHomeBrowser(t)
+ b.set(homeSession(t, b.a, u.Email, "magiclink"))
+ if !b.menuOffers() {
+ t.Fatal("an unlinked local session is not offered Connect your Oficina account")
+ }
+}
+
+// The bug: linked, but signed in with the emailed link.
+func TestHomeMenuHidesConnectFromLinkedLocalSession(t *testing.T) {
+ b, u := newHomeBrowser(t)
+ homeLinked(t, b.a, u)
+ b.set(homeSession(t, b.a, u.Email, "magiclink"))
+ if b.menuOffers() {
+ t.Fatal("a linked person signed in locally is still offered Connect your Oficina account")
+ }
+}
+
+func TestHomeMenuHidesConnectFromHomeSession(t *testing.T) {
+ b, u := newHomeBrowser(t)
+ homeLinked(t, b.a, u)
+ b.set(homeSession(t, b.a, "home-person", connect.HomeMethod))
+ if b.menuOffers() {
+ t.Fatal("a Home session is offered Connect your Oficina account")
+ }
+}
+
+// Right after the confirm POST, in the same session: Accounts connected,
+// the row stored, and the menu item gone on the next page.
+func TestHomeMenuConnectThenAccountsConnectedAndItemGone(t *testing.T) {
+ b, u := newHomeBrowser(t)
+ b.set(homeSession(t, b.a, u.Email, "magiclink"))
+ if !b.menuOffers() {
+ t.Fatal("precondition: the item is offered before linking")
+ }
+ start := b.do(http.MethodPost, "/account/home", nil, "")
+ if start.Code != 303 {
+ t.Fatal("start", start.Code, start.Body.String())
+ }
+ auth := b.get(start.Header().Get("Location"))
+ finish := b.home(auth.Header().Get("Location"), "menu-home-person")
+ if finish.Code != 303 || finish.Header().Get("Location") != "/account/home/confirm" {
+ t.Fatal("finish", finish.Code, finish.Header())
+ }
+ page := b.get("/account/home/confirm")
+ if page.Code != 200 || !strings.Contains(page.Body.String(), "Connect these two accounts?") || !strings.Contains(page.Body.String(), "Connect it to the Slides account below") {
+ t.Fatalf("confirm page lacks its own title and copy: %d\n%s", page.Code, page.Body.String())
+ }
+ post := b.do(http.MethodPost, "/account/home/confirm", nil, "")
+ loc := post.Header().Get("Location")
+ if post.Code != 303 || !strings.HasPrefix(loc, "/account/home/confirm?") || !strings.Contains(loc, "connected=1") {
+ t.Fatalf("confirm POST does not lead to Accounts connected: %d %q", post.Code, loc)
+ }
+ if n := b.linkRows(); n != 1 {
+ t.Fatalf("link rows after confirm = %d, want 1", n)
+ }
+ if person, err := b.a.home.Resolve(t.Context(), "menu-home-person"); err != nil || person.ID != u.ID {
+ t.Fatal("stored link does not resolve to the local person", person, err)
+ }
+ done := b.get(loc)
+ if done.Code != 200 || !strings.Contains(done.Body.String(), "Accounts connected") {
+ t.Fatalf("Accounts connected not shown: %d\n%s", done.Code, done.Body.String())
+ }
+ if !strings.Contains(done.Body.String(), `<a rst-btn="primary" href="/">Back to Slides</a>`) {
+ t.Fatal("Accounts connected has no way back\n" + done.Body.String())
+ }
+ // Its way back is the page the link began from (connect.PageFor),
+ // not always the deck list.
+ back := b.get("/account/home/confirm?connected=1&return_to=%2Fagents")
+ if back.Code != 200 || !strings.Contains(back.Body.String(), `<a rst-btn="primary" href="/agents">Back to Slides</a>`) {
+ t.Fatalf("Accounts connected does not lead back to where the link began: %d\n%s", back.Code, back.Body.String())
+ }
+ if b.menuOffers() {
+ t.Fatal("the menu still offers Connect your Oficina account after the link was stored")
+ }
+}
+
+// An ordinary Home sign-in with an unlinked subject shows the unknown
+// page; a local sign-in in the same browser then resumes the link,
+// through a fresh Home assertion and the explicit confirm.
+func TestHomeUnknownAssertionResumesLinkAfterLocalSignIn(t *testing.T) {
+ b, u := newHomeBrowser(t)
+ front := b.get("/signin")
+ if front.Code != 303 {
+ t.Fatal("front door", front.Code, front.Header())
+ }
+ auth := b.get(front.Header().Get("Location"))
+ unknown := b.home(auth.Header().Get("Location"), "unlinked-home-person")
+ body := unknown.Body.String()
+ if unknown.Code != 403 || !strings.Contains(body, "Oficina account not connected") || !strings.Contains(body, "Sign in to your Slides account once to connect it to your Oficina account.") {
+ t.Fatalf("not the unknown page: %d\n%s", unknown.Code, body)
+ }
+ if !regexp.MustCompile(`<a rst-btn="primary" href="/signin/local">`).MatchString(body) {
+ t.Fatal("the unknown page's button does not go to /signin/local\n" + body)
+ }
+ if b.get("/signin/local").Code != 200 {
+ t.Fatal("local sign-in form")
+ }
+ verify := b.localSignIn(u.Email)
+ if verify.Code != 303 || verify.Header().Get("Location") != connect.LocalCompletePath {
+ t.Fatal("local verify", verify.Code, verify.Header())
+ }
+ resume := b.get(connect.LocalCompletePath)
+ if resume.Code != 303 || resume.Header().Get("Location") != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" {
+ t.Fatalf("local sign-in did not resume the link: %d %q", resume.Code, resume.Header().Get("Location"))
+ }
+ if b.jar["__Host-oficina-home-init"] == nil {
+ t.Fatal("resume set no link-init cookie")
+ }
+ fresh := b.get(resume.Header().Get("Location"))
+ target, _ := url.Parse(fresh.Header().Get("Location"))
+ if fresh.Code != 303 || target == nil || target.Query().Get("max_age") != "300" {
+ t.Fatal("resumed link did not ask Oficina for fresh authentication", fresh.Code, fresh.Header())
+ }
+ finish := b.home(fresh.Header().Get("Location"), "unlinked-home-person")
+ if finish.Code != 303 || finish.Header().Get("Location") != "/account/home/confirm" {
+ t.Fatal("finish", finish.Code, finish.Header())
+ }
+ if n := b.linkRows(); n != 0 {
+ t.Fatal("linked before the explicit confirm")
+ }
+ if page := b.get("/account/home/confirm"); page.Code != 200 || !strings.Contains(page.Body.String(), "Connect these two accounts?") {
+ t.Fatalf("confirm page: %d\n%s", page.Code, page.Body.String())
+ }
+ post := b.do(http.MethodPost, "/account/home/confirm", nil, "")
+ if post.Code != 303 || !strings.Contains(post.Header().Get("Location"), "connected=1") {
+ t.Fatal("confirm", post.Code, post.Header())
+ }
+ if person, err := b.a.home.Resolve(t.Context(), "unlinked-home-person"); err != nil || person.ID != u.ID {
+ t.Fatal("resumed link not stored", person, err)
+ }
+ if b.linkRows() != 1 {
+ t.Fatal("expected one link row")
+ }
+}
+
+// Without the remembered arrival, a local sign-in lands as before.
+func TestHomeLocalSignInWithoutMarkerLandsAsBefore(t *testing.T) {
+ b, u := newHomeBrowser(t)
+ verify := b.localSignIn(u.Email)
+ if verify.Code != 303 || verify.Header().Get("Location") != connect.LocalCompletePath {
+ t.Fatal("local verify", verify.Code, verify.Header())
+ }
+ if done := b.get(connect.LocalCompletePath); done.Code != 303 || done.Header().Get("Location") != "/" {
+ t.Fatalf("plain local sign-in did not land on /: %d %q", done.Code, done.Header().Get("Location"))
+ }
+ if b.jar["__Host-oficina-home-init"] != nil {
+ t.Fatal("a plain local sign-in began a link")
+ }
+}
+
+// The connect page's two buttons share one rst-form-foot row, primary
+// first (estilo's homeconnect template).
+func TestHomeConnectPageButtonsInOneFootRow(t *testing.T) {
+ b, u := newHomeBrowser(t)
+ b.set(homeSession(t, b.a, u.Email, "magiclink"))
+ w := b.get("/account/home")
+ if w.Code != 200 {
+ t.Fatal(w.Code)
+ }
+ row := regexp.MustCompile(`(?s)<div rst-form-foot>\s*<form method="post" action="/account/home"><button rst-btn="primary" type="submit">Connect</button></form>\s*<a rst-btn="ghost" href="/">Back</a>\s*</div>`)
+ if !row.MatchString(w.Body.String()) {
+ t.Fatalf("the Connect and Back buttons are not one rst-form-foot row, primary first:\n%s", w.Body.String())
+ }
+}
diff --git a/internal/slides/locales/en.toml b/internal/slides/locales/en.toml
index 4c48c12..30ede6c 100644
--- a/internal/slides/locales/en.toml
+++ b/internal/slides/locales/en.toml
@@ -602,11 +602,11 @@ slides.home.start_title = "Connect your Oficina account"
slides.home.start_body = "Use your Oficina account to sign in to this Slides account. Your decks and sharing settings stay as they are."
slides.home.start_action = "Connect"
slides.home.cancel_action = "Back"
-slides.home.confirm_title = "Confirm account connection"
-slides.home.confirm_body = "Connect the account below to the Oficina account you just signed in to."
+slides.home.confirm_title = "Connect these two accounts?"
+slides.home.confirm_body = "Your Oficina account is confirmed. Connect it to the Slides account below, and you can sign in to Slides with Oficina from now on."
slides.home.confirm_action = "Connect accounts"
slides.home.unknown_title = "Oficina account not connected"
-slides.home.unknown_body = "Sign in to your existing Slides account, then connect your Oficina account from the account menu."
+slides.home.unknown_body = "Sign in to your Slides account once to connect it to your Oficina account."
slides.home.local_action = "Sign in to your existing Slides account"
slides.home.failed_title = "Could not connect accounts"
slides.home.failed_body = "Return to Slides and start again."