Upload 10 files
Browse files
app.py
CHANGED
|
@@ -226,16 +226,25 @@ def _oidc_headers() -> Dict[str, str]:
|
|
| 226 |
# 认证中间件
|
| 227 |
# ------------------------------------------------------------------------------
|
| 228 |
|
| 229 |
-
async def auth_middleware(
|
|
|
|
|
|
|
|
|
|
| 230 |
"""
|
| 231 |
-
认证中间件:
|
| 232 |
-
|
| 233 |
"""
|
| 234 |
-
|
| 235 |
-
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
# 检查缓存
|
| 241 |
if token_hash in TOKEN_MAP:
|
|
@@ -246,8 +255,8 @@ async def auth_middleware(authorization: Optional[str] = Header(default=None)) -
|
|
| 246 |
"refreshToken": TOKEN_MAP[token_hash]["refreshToken"],
|
| 247 |
}
|
| 248 |
|
| 249 |
-
# 解析
|
| 250 |
-
client_id, client_secret, refresh_token = _parse_bearer_token(
|
| 251 |
|
| 252 |
if not client_id or not client_secret or not refresh_token:
|
| 253 |
raise HTTPException(status_code=401, detail="Invalid token format. Expected: clientId:clientSecret:refreshToken")
|
|
@@ -277,8 +286,11 @@ async def auth_middleware(authorization: Optional[str] = Header(default=None)) -
|
|
| 277 |
# Dependencies
|
| 278 |
# ------------------------------------------------------------------------------
|
| 279 |
|
| 280 |
-
async def require_account(
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
# ------------------------------------------------------------------------------
|
| 284 |
# Root endpoint
|
|
|
|
| 226 |
# 认证中间件
|
| 227 |
# ------------------------------------------------------------------------------
|
| 228 |
|
| 229 |
+
async def auth_middleware(
|
| 230 |
+
authorization: Optional[str] = Header(default=None),
|
| 231 |
+
x_api_key: Optional[str] = Header(default=None, alias="x-api-key")
|
| 232 |
+
) -> Dict[str, Any]:
|
| 233 |
"""
|
| 234 |
+
认证中间件: 支持 OpenAI Bearer token 和 Claude x-api-key
|
| 235 |
+
Token 格式: clientId:clientSecret:refreshToken
|
| 236 |
"""
|
| 237 |
+
# 优先使用 x-api-key (Claude 格式)
|
| 238 |
+
token = x_api_key if x_api_key else None
|
| 239 |
|
| 240 |
+
# 如果没有 x-api-key,尝试从 Authorization header 获取 (OpenAI 格式)
|
| 241 |
+
if not token and authorization and authorization.startswith("Bearer "):
|
| 242 |
+
token = authorization[7:]
|
| 243 |
+
|
| 244 |
+
if not token:
|
| 245 |
+
raise HTTPException(status_code=401, detail="Missing authentication. Provide Authorization header or x-api-key")
|
| 246 |
+
|
| 247 |
+
token_hash = _sha256(token)
|
| 248 |
|
| 249 |
# 检查缓存
|
| 250 |
if token_hash in TOKEN_MAP:
|
|
|
|
| 255 |
"refreshToken": TOKEN_MAP[token_hash]["refreshToken"],
|
| 256 |
}
|
| 257 |
|
| 258 |
+
# 解析 token
|
| 259 |
+
client_id, client_secret, refresh_token = _parse_bearer_token(token)
|
| 260 |
|
| 261 |
if not client_id or not client_secret or not refresh_token:
|
| 262 |
raise HTTPException(status_code=401, detail="Invalid token format. Expected: clientId:clientSecret:refreshToken")
|
|
|
|
| 286 |
# Dependencies
|
| 287 |
# ------------------------------------------------------------------------------
|
| 288 |
|
| 289 |
+
async def require_account(
|
| 290 |
+
authorization: Optional[str] = Header(default=None),
|
| 291 |
+
x_api_key: Optional[str] = Header(default=None, alias="x-api-key")
|
| 292 |
+
) -> Dict[str, Any]:
|
| 293 |
+
return await auth_middleware(authorization, x_api_key)
|
| 294 |
|
| 295 |
# ------------------------------------------------------------------------------
|
| 296 |
# Root endpoint
|