feat:插件API支持bear认证

This commit is contained in:
jxxghp
2025-05-07 13:26:42 +08:00
parent 4dc2c18075
commit 0cdea3318c
3 changed files with 10 additions and 3 deletions
+7 -3
View File
@@ -9,7 +9,7 @@ from app import schemas
from app.command import Command from app.command import Command
from app.core.config import settings from app.core.config import settings
from app.core.plugin import PluginManager 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.systemconfig_oper import SystemConfigOper
from app.db.user_oper import get_current_active_superuser from app.db.user_oper import get_current_active_superuser
from app.factory import app from app.factory import app
@@ -68,9 +68,13 @@ def _update_plugin_api_routes(plugin_id: Optional[str], action: str):
try: try:
api["path"] = api_path api["path"] = api_path
allow_anonymous = api.pop("allow_anonymous", False) allow_anonymous = api.pop("allow_anonymous", False)
auth_mode = api.pop("auth", "apikey")
dependencies = api.setdefault("dependencies", []) dependencies = api.setdefault("dependencies", [])
if not allow_anonymous and Depends(verify_apikey) not in dependencies: if not allow_anonymous:
dependencies.append(Depends(verify_apikey)) 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"]) app.add_api_route(**api, tags=["plugin"])
is_modified = True is_modified = True
logger.debug(f"Added plugin route: {api_path}") logger.debug(f"Added plugin route: {api_path}")
+2
View File
@@ -475,6 +475,8 @@ class PluginManager(metaclass=Singleton):
apis = plugin.get_api() or [] apis = plugin.get_api() or []
for api in apis: for api in apis:
api["path"] = f"/{plugin_id}{api['path']}" api["path"] = f"/{plugin_id}{api['path']}"
if not api.get("auth"):
api["auth"] = "apikey"
ret_apis.extend(apis) ret_apis.extend(apis)
except Exception as e: except Exception as e:
logger.error(f"获取插件 {plugin_id} API出错:{str(e)}") logger.error(f"获取插件 {plugin_id} API出错:{str(e)}")
+1
View File
@@ -99,6 +99,7 @@ class _PluginBase(metaclass=ABCMeta):
"path": "/xx", "path": "/xx",
"endpoint": self.xxx, "endpoint": self.xxx,
"methods": ["GET", "POST"], "methods": ["GET", "POST"],
"auth: "apikey", # 鉴权类型:apikey/bear
"summary": "API名称", "summary": "API名称",
"description": "API说明" "description": "API说明"
}] }]