ForcePilot/backend/package/yuxi/channel/security/registry.py
Kris bab30f2715
Some checks failed
Deploy VitePress site to Pages / build (push) Has been cancelled
Ruff Format Check / Ruff Format & Lint (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
feat:0715
2026-07-15 12:30:58 +08:00

56 lines
1.5 KiB
Python

"""多渠道网关安全策略检查器注册表。"""
from __future__ import annotations
import hashlib
import json
from dataclasses import dataclass
from typing import TYPE_CHECKING, Protocol, runtime_checkable
from yuxi.channel.plugins.registry_base import GenericRegistry
if TYPE_CHECKING:
from yuxi.channel.message.models import InboundMessage
from yuxi.channel.plugins.protocol import ChannelPlugin
from .policy import SecurityCheckResult
@dataclass
class SecurityContext:
"""安全策略检查器执行上下文。"""
config: dict
config_checker: dict
plugin: ChannelPlugin
inbound: InboundMessage
chat_type: str
resolved_sender_id: str | None
@runtime_checkable
class SecurityChecker(Protocol):
"""安全策略检查器协议。"""
name: str
default_priority: int = 0
async def check(self, ctx: SecurityContext) -> SecurityCheckResult | None: ...
class SecurityCheckerRegistry(GenericRegistry["SecurityChecker"]):
"""安全策略检查器注册表:负责注册、解析、排序与生命周期管理。"""
_CONFIG_KEY = "security_checkers"
_ORDER_FIELD = "priority"
_DEFAULT_ORDER_ATTR = "default_priority"
_ITEM_LABEL = "security_checker"
@staticmethod
def _make_config_hash(config: dict) -> str:
security_checkers = config.get("security_checkers")
if security_checkers is None:
return "__default__"
payload = json.dumps(security_checkers, sort_keys=True, default=str)
return hashlib.sha256(payload.encode("utf-8")).hexdigest()