Cache credentials within each process

Merged
RO
3mo ago
+728-20

Closes #453

What changed

  • Cache the first successful credential load in auth.Manager, guarded by its existing mutex, so one process no longer opens a Secret Service session for every HTTP request.
  • Copy credentials into and out of the cache so callers cannot mutate cached state.
  • Publish login and refreshed credentials only after persistence succeeds; clear the cache only after successful logout or invalid_grant deletion.
  • Preserve failed save/delete credentials, and never cache missing, malformed, or transiently failed loads.
  • Force the refresh/401 path to reread persistent storage under the existing cross-process lock, adopting OAuth, static-token, or cookie credentials rotated by another process.
  • Clear stale cached credentials when that forced reread discovers the stored credential is gone or cannot be read.

Safeguards and scope

  • HEY_TOKEN continues to bypass credential storage.
  • Concurrent requests coalesce behind the Manager mutex, and refresh-token rotation retains the existing cross-process lock/adoption behavior.
  • The keyring availability-probe removal suggested in the issue is intentionally deferred; its fallback semantics are separate from this fix.
  • Cross-process cache invalidation for ordinary requests is intentionally deferred. A running process keeps its process-local credential until refresh/401 or exit; logout clears persistent storage and the Manager that performed it. No generation sidecar, IPC, TTL, or plugin command consolidation is added here.
  • Fresh concurrent processes can still each open one Secret Service session; the upstream gnome-keyring fix remains definitive.

Tests

  • GOWORK=off go test ./internal/auth ./internal/cable ./internal/cmd -count=1
  • GOWORK=off go test -race ./internal/auth -count=1
  • GOWORK=off make test
  • git diff --check

Focused coverage includes successful-load reuse, concurrent load and refresh coalescing, copy safety, failed load/save/delete behavior, logout and invalid_grant cleanup, forced-read deletion, HEY_TOKEN, refresh-token locking/adoption, and real SDK 401 retries with replacement OAuth, static-token, and cookie credentials.


Summary by cubic

Closes #453 by caching credentials after the first successful load in each process instead of opening a Secret Service session for every request. The cache is process-local, so a running process adopts another process's changes on refresh, a 401 retry, or a cable re-dial.

Bug Fixes

  • Copies credentials in and out of the cache so callers cannot mutate cached state.
  • Updates the cache only after successful persistence, retaining working credentials when saves or deletes fail.
  • Clears stale credentials after successful logout, invalid_grant cleanup, or a forced read that finds storage unavailable.
  • Forced refreshes reread storage under the cross-process lock and adopt rotated OAuth, static-token, or cookie credentials before the SDK retries.
  • Cable connections reread stored credentials on each dial, so reconnects use the latest authentication.
  • HEY_TOKEN continues to bypass credential storage, and concurrent requests share one load or refresh.