security: 平台中继令牌不对外展示、不允许删除
平台中继令牌由系统自动创建(name='平台中继令牌'),桌面端智能体
调用模型时使用此令牌。为防止用户复制外泄配额或误删导致桌面端
无法调用模型:
1. GET /opc/compute/tokens:过滤掉 name='平台中继令牌' 的令牌,
不返回给前端展示
2. DELETE /opc/compute/tokens/{id}:删除前校验,若目标令牌是
平台中继令牌,返回 403 拒绝删除
3. 定义常量 PLATFORM_RELAY_TOKEN_NAME 统一管理
This commit is contained in:
@@ -18,6 +18,9 @@ from ... import config
|
||||
from ...services import compute_client, compute_catalog
|
||||
|
||||
router = APIRouter(prefix="/opc", tags=["opc"])
|
||||
PLATFORM_RELAY_TOKEN_NAME = "平台中继令牌"
|
||||
# 平台中继令牌:系统自动创建,桌面端智能体调用模型时使用,不对外展示、不允许删除
|
||||
|
||||
|
||||
|
||||
@router.get("/dashboard", summary="OPC 工作台总览")
|
||||
@@ -530,6 +533,9 @@ async def opc_compute_tokens(user: dict = Depends(require_roles("opc_member"))):
|
||||
out = []
|
||||
for it in items or []:
|
||||
it = dict(it)
|
||||
# 平台中继令牌由系统自动创建,桌面端智能体调用模型时使用,不对外展示、不允许删除
|
||||
if str(it.get("name") or "") == PLATFORM_RELAY_TOKEN_NAME:
|
||||
continue
|
||||
key = str(it.get("key") or it.get("token") or "")
|
||||
if key and not key.startswith("sk-"):
|
||||
it["key"] = f"sk-{key}"
|
||||
@@ -563,6 +569,17 @@ async def opc_compute_tokens_create(
|
||||
|
||||
@router.delete("/compute/tokens/{token_id}", summary="删除算力令牌")
|
||||
async def opc_compute_tokens_delete(token_id: int, user: dict = Depends(require_roles("opc_member"))):
|
||||
# 平台中继令牌不允许删除(桌面端智能体依赖此令牌调用模型)
|
||||
try:
|
||||
existing = await compute_client.list_user_tokens(user.get("username"))
|
||||
except compute_client.ComputeError as exc:
|
||||
raise HTTPException(status_code=502, detail=f"算力引擎对接失败: {exc}") from exc
|
||||
existing_items = existing if isinstance(existing, list) else existing.get("items", [])
|
||||
for it in existing_items or []:
|
||||
if int(dict(it).get("id") or 0) == token_id:
|
||||
if str(dict(it).get("name") or "") == PLATFORM_RELAY_TOKEN_NAME:
|
||||
raise HTTPException(status_code=403, detail="平台中继令牌由系统管理,不允许删除")
|
||||
break
|
||||
try:
|
||||
await compute_client.delete_token(token_id)
|
||||
except compute_client.ComputeError as exc:
|
||||
|
||||
Reference in New Issue
Block a user