feat(16.3-01): tracer — g15_ token scopes through composite backend guard

- Add pact.TokenScope/HasTokenScopes and bouncer.ScopeLimit on Principal
- Append backend_api_tokens migration; Allows evaluates scope before superuser
- Composite guard routes Bearer g15_… to token lookup; smoke tests cover path

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-11 01:46:32 +02:00
parent 4d107f4ccb
commit b9273c1da7
12 changed files with 1190 additions and 13 deletions

View File

@@ -14,10 +14,11 @@ const BackendJWTBlacklistTable = "backend_jwt_blacklist"
// BackendAdminMigrations creates Winter-shaped backend identity tables, seeds
// the developer and publisher system roles, makes backend user emails
// unique case-insensitively and adds WinterCMS's nullable jsonable metadata
// column to backend_users (the admin subtitle lives there). History is isolated under the summercms.cabana
// plugin id. DDL is re-runnable so the system-role seed stays idempotent if
// the history row is removed.
// unique case-insensitively, adds WinterCMS's nullable jsonable metadata
// column to backend_users (the admin subtitle lives there), and creates
// backend_api_tokens for personal admin API tokens. History is isolated under
// the summercms.cabana plugin id. DDL is re-runnable so the system-role seed
// stays idempotent if the history row is removed.
var BackendAdminMigrations = []*gormigrate.Migration{
{
ID: "202609240001_backend_admin_identity",
@@ -130,4 +131,35 @@ FROM backend_users GROUP BY lower(email) HAVING count(*) > 1 ORDER BY 1`).Scan(&
return tx.Exec(`ALTER TABLE backend_users DROP COLUMN IF EXISTS metadata`).Error
},
},
{
// Personal admin API tokens (sha256 of g15_… only; plaintext never stored).
ID: "202610110001_backend_api_tokens",
Migrate: func(tx *gorm.DB) error {
for _, stmt := range []string{
`CREATE TABLE IF NOT EXISTS backend_api_tokens (
id BIGSERIAL PRIMARY KEY,
backend_user_id INTEGER NOT NULL REFERENCES backend_users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
token_hash CHAR(64) NOT NULL UNIQUE,
token_hint TEXT NOT NULL,
scopes TEXT NOT NULL,
expires_at TIMESTAMPTZ,
last_used_at TIMESTAMPTZ,
revoked_at TIMESTAMPTZ,
revoked_by INTEGER REFERENCES backend_users(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)`,
`CREATE INDEX IF NOT EXISTS backend_api_tokens_backend_user_id_index ON backend_api_tokens (backend_user_id)`,
} {
if err := tx.Exec(stmt).Error; err != nil {
return err
}
}
return nil
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec(`DROP TABLE IF EXISTS backend_api_tokens`).Error
},
},
}