diff --git a/app/api/endpoints/plugin.py b/app/api/endpoints/plugin.py index ed360963..85f9850c 100644 --- a/app/api/endpoints/plugin.py +++ b/app/api/endpoints/plugin.py @@ -9,7 +9,7 @@ from app import schemas from app.command import Command from app.core.config import settings from app.core.plugin import PluginManager -from app.core.security import verify_apikey, verify_token, verify_apitoken +from app.core.security import verify_apikey, verify_token from app.db.systemconfig_oper import SystemConfigOper from app.db.user_oper import get_current_active_superuser from app.factory import app @@ -68,9 +68,13 @@ def _update_plugin_api_routes(plugin_id: Optional[str], action: str): try: api["path"] = api_path allow_anonymous = api.pop("allow_anonymous", False) + auth_mode = api.pop("auth", "apikey") dependencies = api.setdefault("dependencies", []) - if not allow_anonymous and Depends(verify_apikey) not in dependencies: - dependencies.append(Depends(verify_apikey)) + if not allow_anonymous: + if auth_mode == "bear" and Depends(verify_token) not in dependencies: + dependencies.append(Depends(verify_token)) + elif Depends(verify_apikey) not in dependencies: + dependencies.append(Depends(verify_apikey)) app.add_api_route(**api, tags=["plugin"]) is_modified = True logger.debug(f"Added plugin route: {api_path}") diff --git a/app/core/plugin.py b/app/core/plugin.py index 620df032..6d39fe21 100644 --- a/app/core/plugin.py +++ b/app/core/plugin.py @@ -475,6 +475,8 @@ class PluginManager(metaclass=Singleton): apis = plugin.get_api() or [] for api in apis: api["path"] = f"/{plugin_id}{api['path']}" + if not api.get("auth"): + api["auth"] = "apikey" ret_apis.extend(apis) except Exception as e: logger.error(f"获取插件 {plugin_id} API出错:{str(e)}") diff --git a/app/plugins/__init__.py b/app/plugins/__init__.py index c72cd514..be3c3540 100644 --- a/app/plugins/__init__.py +++ b/app/plugins/__init__.py @@ -99,6 +99,7 @@ class _PluginBase(metaclass=ABCMeta): "path": "/xx", "endpoint": self.xxx, "methods": ["GET", "POST"], + "auth: "apikey", # 鉴权类型:apikey/bear "summary": "API名称", "description": "API说明" }]