56 lines
1.5 KiB
Python
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()
|