feat(bot,wizard): smart message filtering, encrypted standalone wizard, and file persistence
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
"""HMAC-SHA256 token verification for standalone draft review."""
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import time
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
def _b64url_encode(data: bytes) -> str:
|
||||
return base64.urlsafe_b64encode(data).rstrip(b"=").decode("ascii")
|
||||
|
||||
|
||||
def _b64url_decode(s: str) -> bytes:
|
||||
padding = 4 - (len(s) % 4)
|
||||
if padding != 4:
|
||||
s += "=" * padding
|
||||
return base64.urlsafe_b64decode(s.encode("ascii"))
|
||||
|
||||
|
||||
def _get_secret(secret: str | None = None) -> str:
|
||||
if secret:
|
||||
return secret
|
||||
try:
|
||||
from app.config import settings
|
||||
return settings.DRAFT_TOKEN_SECRET
|
||||
except Exception:
|
||||
return "procurement-draft-secret-change-in-prod-v7"
|
||||
|
||||
|
||||
def create_draft_token(
|
||||
draft_id: str | UUID,
|
||||
chat_id: int | None = None,
|
||||
user_id: str | None = None,
|
||||
secret: str | None = None,
|
||||
expires_in: int = 7200,
|
||||
) -> str:
|
||||
sec = _get_secret(secret)
|
||||
now = int(time.time())
|
||||
payload = {
|
||||
"draft_id": str(draft_id),
|
||||
"chat_id": chat_id,
|
||||
"user_id": str(user_id) if user_id else None,
|
||||
"iat": now,
|
||||
"exp": now + expires_in,
|
||||
}
|
||||
payload_bytes = json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
|
||||
payload_b64 = _b64url_encode(payload_bytes)
|
||||
|
||||
sig = hmac.new(sec.encode("utf-8"), payload_b64.encode("ascii"), hashlib.sha256).digest()
|
||||
sig_b64 = _b64url_encode(sig)
|
||||
|
||||
return f"{payload_b64}.{sig_b64}"
|
||||
|
||||
|
||||
def verify_draft_token(
|
||||
token: str,
|
||||
secret: str | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Verify signature and expiration of a draft token. Return payload or None."""
|
||||
if not token or "." not in token:
|
||||
return None
|
||||
try:
|
||||
sec = _get_secret(secret)
|
||||
payload_b64, sig_b64 = token.split(".", 1)
|
||||
expected_sig = hmac.new(sec.encode("utf-8"), payload_b64.encode("ascii"), hashlib.sha256).digest()
|
||||
actual_sig = _b64url_decode(sig_b64)
|
||||
|
||||
if not hmac.compare_digest(expected_sig, actual_sig):
|
||||
return None
|
||||
|
||||
payload_bytes = _b64url_decode(payload_b64)
|
||||
payload = json.loads(payload_bytes.decode("utf-8"))
|
||||
|
||||
exp = payload.get("exp")
|
||||
if exp is not None and int(exp) < int(time.time()):
|
||||
return None
|
||||
|
||||
return payload
|
||||
except Exception:
|
||||
return None
|
||||
Reference in New Issue
Block a user