devme commited on
Commit
4071d32
·
verified ·
1 Parent(s): 758c59c

Upload 10 files

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -226,16 +226,25 @@ def _oidc_headers() -> Dict[str, str]:
226
  # 认证中间件
227
  # ------------------------------------------------------------------------------
228
 
229
- async def auth_middleware(authorization: Optional[str] = Header(default=None)) -> Dict[str, Any]:
 
 
 
230
  """
231
- 认证中间件: 解析 Bearer token 并返回账户信息
232
- Bearer token 格式: clientId:clientSecret:refreshToken
233
  """
234
- if not authorization or not authorization.startswith("Bearer "):
235
- raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
236
 
237
- bearer_token = authorization[7:] # 移除 "Bearer " 前缀
238
- token_hash = _sha256(bearer_token)
 
 
 
 
 
 
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
- # 解析 bearer token
250
- client_id, client_secret, refresh_token = _parse_bearer_token(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(authorization: Optional[str] = Header(default=None)) -> Dict[str, Any]:
281
- return await auth_middleware(authorization)
 
 
 
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