这是一个批量整理提交,包含以下主要改动: 1. 删除多处冗余的空行和未使用的导入 2. 修复文件末尾缺少换行符的问题 3. 调整部分模块的导入顺序与代码排版 4. 修复部分配置默认值与策略逻辑 5. 新增多个功能模块与辅助工具 6. 完善异常处理与日志记录 7. 修复速率限制、消息缓存、权限校验等逻辑bug 8. 废弃部分旧有API与配置项并添加警告提示
95 lines
3.1 KiB
Python
95 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import secrets
|
|
import time
|
|
from dataclasses import dataclass, field
|
|
|
|
from yuxi.channels.adapters.slack.security import SecurityDecision
|
|
|
|
|
|
def _normalize_code(code: str) -> str:
|
|
for prefix in ("slack:", "Slack:", "SLACK:"):
|
|
if code.startswith(prefix):
|
|
return code[len(prefix) :]
|
|
return code
|
|
|
|
|
|
@dataclass
|
|
class PendingPairing:
|
|
user_id: str
|
|
pairing_code: str
|
|
created_at: float = field(default_factory=time.monotonic)
|
|
|
|
def is_expired(self, ttl_seconds: float = 300.0) -> bool:
|
|
return time.monotonic() - self.created_at > ttl_seconds
|
|
|
|
|
|
class PairingManager:
|
|
def __init__(self, ttl_seconds: float = 300.0):
|
|
self._ttl = ttl_seconds
|
|
self._pending: dict[str, PendingPairing] = {}
|
|
self._approved_users: set[str] = set()
|
|
self._lock = asyncio.Lock()
|
|
|
|
async def generate_pairing(self, user_id: str) -> SecurityDecision:
|
|
async with self._lock:
|
|
if user_id in self._approved_users:
|
|
return SecurityDecision(allowed=True, reason="user_approved")
|
|
|
|
existing = self._pending.get(user_id)
|
|
if existing and not existing.is_expired(self._ttl):
|
|
return SecurityDecision(
|
|
allowed=False,
|
|
requires_pairing=True,
|
|
pairing_code=existing.pairing_code,
|
|
reason="pending_pairing_exists",
|
|
)
|
|
|
|
code = secrets.token_hex(3).upper()
|
|
pairing = PendingPairing(user_id=user_id, pairing_code=code)
|
|
self._pending[user_id] = pairing
|
|
|
|
return SecurityDecision(
|
|
allowed=False,
|
|
requires_pairing=True,
|
|
pairing_code=code,
|
|
reason="pairing_required",
|
|
)
|
|
|
|
async def approve(self, pairing_code: str) -> str | None:
|
|
async with self._lock:
|
|
normalized = _normalize_code(pairing_code)
|
|
for user_id, pairing in list(self._pending.items()):
|
|
if pairing.pairing_code == normalized:
|
|
if pairing.is_expired(self._ttl):
|
|
self._pending.pop(user_id, None)
|
|
return None
|
|
self._approved_users.add(user_id)
|
|
self._pending.pop(user_id, None)
|
|
return user_id
|
|
return None
|
|
|
|
def is_approved(self, user_id: str) -> bool:
|
|
return user_id in self._approved_users
|
|
|
|
async def clear_expired(self) -> None:
|
|
async with self._lock:
|
|
for user_id, pairing in list(self._pending.items()):
|
|
if pairing.is_expired(self._ttl):
|
|
self._pending.pop(user_id, None)
|
|
|
|
async def clear_all(self) -> None:
|
|
async with self._lock:
|
|
self._pending.clear()
|
|
|
|
async def pending_count(self) -> int:
|
|
async with self._lock:
|
|
self._clear_expired_locked()
|
|
return len(self._pending)
|
|
|
|
def _clear_expired_locked(self) -> None:
|
|
for user_id, pairing in list(self._pending.items()):
|
|
if pairing.is_expired(self._ttl):
|
|
self._pending.pop(user_id, None)
|