新增企业微信、微博、WhatsApp、Workplace 四个渠道扩展。 企业微信渠道扩展主要模块:config, gateway, webhook, webhook_bot, outbound, streaming, pairing, security, crypto, dedupe, persistent_dedupe, card, directory, events, externalcontact, media, mentions, menu, message, oauth, status 微博渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, passive_reply, broadcast, message, menu, media, subscription, template, status WhatsApp 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, monitor, status Workplace 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, challenge, groups, media, mentions, menu, monitor, persona, quick_reply, signature, subscriptions, template, threading, users, status
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import hashlib
|
|
import random
|
|
import time
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
CODE_TTL_SECONDS = 300
|
|
_code_store: dict[str, tuple[str, float]] = {}
|
|
|
|
|
|
class WorkplacePairing:
|
|
id_label: str = "Workplace User ID"
|
|
|
|
async def generate_code(self, peer_id: str) -> str:
|
|
seed = f"{peer_id}:{int(time.time())}"
|
|
random.seed(int(hashlib.md5(seed.encode()).hexdigest(), 16) % (2**32))
|
|
code = str(random.randint(0, 999999)).zfill(6)
|
|
|
|
_code_store[peer_id] = (code, time.time() + CODE_TTL_SECONDS)
|
|
|
|
logger.info("Workplace pairing code generated for peer_id=%s", peer_id)
|
|
return code
|
|
|
|
async def verify_code(self, peer_id: str, code: str) -> bool:
|
|
entry = _code_store.get(peer_id)
|
|
if entry is None:
|
|
logger.info("Workplace pairing code not found for peer_id=%s", peer_id)
|
|
return False
|
|
|
|
stored_code, expires_at = entry
|
|
if time.time() > expires_at:
|
|
_code_store.pop(peer_id, None)
|
|
logger.info("Workplace pairing code expired for peer_id=%s", peer_id)
|
|
return False
|
|
|
|
result = stored_code == code
|
|
if result:
|
|
_code_store.pop(peer_id, None)
|
|
logger.info("Workplace pairing code verified for peer_id=%s", peer_id)
|
|
|
|
return result
|
|
|
|
def normalize_allow_entry(self, entry: str) -> str:
|
|
return entry.strip()
|