新增企业微信、微博、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
36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
import random
|
|
import string
|
|
import time
|
|
from collections import defaultdict
|
|
|
|
|
|
class WeComPairing:
|
|
CODE_TTL = 600
|
|
RATE_LIMIT_WINDOW = 60
|
|
RATE_LIMIT_MAX = 5
|
|
|
|
def __init__(self):
|
|
self._codes: dict[str, tuple[str, float]] = {}
|
|
self._rate_limit: dict[str, list[float]] = defaultdict(list)
|
|
|
|
def generate_code(self, peer_id: str) -> str | None:
|
|
now = time.time()
|
|
timestamps = self._rate_limit[peer_id]
|
|
timestamps[:] = [t for t in timestamps if now - t < self.RATE_LIMIT_WINDOW]
|
|
|
|
if len(timestamps) >= self.RATE_LIMIT_MAX:
|
|
return None
|
|
|
|
timestamps.append(now)
|
|
code = "".join(random.choices(string.digits, k=6))
|
|
self._codes[peer_id] = (code, now + self.CODE_TTL)
|
|
return code
|
|
|
|
def verify(self, peer_id: str, code: str) -> bool:
|
|
entry = self._codes.get(peer_id)
|
|
if entry is None:
|
|
return False
|
|
stored_code, expires_at = entry
|
|
del self._codes[peer_id]
|
|
return stored_code == code and time.time() < expires_at
|