[Feature] Enable FastDeploy to support adding the “--api-key” authentication parameter. (#4806)

* add api key initial commit

* add unit test

* modify unit test

* move middleware to a single file and add unit tests
This commit is contained in:
kxz2002
2025-11-08 18:24:02 +08:00
committed by GitHub
parent 80aedb82ce
commit 87911b7cf1
7 changed files with 454 additions and 2 deletions

View File

@@ -1,5 +1,4 @@
"""
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
"""# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
@@ -38,6 +37,7 @@ from fastdeploy.engine.engine import LLMEngine
from fastdeploy.engine.expert_service import ExpertService
from fastdeploy.entrypoints.chat_utils import load_chat_template
from fastdeploy.entrypoints.engine_client import EngineClient
from fastdeploy.entrypoints.openai.middleware import AuthenticationMiddleware
from fastdeploy.entrypoints.openai.protocol import (
ChatCompletionRequest,
ChatCompletionResponse,
@@ -266,6 +266,12 @@ app.add_exception_handler(Exception, ExceptionHandler.handle_exception)
instrument(app)
env_api_key_func = environment_variables.get("FD_API_KEY")
env_tokens = env_api_key_func() if env_api_key_func else []
if tokens := [key for key in (args.api_key or env_tokens) if key]:
app.add_middleware(AuthenticationMiddleware, tokens)
@asynccontextmanager
async def connection_manager():
"""