Skip to content

Commit 816d293

Browse files
committed
fix: 修复 /v1/messages 端点返回 Anthropic 标准错误格式
- 新增 verify_api_key_anthropic 函数,返回 Anthropic 标准错误格式 - 更新 /v1/messages、/:selector/v1/messages、/api/provider/:provider/v1/messages 端点使用新认证函数 - 优先检查 x-api-key header(Anthropic 标准) - 错误响应格式: {"type": "error", "error": {"type": "authentication_error", "message": "..."}} Fixes #3
1 parent a504ec8 commit 816d293

6 files changed

Lines changed: 53 additions & 6 deletions

File tree

166 KB
Loading
169 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "proxycast",
33
"private": true,
4-
"version": "0.12.0",
4+
"version": "0.12.1",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "proxycast"
3-
version = "0.12.0"
3+
version = "0.12.1"
44
description = "AI API Proxy Desktop App"
55
authors = ["you"]
66
edition = "2021"

src-tauri/src/server.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,50 @@ async fn verify_api_key(
877877
Ok(())
878878
}
879879

880+
/// Anthropic 格式的 API key 验证
881+
/// 返回 Anthropic 标准错误格式:{"type": "error", "error": {"type": "...", "message": "..."}}
882+
async fn verify_api_key_anthropic(
883+
headers: &HeaderMap,
884+
expected_key: &str,
885+
) -> Result<(), (StatusCode, Json<serde_json::Value>)> {
886+
let auth = headers
887+
.get("x-api-key")
888+
.or_else(|| headers.get("authorization"))
889+
.and_then(|v| v.to_str().ok());
890+
891+
let key = match auth {
892+
Some(s) if s.starts_with("Bearer ") => &s[7..],
893+
Some(s) => s,
894+
None => {
895+
return Err((
896+
StatusCode::UNAUTHORIZED,
897+
Json(serde_json::json!({
898+
"type": "error",
899+
"error": {
900+
"type": "authentication_error",
901+
"message": "No API key provided. Please set the x-api-key header."
902+
}
903+
})),
904+
))
905+
}
906+
};
907+
908+
if key != expected_key {
909+
return Err((
910+
StatusCode::UNAUTHORIZED,
911+
Json(serde_json::json!({
912+
"type": "error",
913+
"error": {
914+
"type": "authentication_error",
915+
"message": "Invalid API key"
916+
}
917+
})),
918+
));
919+
}
920+
921+
Ok(())
922+
}
923+
880924
async fn chat_completions(
881925
State(state): State<AppState>,
882926
headers: HeaderMap,
@@ -1293,7 +1337,8 @@ async fn anthropic_messages(
12931337
headers: HeaderMap,
12941338
Json(mut request): Json<AnthropicMessagesRequest>,
12951339
) -> Response {
1296-
if let Err(e) = verify_api_key(&headers, &state.api_key).await {
1340+
// 使用 Anthropic 格式的认证验证(优先检查 x-api-key)
1341+
if let Err(e) = verify_api_key_anthropic(&headers, &state.api_key).await {
12971342
state
12981343
.logs
12991344
.write()
@@ -2276,7 +2321,8 @@ async fn anthropic_messages_with_selector(
22762321
headers: HeaderMap,
22772322
Json(request): Json<AnthropicMessagesRequest>,
22782323
) -> Response {
2279-
if let Err(e) = verify_api_key(&headers, &state.api_key).await {
2324+
// 使用 Anthropic 格式的认证验证
2325+
if let Err(e) = verify_api_key_anthropic(&headers, &state.api_key).await {
22802326
state.logs.write().await.add(
22812327
"warn",
22822328
&format!("Unauthorized request to /{}/v1/messages", selector),
@@ -2523,7 +2569,8 @@ async fn amp_messages(
25232569
headers: HeaderMap,
25242570
Json(mut request): Json<AnthropicMessagesRequest>,
25252571
) -> Response {
2526-
if let Err(e) = verify_api_key(&headers, &state.api_key).await {
2572+
// 使用 Anthropic 格式的认证验证
2573+
if let Err(e) = verify_api_key_anthropic(&headers, &state.api_key).await {
25272574
state.logs.write().await.add(
25282575
"warn",
25292576
&format!(

src-tauri/tauri.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://schema.tauri.app/config/2",
33
"productName": "ProxyCast",
4-
"version": "0.12.0",
4+
"version": "0.12.1",
55
"identifier": "com.proxycast.app",
66
"build": {
77
"beforeDevCommand": "npm run dev",

0 commit comments

Comments
 (0)