feat(cache): 打通 cargo 客户端路径(裸 token + 401 带 WWW-Authenticate)

cargo 客户端此前有三处独立断裂,任一处都足以让它全程 401、装不上 crate:

1. cargo 发的是**裸 token**(Authorization: sk_…,**无 Bearer 前缀**)
   ⇒ core 的 auth::downstream_key 认不出来。
   core 是 ipass-all / ipass-sso 的 LLM / apix 共用口径,改它爆炸半径太大
   ⇒ 插件内新增 src/key.rs:core 的三种携带方式 + 裸 Authorization
   (已排除 Basic/Token/Digest/Negotiate 前缀)。
   取到的值**仍须过 auth::identity()**(比对 shm 的 ukey: 索引)
   ⇒ 不构成任何授权绕过。
2. 客户端不带凭据去拉索引 ⇒ 网关 401,而 cargo 不会重试。
   修法在网关自发的 config.json 里:加 "auth-required": true,
   cargo 才会给**每个**请求(索引文件 + .crate 下载)附 token
   (cargo 0.100.0 http_remote.rs:522-532 / download.rs:67)。
3. 换源链每跳会重跑 ACCESS 阶段 ⇒ 裸 sk_ 必须与 Bearer 形态同等处理:
   cache.rs 的「客户端 Authorization 是不是本网关的 sk_」改用同一个解析器,
   否则裸 token 会被透传给上游源。

另:401 改为插件自己发(core 的 http::fail 硬编码 &[],加不了响应头),
带 WWW-Authenticate: Bearer realm="ipass-cache";
nginx 侧那三处 add_header 已删除(会叠加成重复头,且对 200 也生效)。

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
adminandClaude Code committed 2026-10-31 12:13:03 +08:00
1 parent c83d4c7515
commit fefd57ac7f
4 files changed
+73 -5

No files matched your search

+20 -4
View File
@@ -293,6 +293,21 @@ fn cool_down_failed(r: *mut ngx_http_request_t) {
// access:统一 Key 鉴权 + 上游就绪检查
// ===========================================================================
/// 401 响应:**必须带 `WWW-Authenticate`**,否则客户端不知道凭据怎么带。
///
/// core 的 `http::fail` 硬编码 `&[]`(加不了响应头)⇒ 这里自己拼。
/// cargo 不依赖这个头(它靠 config.json 里的 `auth-required` 决定是否附带 token),
/// 但 curl / npm / pip 这类客户端靠它触发「带凭据重试」。
fn fail_401(r: *mut ngx_http_request_t, msg: &str) -> Status {
let body = serde_json::json!({ "code": 401, "msg": msg }).to_string();
http::send_with(
r,
HTTPStatus::UNAUTHORIZED,
&body,
&[("WWW-Authenticate", "Bearer realm=\"ipass-cache\"")],
)
}
/// 制品缓存 access handler。受管制 URI 由路由表(8 前缀)圈定;
/// R19 禁用 → fail-closed 404(数据面是鉴权后的磁盘读,放行=无鉴权读缓存盘)。
pub struct CacheAccessHandler;
@@ -341,10 +356,11 @@ impl HttpRequestHandler for CacheAccessHandler {
}
// ---- 统一 Key 鉴权(与 LLM / API 代理同一把 sk_、同一份配额)----
let Some(key) = auth::downstream_key(r) else {
return http::fail(
// 携带方式:Authorization: Bearer sk_… / Authorization: sk_…(cargo 裸 token)
// / x-api-key: sk_… / api-key: sk_…
let Some(key) = crate::key::downstream_key(r) else {
return fail_401(
r,
HTTPStatus::UNAUTHORIZED,
"缺少 API Key,请在 Authorization: Bearer 或 x-api-key 中携带",
);
};
@@ -353,7 +369,7 @@ impl HttpRequestHandler for CacheAccessHandler {
"[cache] API Key 校验失败 route={route} client={}",
http::remote_addr(r)
));
return http::fail(r, HTTPStatus::UNAUTHORIZED, "API Key 无效或已失效");
return fail_401(r, "API Key 无效或已失效");
};
// ⚠️ 换源链的每一跳都会**重跑 ACCESS 阶段**(nginx 1.30.4 的