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
},
},
}

View File

@@ -52,7 +52,7 @@ func TestPhase09MigrationsFreshRollback(t *testing.T) {
t.Fatal(err)
}
rollbackBackendAdmin(t, admin)
for _, name := range []string{"backend_users", "backend_user_roles", "backend_jwt_blacklist"} {
for _, name := range []string{"backend_users", "backend_user_roles", "backend_jwt_blacklist", "backend_api_tokens"} {
if gdb.Migrator().HasTable(name) {
t.Fatalf("%s survived admin rollback", name)
}
@@ -558,10 +558,20 @@ func TestBackendAdminMetadataColumn(t *testing.T) {
if err := gdb.Table(history).Order("id").Pluck("id", &ids).Error; err != nil {
t.Fatal(err)
}
if len(ids) == 0 || ids[len(ids)-1] != "202610090001_backend_users_metadata" {
if len(ids) == 0 || ids[len(ids)-1] != "202610110001_backend_api_tokens" {
t.Fatalf("cabana history = %v", ids)
}
if last := BackendAdminMigrations[len(BackendAdminMigrations)-1].ID; last != "202610090001_backend_users_metadata" {
foundMeta := false
for _, id := range ids {
if id == "202610090001_backend_users_metadata" {
foundMeta = true
break
}
}
if !foundMeta {
t.Fatalf("cabana history missing metadata migration: %v", ids)
}
if last := BackendAdminMigrations[len(BackendAdminMigrations)-1].ID; last != "202610110001_backend_api_tokens" {
t.Fatalf("last BackendAdminMigrations entry = %s", last)
}
@@ -569,6 +579,11 @@ func TestBackendAdminMetadataColumn(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Newest is backend_api_tokens; roll it back, then metadata, so the
// metadata column is gone while the email index remains.
if err := m.RollbackLast(); err != nil {
t.Fatal(err)
}
if err := m.RollbackLast(); err != nil {
t.Fatal(err)
}