ForcePilot/backend/package/yuxi/channel/extensions/yuanbao/pairing.py
Kris 5946478772 feat(channel): 添加小红书、XMPP、元宝和 Zalo 渠道扩展
新增小红书、XMPP、元宝、Zalo 四个渠道扩展。

小红书渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, media, status, window

XMPP 渠道扩展主要模块:plugin, config, gateway, outbound, streaming, pairing, security, dedupe, accounts, commands, muc, rate_limiter, stanza_utils, status, monitor

元宝渠道扩展主要模块:plugin, client, config_schema, gateway, outbound(chunk/queue/transport), inbound(dispatcher), streaming, pairing, security, accounts, actions, commands, codec(biz/conn), session, shared, utils

Zalo 渠道扩展主要模块:api, config, gateway, webhook, outbound, pairing, security, session, polling, monitor, status
2026-05-21 12:04:05 +08:00

48 lines
1.3 KiB
Python

from __future__ import annotations
import logging
import secrets
import time
logger = logging.getLogger(__name__)
PAIRING_CODE_LENGTH = 8
class PairingManager:
def __init__(self, ttl_seconds: int = 3600):
self._ttl_seconds = ttl_seconds
self._codes: dict[str, tuple[str, float]] = {}
def generate_code(self, account_id: str, peer_id: str) -> str:
key = f"{account_id}:{peer_id}"
code = secrets.token_hex(PAIRING_CODE_LENGTH // 2)[:PAIRING_CODE_LENGTH].upper()
self._codes[key] = (code, time.time() + self._ttl_seconds)
return code
def verify_code(self, account_id: str, peer_id: str, code: str) -> bool:
key = f"{account_id}:{peer_id}"
entry = self._codes.get(key)
if entry is None:
return False
stored_code, expiry = entry
if time.time() > expiry:
del self._codes[key]
return False
if not secrets.compare_digest(stored_code.upper(), code.upper()):
return False
del self._codes[key]
return True
def expire_codes(self) -> int:
now = time.time()
expired = [
k for k, (_, exp) in self._codes.items() if now > exp
]
for k in expired:
del self._codes[k]
return len(expired)