refactor(api-keys): use role field and route-level API key guards

Replace scopes JSON with a viewer/contributor/admin role column, move
unsafe-method authorization from path-prefix middleware to declarative
APIKey* guards on routes, and close remaining KB-scope gaps in handlers.
This commit is contained in:
wizardchen
2026-07-06 13:23:55 +08:00
committed by lyingbug
parent 3ace6e657f
commit 9a76a24ce6
28 changed files with 678 additions and 438 deletions
+3 -3
View File
@@ -48,13 +48,13 @@ export interface APIPrincipalTestToken {
external_user_id: string
}
export type TenantAPIKeyScope = 'read' | 'write' | 'admin'
export type TenantAPIKeyRole = 'viewer' | 'contributor' | 'admin'
export interface TenantAPIKey {
id: number
name: string
api_key: string
scopes: TenantAPIKeyScope[]
role: TenantAPIKeyRole
knowledge_base_ids: string[]
last_used_at?: string
expires_at?: string
@@ -67,7 +67,7 @@ export interface CreatedTenantAPIKey extends TenantAPIKey {
export interface CreateTenantAPIKeyPayload {
name: string
scopes: TenantAPIKeyScope[]
role: TenantAPIKeyRole
knowledge_base_ids?: string[]
expires_at_unix?: number
}
@@ -130,7 +130,7 @@
</td>
<td>
<t-tag size="small" variant="light">
{{ formatApiKeyAccessRoleLabel(key.scopes) }}
{{ formatApiKeyAccessRoleLabel(key.role) }}
</t-tag>
</td>
<td>
@@ -469,7 +469,7 @@
</t-radio-group>
<p class="scope-role-hint">{{ $t('integrations.api.apiKeyAccessRoleHint') }}</p>
<div class="scope-help scope-help--role">
<p class="scope-help__role">{{ formatApiKeyAccessRoleLabel(accessRoleToScopes(apiKeyForm.role)) }}</p>
<p class="scope-help__role">{{ formatApiKeyAccessRoleLabel(apiKeyForm.role) }}</p>
<p class="scope-help__desc">{{ apiKeyFormRoleDesc }}</p>
</div>
</div>
@@ -522,7 +522,7 @@ import {
type APIPrincipalConfig,
type APIPrincipalMode,
type TenantAPIKey,
type TenantAPIKeyScope,
type TenantAPIKeyRole,
} from '@/api/tenant'
import { listKnowledgeBases } from '@/api/knowledge-base'
import { getApiBaseUrl } from '@/utils/api-base'
@@ -576,32 +576,14 @@ const apiKeyForm = reactive({
knowledge_base_ids: [] as string[],
})
function scopesToAccessRole(scopes: TenantAPIKeyScope[] | undefined): ApiKeyAccessRole {
const normalized = (scopes ?? []).map((scope) => scope.toLowerCase())
if (normalized.includes('admin')) return 'admin'
if (normalized.includes('write')) return 'contributor'
return 'viewer'
}
function accessRoleToScopes(role: ApiKeyAccessRole): TenantAPIKeyScope[] {
switch (role) {
case 'admin':
return ['admin']
case 'contributor':
return ['write']
default:
return ['read']
}
}
function formatApiKeyAccessRoleLabel(scopes: TenantAPIKeyScope[] | undefined): string {
const role = scopesToAccessRole(scopes)
function formatApiKeyAccessRoleLabel(role: ApiKeyAccessRole | TenantAPIKeyRole | undefined): string {
const normalized = (role ?? 'viewer') as ApiKeyAccessRole
const labels: Record<ApiKeyAccessRole, string> = {
viewer: t('integrations.api.accessRoleViewer'),
contributor: t('integrations.api.accessRoleContributor'),
admin: t('integrations.api.accessRoleAdmin'),
}
return labels[role]
return labels[normalized] ?? labels.viewer
}
const apiKeyFormRoleDesc = computed(() => {
@@ -1152,7 +1134,7 @@ async function createScopedAPIKey() {
try {
const resp = await createTenantAPIKey(tenantId.value, {
name: apiKeyForm.name.trim(),
scopes: accessRoleToScopes(apiKeyForm.role),
role: apiKeyForm.role,
knowledge_base_ids: apiKeyForm.knowledge_base_ids,
})
if (!resp.success || !resp.data?.api_key) {