Update app.py
Browse files
app.py
CHANGED
|
@@ -367,15 +367,32 @@ class ChatRequest(BaseModel):
|
|
| 367 |
model: str = "sonar"
|
| 368 |
history: List[Any] = []
|
| 369 |
|
|
|
|
|
|
|
|
|
|
| 370 |
def verify_api_key(request: Request):
|
|
|
|
| 371 |
auth = request.headers.get("Authorization")
|
| 372 |
-
if
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
-
# ========== المسار الرئيسي (تمت الإضافة هنا) ==========
|
| 379 |
@app.get("/")
|
| 380 |
async def root():
|
| 381 |
return {
|
|
@@ -387,7 +404,7 @@ async def root():
|
|
| 387 |
"POST /chat": "إرسال رسالة والحصول على رد (يتطلب مفتاح)",
|
| 388 |
"POST /chat/stream": "إرسال رسالة والحصول على رد متدفق (يتطلب مفتاح)"
|
| 389 |
},
|
| 390 |
-
"authentication": "Bearer YOUR_API_KEY",
|
| 391 |
"status": "✅ Server is working"
|
| 392 |
}
|
| 393 |
|
|
|
|
| 367 |
model: str = "sonar"
|
| 368 |
history: List[Any] = []
|
| 369 |
|
| 370 |
+
# =====================================================
|
| 371 |
+
# تعديل دالة التحقق لدعم X-API-Key مع الاحتفاظ بـ Bearer
|
| 372 |
+
# =====================================================
|
| 373 |
def verify_api_key(request: Request):
|
| 374 |
+
# الطريقة الأولى: Authorization: Bearer XXX
|
| 375 |
auth = request.headers.get("Authorization")
|
| 376 |
+
if auth and auth.startswith("Bearer "):
|
| 377 |
+
key = auth.replace("Bearer ", "")
|
| 378 |
+
if key == API_KEY:
|
| 379 |
+
return True
|
| 380 |
+
|
| 381 |
+
# الطريقة الثانية: X-API-Key: XXX
|
| 382 |
+
x_api_key = request.headers.get("X-API-Key")
|
| 383 |
+
if x_api_key and x_api_key == API_KEY:
|
| 384 |
+
return True
|
| 385 |
+
|
| 386 |
+
# لا يوجد مفتاح صالح
|
| 387 |
+
raise HTTPException(
|
| 388 |
+
status_code=401,
|
| 389 |
+
detail="Invalid or missing API key. Use 'Authorization: Bearer KEY' or 'X-API-Key: KEY'"
|
| 390 |
+
)
|
| 391 |
+
|
| 392 |
+
# =====================================================
|
| 393 |
+
# نقاط النهاية (بدون تغيير)
|
| 394 |
+
# =====================================================
|
| 395 |
|
|
|
|
| 396 |
@app.get("/")
|
| 397 |
async def root():
|
| 398 |
return {
|
|
|
|
| 404 |
"POST /chat": "إرسال رسالة والحصول على رد (يتطلب مفتاح)",
|
| 405 |
"POST /chat/stream": "إرسال رسالة والحصول على رد متدفق (يتطلب مفتاح)"
|
| 406 |
},
|
| 407 |
+
"authentication": "Bearer YOUR_API_KEY or X-API-Key: YOUR_API_KEY",
|
| 408 |
"status": "✅ Server is working"
|
| 409 |
}
|
| 410 |
|