api_key authenticatication for dashscope server
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from fastapi import FastAPI, HTTPException, Path, Request
|
from fastapi import FastAPI, HTTPException, Path, Request, Depends, Security
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.responses import StreamingResponse, JSONResponse
|
from fastapi.responses import StreamingResponse, JSONResponse
|
||||||
|
from fastapi.security import APIKeyHeader
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
import os
|
import os
|
||||||
@@ -21,6 +22,18 @@ pipeline_config = tyro.cli(PipelineConfig)
|
|||||||
logger.info(f"starting agent with pipeline: \n{pipeline_config}")
|
logger.info(f"starting agent with pipeline: \n{pipeline_config}")
|
||||||
pipeline:Pipeline = pipeline_config.setup()
|
pipeline:Pipeline = pipeline_config.setup()
|
||||||
|
|
||||||
|
# API Key Authentication
|
||||||
|
API_KEY_HEADER = APIKeyHeader(name="Authorization", auto_error=True)
|
||||||
|
VALID_API_KEYS = set(filter(None, os.environ.get("FAST_AUTH_KEYS", "").split(",")))
|
||||||
|
|
||||||
|
|
||||||
|
async def verify_api_key(api_key: str = Security(API_KEY_HEADER)):
|
||||||
|
"""Verify the API key from Authorization header (Bearer token format)."""
|
||||||
|
key = api_key[7:] if api_key.startswith("Bearer ") else api_key
|
||||||
|
if VALID_API_KEYS and key not in VALID_API_KEYS:
|
||||||
|
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||||
|
return key
|
||||||
|
|
||||||
|
|
||||||
class DSMessage(BaseModel):
|
class DSMessage(BaseModel):
|
||||||
role: str
|
role: str
|
||||||
@@ -136,6 +149,7 @@ async def application_responses(
|
|||||||
request: Request,
|
request: Request,
|
||||||
app_id: str = Path(...),
|
app_id: str = Path(...),
|
||||||
session_id: str = Path(...),
|
session_id: str = Path(...),
|
||||||
|
_: str = Depends(verify_api_key),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
@@ -220,6 +234,7 @@ async def application_responses(
|
|||||||
async def application_completion(
|
async def application_completion(
|
||||||
request: Request,
|
request: Request,
|
||||||
app_id: str = Path(...),
|
app_id: str = Path(...),
|
||||||
|
_: str = Depends(verify_api_key),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
|
|||||||
Reference in New Issue
Block a user