Uplaćeno Docs
sr en

API · Webhookovi Beta

Provera potpisa

Zaglavlje X-Uplaceno-Potpis: t=<unix>,v1=<hex> (pri rotaciji tajne mogu biti dva v1=). Potpis = HMAC-SHA256 tajnom endpointa nad stringom "<t>.<sirovo telo>" (telo tačno kao primljeno bajtovima).

Pravila prijema

  • Odbacite t stariji od 5 minuta (skew dozvoljen malom tolerancijom).
  • Za isti id_dogadjaja prihvatite obradu samo jednom (idempotentnost); ponovni HTTP pokušaji nose isti ID.
  • Odgovorite brzo 2xx i obradite asinhrono; redosled događaja nije zagarantovan.

Node.js

import crypto from "node:crypto";

function verifySignature(rawBody, header, secrets) {
  const parts = Object.fromEntries(
    header.split(",").map((p) => p.trim().split("=").map((s) => s.trim()))
  );
  const t = Number(parts.t);
  if (!t || Date.now() / 1000 - t > 300) return false;
  const v1s = header.match(/v1=([0-9a-f]+)/gi)?.map((x) => x.slice(3)) ?? [];
  const signed = `${t}.${rawBody}`;
  return v1s.some((hex) =>
    secrets.some((secret) => {
      const expected = crypto.createHmac("sha256", secret).update(signed, "utf8").digest("hex");
      return expected.length === hex.length && crypto.timingSafeEqual(Buffer.from(expected, "hex"), Buffer.from(hex, "hex"));
    })
  );
}

Python

import hmac, hashlib, re, time

def verify_signature(raw_body: bytes, header: str, secrets: list[str]) -> bool:
    t_match = re.search(r"t=(\d+)", header)
    if not t_match or time.time() - int(t_match.group(1)) > 300:
        return False
    t = t_match.group(1).encode()
    payload = t + b"." + raw_body
    v1s = [m.group(1) for m in re.finditer(r"v1=([0-9a-f]+)", header)]
    for hex_sig in v1s:
        sig = bytes.fromhex(hex_sig)
        for secret in secrets:
            expected = hmac.new(secret.encode(), payload, hashlib.sha256).digest()
            if hmac.compare_digest(expected, sig):
                return True
    return False

PHP

function verify_uplaceno_signature(string $raw, string $header, array $secrets): bool {
    if (!preg_match('/t=(\d+)/', $header, $m) || time() - (int)$m[1] > 300) {
        return false;
    }
    $t = $m[1];
    $signed = $t . '.' . $raw;
    preg_match_all('/v1=([0-9a-f]+)/', $header, $v1s);
    foreach ($v1s[1] as $hex) {
        $sig = hex2bin($hex);
        foreach ($secrets as $secret) {
            $expected = hash_hmac('sha256', $signed, $secret, true);
            if ($expected !== false && hash_equals($expected, $sig)) {
                return true;
            }
        }
    }
    return false;
}