Merge branch 'perf/dashboard-loading' into main
Reduce dashboard authentication hops and loading delays
6 files changed,
+127
−14
internal/home/app.go+1 −0internal/home/assetversion.go+2 −2internal/home/dashboard.go+18 −4internal/home/dashboard_exchange.go+15 −4internal/home/dashboard_test.go+90 −3internal/home/templates/assertion.html+1 −1
CI passed — run details
diff --git a/internal/home/app.go b/internal/home/app.go| index abd4186..fe40da7 100644 |
| --- a/internal/home/app.go |
| +++ b/internal/home/app.go |
| @@ -202,6 +202,7 @@ func (a *App) Router() http.Handler { |
| r.Get("/dashboard/authorize", a.beginDashboard) |
| r.With(a.sessions.Require).Get("/dashboard/resume", a.authorizeDashboard) |
| r.With(a.sessions.Require).Get("/authorize/resume", a.handleAssertion) |
| + versionAsset("/static/assertion.js", staticAsset("assertion.js")) |
| r.Get("/static/assertion.js", serveAsset(staticAsset("assertion.js"), "text/javascript; charset=utf-8")) |
| } |
| r.Get("/signin", a.handleSigninForm) |
diff --git a/internal/home/assetversion.go b/internal/home/assetversion.go| index 99feb6a..07281ba 100644 |
| --- a/internal/home/assetversion.go |
| +++ b/internal/home/assetversion.go |
| @@ -16,8 +16,8 @@ import ( |
| // else, so a returning visitor fetches only the page, and a deploy still |
| // takes effect at once: new bytes, new version, new URL. |
| // |
| -// Scripts are linked the same way where a template can (busy.js); the |
| -// passkey and handoff scripts keep bare paths their own modules import, |
| +// Scripts are linked the same way where a template can (busy.js, assertion.js); |
| +// passkey scripts keep bare paths their own modules import, |
| // revalidated by ETag. noscript_test.go's closed list names them all. |
| // contentVersion is the first 16 hex digits of the body's SHA-256. |
diff --git a/internal/home/dashboard.go b/internal/home/dashboard.go| index 5ed3897..5d51f66 100644 |
| --- a/internal/home/dashboard.go |
| +++ b/internal/home/dashboard.go |
| @@ -69,13 +69,22 @@ func (a *App) handleDashboard(w http.ResponseWriter, r *http.Request) { |
| now := time.Now() |
| a.dash.mu.Lock() |
| a.dash.sweep(now) |
| + // The callback returns to the ordinary URL. A ready, browser-bound |
| + // pending request takes precedence over a still-fresh cached page. |
| + nonce := q.Get("dashboard") |
| + if nonce == "" && !q.Has("refresh") { |
| + if candidate := dashboardNonce(r); candidate != "" { |
| + if pending, ok := a.dash.pending[candidate]; ok && pending.Tokens != nil { |
| + nonce = candidate |
| + } |
| + } |
| + } |
| cached, found := a.dash.cache[key] |
| - if q.Get("dashboard") == "" && !q.Has("refresh") && found && now.Sub(cached.At) < 30*time.Second { |
| + if nonce == "" && !q.Has("refresh") && found && now.Sub(cached.At) < 30*time.Second { |
| a.dash.mu.Unlock() |
| a.renderDashboard(w, r, team, cached.Results) |
| return |
| } |
| - nonce := q.Get("dashboard") |
| if nonce != "" { |
| pending, present := a.dash.pending[nonce] |
| if !present || nonce != dashboardNonce(r) || pending.Tokens == nil || pending.Subject != subject || pending.Session != session || pending.Team != team.ID { |
| @@ -106,8 +115,13 @@ func (a *App) handleDashboard(w http.ResponseWriter, r *http.Request) { |
| } |
| }) |
| a.dash.mu.Unlock() |
| - // Finish at the ordinary selected-team URL, so refresh never reuses a token. |
| - http.Redirect(w, r, "/?team="+url.QueryEscape(team.ID), http.StatusSeeOther) |
| + // Old callbacks may still carry the nonce in the URL during a rollout. |
| + // New callbacks finish here, at the canonical URL, without another hop. |
| + if q.Has("dashboard") { |
| + http.Redirect(w, r, "/?team="+url.QueryEscape(team.ID), http.StatusSeeOther) |
| + } else { |
| + a.renderDashboard(w, r, team, results) |
| + } |
| return |
| } |
| if len(a.dash.pending) >= 256 { |
diff --git a/internal/home/dashboard_exchange.go b/internal/home/dashboard_exchange.go| index 8af81ac..627be85 100644 |
| --- a/internal/home/dashboard_exchange.go |
| +++ b/internal/home/dashboard_exchange.go |
| @@ -29,10 +29,17 @@ func (a *App) beginDashboard(w http.ResponseWriter, r *http.Request) { |
| http.NotFound(w, r) |
| return |
| } |
| - if _, ok := a.dashboardTeam(q.Get("team")); !ok { |
| + team, ok := a.dashboardTeam(q.Get("team")) |
| + if !ok { |
| http.NotFound(w, r) |
| return |
| } |
| + // A valid credential session already proves identity on this GET. |
| + // Only an absent session needs the sealed intent and sign-in resume. |
| + if session, ok := a.sessions.From(r); ok && ValidPersonRef(session.Subject) { |
| + a.mintDashboard(w, r, team, q.Get("nonce"), session.Subject) |
| + return |
| + } |
| b, e := json.Marshal(dashboardIntent{q.Get("team"), q.Get("nonce"), time.Now().Unix() + 300}) |
| if e != nil { |
| http.Error(w, "Dashboard unavailable", 503) |
| @@ -86,6 +93,10 @@ func (a *App) authorizeDashboard(w http.ResponseWriter, r *http.Request) { |
| http.Error(w, "Sign in to continue", 401) |
| return |
| } |
| + a.mintDashboard(w, r, team, intent.Nonce, session.Subject) |
| +} |
| + |
| +func (a *App) mintDashboard(w http.ResponseWriter, r *http.Request, team dashboardTeam, nonce, subject string) { |
| tokens := map[string]string{} |
| now := time.Now() |
| for _, s := range team.Sources { |
| @@ -94,14 +105,14 @@ func (a *App) authorizeDashboard(w http.ResponseWriter, r *http.Request) { |
| http.Error(w, "Dashboard unavailable", 503) |
| return |
| } |
| - token, e := dashboard.Sign(a.runtime.signer, dashboard.Claims{Version: 1, Issuer: a.origins.Credential, Audience: s.Origin, Directory: a.origins.Directory, Subject: session.Subject, Team: team.ID, Purpose: "dashboard-read", KeyID: a.runtime.keyID, ID: id, Nonce: intent.Nonce, Issued: now.Unix(), Expires: now.Unix() + 120}) |
| + token, e := dashboard.Sign(a.runtime.signer, dashboard.Claims{Version: 1, Issuer: a.origins.Credential, Audience: s.Origin, Directory: a.origins.Directory, Subject: subject, Team: team.ID, Purpose: "dashboard-read", KeyID: a.runtime.keyID, ID: id, Nonce: nonce, Issued: now.Unix(), Expires: now.Unix() + 120}) |
| if e != nil { |
| http.Error(w, "Dashboard unavailable", 503) |
| return |
| } |
| tokens[s.Origin] = token |
| } |
| - b, e = json.Marshal(tokens) |
| + b, e := json.Marshal(tokens) |
| if e != nil { |
| http.Error(w, "Dashboard unavailable", 503) |
| return |
| @@ -165,5 +176,5 @@ func (a *App) receiveDashboard(w http.ResponseWriter, r *http.Request) { |
| pending.Tokens = tokens |
| pending.Expires = now.Add(120 * time.Second) |
| a.dash.pending[nonce] = pending |
| - http.Redirect(w, r, "/?"+url.Values{"team": {team.ID}, "dashboard": {nonce}}.Encode(), http.StatusSeeOther) |
| + http.Redirect(w, r, "/?"+url.Values{"team": {team.ID}}.Encode(), http.StatusSeeOther) |
| } |
diff --git a/internal/home/dashboard_test.go b/internal/home/dashboard_test.go| index a065598..7459965 100644 |
| --- a/internal/home/dashboard_test.go |
| +++ b/internal/home/dashboard_test.go |
| @@ -103,11 +103,11 @@ func TestDashboardExchangeIsolationAndPrivacy(t *testing.T) { |
| t.Fatal("ambiguous session") |
| } |
| finish := exchangeRequest(p.directory, "GET", finishURL, "", nil, nonce, directorySession) |
| - if finish.Code != 303 || calls != 1 { |
| + if finish.Code != 200 || calls != 1 { |
| t.Fatal("finish", finish.Code, calls) |
| } |
| - if rec := exchangeRequest(p.directory, "GET", finishURL, "", nil, nonce, directorySession); rec.Code != 400 { |
| - t.Fatal("finish replay") |
| + if rec := exchangeRequest(p.directory, "GET", finishURL, "", nil, nonce, directorySession); rec.Code != 200 || calls != 1 { |
| + t.Fatal("canonical reload spent a token again") |
| } |
| page := exchangeRequest(p.directory, "GET", directoryOrigin+"/", "", nil, directorySession, preference) |
| if page.Code != 200 || !strings.Contains(page.Body.String(), "Dashboard private title <script>") || strings.Contains(page.Body.String(), "https://blue.docs.oficina.works/") || calls != 1 { |
| @@ -202,3 +202,90 @@ func TestDashboardDuplicateOriginRefused(t *testing.T) { |
| t.Fatal(w.Code) |
| } |
| } |
| + |
| +// Four document requests suffice with live sessions. The authenticated GET |
| +// after the cross-origin POST still binds consumption to the current session. |
| +func TestDashboardAuthenticatedFastPath(t *testing.T) { |
| + p := newExchangePair(t) |
| + credentialSession := signInFully(t, p.credential, sealtest.CanaryAddress) |
| + pending, intent := p.start(t) |
| + token, _ := p.mint(t, intent, credentialSession) |
| + directorySession := p.finish(t, token, pending) |
| + team := dashboardTeam{ID: "blue", Name: "Blue", Sources: []dashboardSource{{"docs", "https://blue.docs.oficina.works"}}} |
| + p.directory.runtime.dashboard = []dashboardTeam{team} |
| + p.credential.runtime.dashboard = []dashboardTeam{team} |
| + calls := 0 |
| + p.directory.dash.client.Transport = dashboardTransport(func(r *http.Request) (*http.Response, error) { |
| + calls++ |
| + return dashboardTestResponse(dashboardTestBody(t, dashboard.Response{Version: 1, Team: team.ID, Kind: "documents", Items: []dashboard.Item{}})), nil |
| + }) |
| + // Explicit refresh must consume the new proof even with a fresh cached page. |
| + for _, query := range []string{"?team=blue", "?team=blue&refresh=1"} { |
| + start := exchangeRequest(p.directory, "GET", directoryOrigin+"/"+query, "", nil, directorySession) |
| + if start.Code != 303 { |
| + t.Fatal("start", start.Code) |
| + } |
| + nonce := namedCookie(t, start, dashboardCookie) |
| + minted := exchangeRequest(p.credential.App, "GET", start.Header().Get("Location"), "", nil, credentialSession) |
| + if minted.Code != 200 { |
| + t.Fatal("unnecessary credential redirect", minted.Code) |
| + } |
| + m := regexp.MustCompile(`name="assertion" value="([^"]+)"`).FindStringSubmatch(minted.Body.String()) |
| + if len(m) != 2 { |
| + t.Fatal("no assertion form") |
| + } |
| + script := regexp.MustCompile(`<script src='(/static/assertion.js\?v=[0-9a-f]{16})' async></script>`).FindStringSubmatch(minted.Body.String()) |
| + if len(script) != 2 { |
| + t.Fatal("handoff script blocks on styles or revalidates") |
| + } |
| + asset := exchangeRequest(p.credential.App, "GET", Origin+script[1], "", nil) |
| + if asset.Code != 200 || asset.Header().Get("Cache-Control") != "public, max-age=31536000, immutable" { |
| + t.Fatal("handoff asset not immutable") |
| + } |
| + form := url.Values{"assertion": {html.UnescapeString(m[1])}} |
| + callback := exchangeRequest(p.directory, "POST", directoryOrigin+dashboardCallback, Origin, form, nonce) |
| + if callback.Code != 303 || callback.Header().Get("Location") != "/?team=blue" { |
| + t.Fatal("callback must finish at canonical URL") |
| + } |
| + before := calls |
| + // Duplicate current-session cookies fail without consuming the pending batch. |
| + bad := exchangeRequest(p.directory, "GET", directoryOrigin+"/?team=blue", "", nil, nonce, directorySession, directorySession) |
| + if bad.Code != 400 || calls != before { |
| + t.Fatal("ambiguous session consumed pending proof") |
| + } |
| + finish := exchangeRequest(p.directory, "GET", directoryOrigin+"/?team=blue", "", nil, nonce, directorySession) |
| + if finish.Code != 200 || calls != before+1 || !strings.Contains(finish.Body.String(), "Recent") { |
| + t.Fatal("finish", finish.Code, calls) |
| + } |
| + if finish.Header().Get("Cache-Control") != "no-store" { |
| + t.Fatal("cacheable private dashboard") |
| + } |
| + replay := exchangeRequest(p.directory, "POST", directoryOrigin+dashboardCallback, Origin, form, nonce) |
| + if replay.Code != 400 { |
| + t.Fatal("accepted consumed callback") |
| + } |
| + again := exchangeRequest(p.directory, "GET", directoryOrigin+"/?team=blue", "", nil, directorySession) |
| + if again.Code != 200 || calls != before+1 { |
| + t.Fatal("warm page pulled again") |
| + } |
| + } |
| + // An old callback in flight across a deployment still cleans up its nonce URL. |
| + p.directory.dash.mu.Lock() |
| + for k := range p.directory.dash.cache { |
| + delete(p.directory.dash.cache, k) |
| + } |
| + p.directory.dash.mu.Unlock() |
| + start := exchangeRequest(p.directory, "GET", directoryOrigin+"/?team=blue", "", nil, directorySession) |
| + nonce := namedCookie(t, start, dashboardCookie) |
| + minted := exchangeRequest(p.credential.App, "GET", start.Header().Get("Location"), "", nil, credentialSession) |
| + m := regexp.MustCompile(`name="assertion" value="([^"]+)"`).FindStringSubmatch(minted.Body.String()) |
| + exchangeRequest(p.directory, "POST", directoryOrigin+dashboardCallback, Origin, url.Values{"assertion": {html.UnescapeString(m[1])}}, nonce) |
| + legacyURL := directoryOrigin + "/?team=blue&dashboard=" + url.QueryEscape(nonce.Value) |
| + legacy := exchangeRequest(p.directory, "GET", legacyURL, "", nil, nonce, directorySession) |
| + if legacy.Code != 303 || legacy.Header().Get("Location") != "/?team=blue" { |
| + t.Fatal("legacy callback cleanup") |
| + } |
| + if replay := exchangeRequest(p.directory, "GET", legacyURL, "", nil, nonce, directorySession); replay.Code != 400 { |
| + t.Fatal("legacy nonce replay") |
| + } |
| +} |
diff --git a/internal/home/templates/assertion.html b/internal/home/templates/assertion.html| index 723462b..ed3093e 100644 |
| --- a/internal/home/templates/assertion.html |
| +++ b/internal/home/templates/assertion.html |
| @@ -20,4 +20,4 @@ |
| <button type="submit" rst-btn="primary">Continue</button> |
| </form> |
| {{end}} |
| -{{define "scripts"}}<script src="/static/assertion.js" defer></script>{{end}} |
| +{{define "scripts"}}{{/* The form is already parsed here. Async submits without waiting for CSS. */}}<script src='{{v "/static/assertion.js"}}' async></script>{{end}} |