Migrate CollabTable's AuthGuard proxy onto shared/backend/auth
Lint / Lint (push) Successful in 38s
Tests / Go backend (push) Successful in 1m9s
Tests / Dashboard checks (push) Successful in 1m25s
Docker / Docker Build & Push (push) Successful in 4m32s

Replaces the ~1044-line bespoke session/login/TOTP/admin-user proxy
(backend/auth.go, built directly on authclient) with
sharedauth.NewWithStore, the same shared engine GameStream and
OmniLogger already migrated onto. A tiny collabTableAuthStore adapter
delegates the local one-time setup-claim flag to the existing
collabtable_setup_state table/queries; CollabTable never had a
"require login" toggle, so LoginRequired always reports true and
SetLoginRequired is a no-op.

The local authguard_totp_cache (GetCachedTOTPEnabled/
SetCachedTOTPEnabled) is retired: the shared engine reads TOTP status
live from AuthGuard's GET /api/me. Its table is left in place, same as
the already-dormant auth_users/auth_sessions tables.

URL paths and HTTP methods are unchanged; only response shapes
changed to the shared engine's standardized ones (nested `user: {...}`,
`data: {...}` for TOTP setup/enable/disable). Updated
frontend/src/lib/api.js to translate these back into the shapes the
rest of the frontend already expects, and AppShell.svelte's
saveProfile to always send the current username/email (UpdateMe
requires a non-empty email on every call).

Known behavior differences: POST /api/auth/setup now returns 201
instead of 200; PUT/PATCH /api/auth/users/{id} now returns the updated
user instead of {"ok":true}; POST /api/auth/credentials no longer
forces re-login after a successful change (no bulk session-invalidation
call exists in the shared engine, only single-session Logout).

The separate device/sync API bearer-token scheme (requireAPIToken,
handleTokenStatus/handleTokenRegenerate) is untouched other than
resolving the caller's identity via sharedauth.UserFromContext instead
of local session lookups.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Gabriel20xxandClaude Sonnet 5 committed 2026-09-13 21:11:53 +02:00
1 parent aae8d3807d
commit 6e13364ebd
7 files changed
+300 -1230

No files matched your search

+20 -38
View File
@@ -152,16 +152,16 @@ func (d *DB) initSchema(ctx context.Context) error {
CHECK (id = 1)
);
-- authguard_totp_cache is a best-effort, locally-cached mirror of "does
-- this AuthGuard account have TOTP enabled", keyed by AuthGuard's own
-- numeric user id. AuthGuard's HTTP API (deliberately not modified by
-- this migration) has no endpoint to query current TOTP status, only to
-- enable/verify/disable it, so CollabTable keeps this cache updated
-- itself at every point it can observe the true state (successful TOTP
-- verify/disable calls, and a successful login attempted without a TOTP
-- token, which AuthGuard would have rejected had TOTP been enabled). See
-- handleTOTPStatus's doc comment in auth.go for the full reasoning and
-- its known staleness caveat.
-- authguard_totp_cache is RETIRED as of the migration onto
-- shared/backend/auth (sharedauth.Service): it used to be a
-- best-effort, locally-cached mirror of "does this AuthGuard account
-- have TOTP enabled", keyed by AuthGuard's own numeric user id, needed
-- because AuthGuard's HTTP API had no endpoint to query current TOTP
-- status, only to enable/verify/disable it. sharedauth.Service's
-- Me/Status/TOTPStatus now read TOTP-enabled status live from
-- AuthGuard's GET /api/me on every request instead, so no Go code
-- reads or writes this table anymore. Left in place rather than
-- dropped, same as auth_users/auth_sessions above.
CREATE TABLE IF NOT EXISTS authguard_totp_cache (
authguard_user_id BIGINT PRIMARY KEY,
totp_enabled BOOLEAN NOT NULL DEFAULT FALSE,
@@ -276,34 +276,16 @@ func (d *DB) UnclaimSetup(ctx context.Context) error {
return nil
}
// GetCachedTOTPEnabled returns the best-effort locally-cached TOTP-enabled
// state for an AuthGuard account, or false if this instance has never
// observed that account's TOTP state. See handleTOTPStatus's doc comment in
// auth.go.
func (d *DB) GetCachedTOTPEnabled(ctx context.Context, authGuardUserID int64) (bool, error) {
var enabled bool
err := d.pool.QueryRow(ctx, `SELECT totp_enabled FROM authguard_totp_cache WHERE authguard_user_id = $1`, authGuardUserID).Scan(&enabled)
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("get cached totp state: %w", err)
}
return enabled, nil
}
// SetCachedTOTPEnabled upserts the best-effort locally-cached TOTP-enabled
// state for an AuthGuard account.
func (d *DB) SetCachedTOTPEnabled(ctx context.Context, authGuardUserID int64, enabled bool) error {
_, err := d.pool.Exec(ctx, `
INSERT INTO authguard_totp_cache (authguard_user_id, totp_enabled, updated_at) VALUES ($1, $2, now())
ON CONFLICT (authguard_user_id) DO UPDATE SET totp_enabled = $2, updated_at = now()
`, authGuardUserID, enabled)
if err != nil {
return fmt.Errorf("set cached totp state: %w", err)
}
return nil
}
// Note: this instance used to also keep a local best-effort cache of each
// AuthGuard account's TOTP-enabled state (GetCachedTOTPEnabled/
// SetCachedTOTPEnabled, backed by the authguard_totp_cache table above) --
// that existed only because the pre-migration code believed AuthGuard could
// not report TOTP status live. It can (GET /api/me), so sharedauth.Service's
// Me/Status/TOTPStatus now read it live from AuthGuard directly and this
// cache's Go code was deleted along with the migration onto that shared
// package. The authguard_totp_cache table itself (and any rows already in
// it) is left in place rather than dropped, same as auth_users/auth_sessions
// above.
func (d *DB) hasAPIToken(ctx context.Context, userID int64) (bool, error) {
var exists bool