fix(api-keys): harden scoped key auth after code review

Revoke existing keys on reset, unify scope enforcement, default empty
scopes to read-only, map API key scopes to tenant roles, and remove dead
key-generation helpers left over from the tenant api_key column removal.
This commit is contained in:
wizardchen
2026-07-05 10:40:49 +08:00
committed by lyingbug
parent 505bc7ddec
commit 3f516d7f31
17 changed files with 231 additions and 203 deletions
+2 -3
View File
@@ -23,8 +23,6 @@ type TenantService interface {
UpdateTenant(ctx context.Context, tenant *types.Tenant) (*types.Tenant, error)
// DeleteTenant deletes a tenant
DeleteTenant(ctx context.Context, id uint64) error
// UpdateAPIKey updates the API key
UpdateAPIKey(ctx context.Context, id uint64) (string, error)
// ExtractTenantIDFromAPIKey extracts the tenant ID from the API key
ExtractTenantIDFromAPIKey(apiKey string) (uint64, error)
// ListAllTenants lists all tenants (for users with cross-tenant access permission)
@@ -84,6 +82,7 @@ type TenantAPIKeyRepository interface {
GetAPIKeyByHash(ctx context.Context, hash string) (*types.TenantAPIKey, error)
ListAPIKeys(ctx context.Context, tenantID uint64) ([]*types.TenantAPIKey, error)
RevokeAPIKey(ctx context.Context, tenantID uint64, id uint64) error
RevokeAllAPIKeys(ctx context.Context, tenantID uint64) error
UpdateAPIKeyHash(ctx context.Context, id uint64, hash string) error
UpdateAPIKeyLastUsed(ctx context.Context, id uint64, at time.Time) error
}
@@ -92,7 +91,7 @@ type TenantAPIKeyService interface {
CreateAPIKey(ctx context.Context, req TenantAPIKeyCreateRequest) (*TenantAPIKeyCreateResult, error)
AuthenticateAPIKey(ctx context.Context, token string) (*types.TenantAPIKey, error)
AuthenticateTenantAPIKey(ctx context.Context, tenantID uint64, token string) (*types.TenantAPIKey, error)
EnsureTenantAPIKey(ctx context.Context, tenantID uint64, apiKey string) error
ListAPIKeys(ctx context.Context, tenantID uint64) ([]*types.TenantAPIKey, error)
RevokeAPIKey(ctx context.Context, tenantID uint64, id uint64) error
RevokeAllAPIKeys(ctx context.Context, tenantID uint64) error
}
+13 -4
View File
@@ -99,12 +99,18 @@ func (s TenantAPIKeyScope) HasScope(scope string) bool {
return false
}
func (s TenantAPIKeyScope) AllowsUnsafeOperation() bool {
// TenantRole maps API-key scopes to the tenant RBAC role carried in request
// context. Admin scope -> Admin; write -> Contributor; read-only -> Viewer.
func (s TenantAPIKeyScope) TenantRole() TenantRole {
s = s.Normalize()
if len(s.Scopes) == 0 {
return true
switch {
case s.HasScope(TenantAPIKeyScopeAdmin):
return TenantRoleAdmin
case s.HasScope(TenantAPIKeyScopeWrite):
return TenantRoleContributor
default:
return TenantRoleViewer
}
return s.HasScope(TenantAPIKeyScopeWrite) || s.HasScope(TenantAPIKeyScopeAdmin)
}
func (s TenantAPIKeyScope) AllowsKnowledgeBase(kbID string) bool {
@@ -158,6 +164,9 @@ func normalizeScopeArray(in StringArray) StringArray {
seen[item] = struct{}{}
out = append(out, item)
}
if len(out) == 0 {
out = StringArray{TenantAPIKeyScopeRead}
}
return out
}
+31
View File
@@ -0,0 +1,31 @@
package types
import "testing"
func TestTenantAPIKeyScopeNormalizeDefaultsEmptyScopesToRead(t *testing.T) {
scope := TenantAPIKeyScope{Scopes: StringArray{}}.Normalize()
if len(scope.Scopes) != 1 || scope.Scopes[0] != TenantAPIKeyScopeRead {
t.Fatalf("normalized scopes = %v, want [read]", scope.Scopes)
}
}
func TestTenantAPIKeyScopeTenantRoleMapping(t *testing.T) {
tests := []struct {
name string
in StringArray
want TenantRole
}{
{name: "read", in: StringArray{TenantAPIKeyScopeRead}, want: TenantRoleViewer},
{name: "write", in: StringArray{TenantAPIKeyScopeWrite}, want: TenantRoleContributor},
{name: "admin", in: StringArray{TenantAPIKeyScopeAdmin}, want: TenantRoleAdmin},
{name: "empty defaults read", in: StringArray{}, want: TenantRoleViewer},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TenantAPIKeyScope{Scopes: tt.in}.TenantRole()
if got != tt.want {
t.Fatalf("TenantRole() = %q, want %q", got, tt.want)
}
})
}
}