fix(kiro): 修复测试模型时 403 bearer token invalid 错误

问题:
Kiro 测试模型时返回 403,原因是 get_provider_auth() 直接使用 key.api_key 作为 access_token,但新导入的 key 其 api_key 是占位符,且当 expires_at 未设置时不会触发刷新。

修复:
1. 如果没有缓存的 access_token 或 api_key 是占位符,强制刷新
2. Kiro 类型优先使用 auth_config 中缓存的 access_token
This commit is contained in:
AAEE86
2026-02-09 03:44:13 +08:00
parent a1c5aa4e04
commit 6ffdd38c3a

View File

@@ -1032,6 +1032,7 @@ async def get_provider_auth(
expires_at = token_meta.get("expires_at")
refresh_token = token_meta.get("refresh_token")
provider_type = str(token_meta.get("provider_type") or "")
cached_access_token = str(token_meta.get("access_token") or "").strip()
# 120s skew (or force refresh when upstream returns 401)
should_refresh = False
@@ -1044,6 +1045,12 @@ async def get_provider_auth(
if force_refresh:
should_refresh = True
# Kiro 特殊处理:如果没有缓存的 access_token 或 key.api_key 是占位符,强制刷新
if provider_type == "kiro" and not should_refresh:
decrypted_api_key = crypto_service.decrypt(key.api_key)
if not cached_access_token or decrypted_api_key == "__placeholder__":
should_refresh = True
if should_refresh and refresh_token and provider_type:
try:
from src.core.provider_templates.fixed_providers import FIXED_PROVIDERS
@@ -1070,7 +1077,18 @@ async def get_provider_auth(
# 刷新失败不阻断请求;后续由上游返回 401 再触发管理端处理
pass
decrypted_key = crypto_service.decrypt(key.api_key)
# 获取最终使用的 access_token
# Kiro 优先使用 token_meta 中缓存的 access_token刷新后会更新到 token_meta
effective_access_token: str
if provider_type == "kiro":
# Kiro: 优先使用 token_meta 中的 access_token回退到 key.api_key
cached_token = str(token_meta.get("access_token") or "").strip()
if cached_token:
effective_access_token = cached_token
else:
effective_access_token = crypto_service.decrypt(key.api_key)
else:
effective_access_token = crypto_service.decrypt(key.api_key)
decrypted_auth_config: dict[str, Any] | None = None
if isinstance(token_meta, dict) and token_meta:
@@ -1078,7 +1096,7 @@ async def get_provider_auth(
return ProviderAuthInfo(
auth_header="Authorization",
auth_value=f"Bearer {decrypted_key}",
auth_value=f"Bearer {effective_access_token}",
decrypted_auth_config=decrypted_auth_config,
)
if auth_type == "vertex_ai":