oficina / calendar Public

A first local sign-in after an unknown Oficina arrival resumes the link

Astra's P2 on #31: connect resumes the link from /auth/local/complete,
and that needs the person's people row. An admitted address signing in
for the first time had none (requirePerson makes it on the first page),
so the resume fell to /account/home, which answered 403.
homeLocalComplete now runs EnsurePersonSetup for a fresh, admitted local
session first, from the verified local address and never from the Home
subject, then hands on to connect. The unknown-flow test now also runs
with no people row; that case failed with "/account/home" before this.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 5ffaddf0b225e438ea3815406cc504d39e12bddb parent d7b5731
3 files changed, +49 −4
  • internal/calendar/home.go +27 −0
  • internal/calendar/home_connect_item_test.go +21 −3
  • internal/calendar/routes.go +1 −1

CI passed — run details

diff --git a/internal/calendar/home.go b/internal/calendar/home.go
index 6897343..746da72 100644
--- a/internal/calendar/home.go
+++ b/internal/calendar/home.go
@@ -6,6 +6,7 @@ import (
"database/sql"
"errors"
"net/http"
+ "time"
"amadan.net/oficina/home/connect"
"amadan.net/oficina/home/connect/linkstore"
@@ -159,3 +160,29 @@ func (a *App) homeSignin(w http.ResponseWriter, r *http.Request) {
}
a.home.routes.Signin(w, r)
}
+
+// homeLocalComplete is GET /auth/local/complete with Home configured: where
+// a local sign-in lands. After an Oficina assertion Calendar had no link
+// for, connect resumes the link from here, and that needs the person's
+// people row. A first-ever local sign-in has none yet (requirePerson makes
+// it on the first page), so the resume fell to /account/home, which
+// answered 403 (Astra's review of #31). A fresh, admitted local session
+// gets its person here first, exactly as requirePerson would set it up;
+// any other request is passed on untouched. Never from a Home subject:
+// only the verified local address. Failing costs only the shortcut: the
+// sign-in itself has already succeeded.
+func (a *App) homeLocalComplete(w http.ResponseWriter, r *http.Request) {
+ if s, ok := a.home.sessions.From(r); ok && (s.Method == "magiclink" || s.Method == "keymail") {
+ now := time.Now
+ if a.cfg.homeNow != nil {
+ now = a.cfg.homeNow
+ }
+ address := normalizeAddress(s.Subject)
+ if _, fresh := connect.FreshLocal(connect.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: s.At}, now()); fresh && address != "" && a.home.admitted.allows(address) {
+ if _, err := a.store.EnsurePersonSetup(r.Context(), address); err != nil {
+ a.logger.Error("calendar: ensure person before resuming a Home link", "err", err)
+ }
+ }
+ }
+ a.home.routes.LocalComplete(w, r)
+}
diff --git a/internal/calendar/home_connect_item_test.go b/internal/calendar/home_connect_item_test.go
index a6d018c..85e85fd 100644
--- a/internal/calendar/home_connect_item_test.go
+++ b/internal/calendar/home_connect_item_test.go
@@ -144,11 +144,29 @@ func TestAccountsConnectedReturnsToWhereTheLinkBegan(t *testing.T) {
// An Oficina sign-in with an unlinked subject shows the unknown page; a
// local sign-in in the same browser then begins the link itself, through
// the fresh Home assertion, to the confirm page, and the confirm stores it.
+// "first sign-in" starts with no people row at all (Astra's review of #31):
+// the local sign-in must create the person before connect resumes, or the
+// resume falls to /account/home and that page answers 403.
func TestUnknownOficinaSigninResumesAfterLocalSignin(t *testing.T) {
- f := newHomeRouterFixture(t)
- if _, err := f.app.store.EnsurePersonSetup(t.Context(), "alice@example.test"); err != nil {
- t.Fatal(err)
+ for _, seeded := range []bool{true, false} {
+ name := "existing person"
+ if !seeded {
+ name = "first sign-in"
+ }
+ t.Run(name, func(t *testing.T) {
+ f := newHomeRouterFixture(t)
+ if seeded {
+ if _, err := f.app.store.EnsurePersonSetup(t.Context(), "alice@example.test"); err != nil {
+ t.Fatal(err)
+ }
+ }
+ unknownThenResume(t, f)
+ })
}
+}
+
+func unknownThenResume(t *testing.T, f *homeRouterFixture) {
+ t.Helper()
start := homeRedirect(t, f.do("GET", "/signin", ""))
unknown := f.exchange(t, start, "opaque-home-alice")
if unknown.Code != 403 || !strings.Contains(unknown.Body.String(), "Oficina account not connected") || !strings.Contains(unknown.Body.String(), `<a rst-btn="primary" href="/signin/local">`) {
diff --git a/internal/calendar/routes.go b/internal/calendar/routes.go
index d77014f..426eacc 100644
--- a/internal/calendar/routes.go
+++ b/internal/calendar/routes.go
@@ -46,7 +46,7 @@ func (a *App) routes(h *handlers, authPlugin *rauth.Auth, list allowlist, origin
r.Get("/signin", a.homeSignin)
r.Get("/signin/local", h.signin)
r.Get("/signed-out", a.home.routes.SignedOut)
- r.Get(connect.LocalCompletePath, a.home.routes.LocalComplete)
+ r.Get(connect.LocalCompletePath, a.homeLocalComplete)
r.Get(handoff.StartPath, a.home.bridge.Start)
r.Post(handoff.CallbackPath, a.home.bridge.Callback)
r.Get(handoff.FinishPath, a.home.bridge.Finish)