apps/whatsapp_control_api/app/core/security.py
text
from __future__ import annotations
import hashlib
import hmac
def normalize_whatsapp_number(raw: str) -> str:
return "".join(ch for ch in raw if ch.isdigit())
def verify_meta_signature(raw_body: bytes, signature_header: str | None, app_secret: str) -> bool:
if not signature_header or not app_secret:
return False
prefix = "sha256="
if not signature_header.startswith(prefix):
return False
expected = hmac.new(app_secret.encode("utf-8"), raw_body, hashlib.sha256).hexdigest()
provided = signature_header[len(prefix) :]
return hmac.compare_digest(expected, provided)
def hash_nonce(value: str) -> str:
return hashlib.sha256(value.encode("utf-8")).hexdigest()