Zəng webhook-ları: imzanı yoxlamaq

Son yeniləmə: 9 oktyabr 2026 · 4 dəq oxunur

Sorğunun həqiqətən Clomni-dən gəldiyini webhook-un gizli açarı ilə yoxlayın. Açar webhook yarananda göstərilir, sonra da webhook-un redaktə pəncərəsində tapılır.

Addımlar

  1. 1

    Başlıqlar

    • X-ChatX-Delivery: göndərişin id-si, gövdədəki delivery_id ilə eyni.
    • X-ChatX-Timestamp: göndəriş anı, Unix saniyəsi.
    • X-ChatX-Signature: sha256= və HMAC-SHA256 imzası.
  2. 2

    İmza necə hesablanır

    İmza <timestamp>.<gövdə> sətrindən webhook-un gizli açarı ilə hesablanır. Gövdəni JSON kimi oxuyub yenidən yazmayın: imzanı gələn xam gövdə ilə yoxlayın. Vaxtı 5 dəqiqədən köhnə olan sorğunu qəbul etməyin. Təkrar göndərişdə vaxt və imza yenidən hesablanır, delivery_id isə dəyişmir.

  3. 3

    Node.js

    const crypto = require('crypto');
    
    function verify(rawBody, headers, secret) {
      const timestamp = headers['x-chatx-timestamp'];
      const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(`${timestamp}.${rawBody}`).digest('hex');
      const given = headers['x-chatx-signature'] || '';
      const fresh = Math.abs(Date.now() / 1000 - Number(timestamp)) < 300;
      return fresh && given.length === expected.length && crypto.timingSafeEqual(Buffer.from(given), Buffer.from(expected));
    }
  4. 4

    Python

    import hashlib, hmac, time
    
    def verify(raw_body: bytes, headers, secret: str) -> bool:
        timestamp = headers["X-ChatX-Timestamp"]
        expected = "sha256=" + hmac.new(secret.encode(), f"{timestamp}.".encode() + raw_body, hashlib.sha256).hexdigest()
        fresh = abs(time.time() - int(timestamp)) < 300
        return fresh and hmac.compare_digest(headers.get("X-ChatX-Signature", ""), expected)

Sənəd