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
+6 -13
View File
@@ -11,14 +11,9 @@ import (
var errTenantAPIKeyScopeForbidden = stderrors.New("tenant api key scope forbidden")
func authorizeTenantAPIKeyOperation(ctx context.Context, method string) error {
if _, ok := types.TenantAPIKeyScopeFromContext(ctx); !ok {
return nil
}
return nil
}
func authorizeTenantAPIKeyRoute(ctx context.Context, method, path string) error {
// authorizeTenantAPIKeyAccess enforces per-key operation and route scopes for
// X-API-Key callers. Non-API-key principals are not restricted.
func authorizeTenantAPIKeyAccess(ctx context.Context, method, path string) error {
scope, ok := types.TenantAPIKeyScopeFromContext(ctx)
if !ok {
return nil
@@ -99,22 +94,20 @@ func isKnowledgeScopedUnsafePath(path string) bool {
func allowsTenantAPIKeyRead(scope types.TenantAPIKeyScope) bool {
scope = scope.Normalize()
return len(scope.Scopes) == 0 ||
scope.HasScope(types.TenantAPIKeyScopeRead) ||
return scope.HasScope(types.TenantAPIKeyScopeRead) ||
scope.HasScope(types.TenantAPIKeyScopeWrite) ||
scope.HasScope(types.TenantAPIKeyScopeAdmin)
}
func allowsTenantAPIKeyWrite(scope types.TenantAPIKeyScope) bool {
scope = scope.Normalize()
return len(scope.Scopes) == 0 ||
scope.HasScope(types.TenantAPIKeyScopeWrite) ||
return scope.HasScope(types.TenantAPIKeyScopeWrite) ||
scope.HasScope(types.TenantAPIKeyScopeAdmin)
}
func allowsTenantAPIKeyAdmin(scope types.TenantAPIKeyScope) bool {
scope = scope.Normalize()
return len(scope.Scopes) == 0 || scope.HasScope(types.TenantAPIKeyScopeAdmin)
return scope.HasScope(types.TenantAPIKeyScopeAdmin)
}
func isTenantAPIKeyReadUnsafePath(method, path string) bool {