[,v1=] // where each v1 is the HMAC-SHA256 of "." keyed with your signing secret (whsec_...). // During a secret rotation the header carries two v1 values for 24 hours; accept the request if any matches. // Check the RAW body, byte for byte, before parsing it as JSON. const BAATZO_TOLERANCE_SECONDS = 300; /** True when $header is a valid Baatzo signature of $rawBody, made at most 5 minutes from now. */ function verify_baatzo_signature(string $rawBody, ?string $header, string $secret, ?int $now = null): bool { if ($header === null || $secret === '') { return false; } $t = null; $signatures = []; foreach (explode(',', $header) as $part) { [$k, $v] = array_map('trim', array_pad(explode('=', $part, 2), 2, '')); if ($k === 't' && preg_match('/^[0-9]{1,12}$/D', $v)) { $t = (int) $v; } if ($k === 'v1' && preg_match('/^[0-9a-f]{64}$/D', $v)) { $signatures[] = $v; } } if ($t === null || $signatures === []) { return false; } if (abs(($now ?? time()) - $t) > BAATZO_TOLERANCE_SECONDS) { return false; } $expected = hash_hmac('sha256', $t . '.' . $rawBody, $secret); foreach ($signatures as $signature) { if (hash_equals($expected, $signature)) { return true; } } return false; } // A webhook script: php://input is the raw body. // // $raw = file_get_contents('php://input'); // if (!verify_baatzo_signature($raw, $_SERVER['HTTP_BAATZO_SIGNATURE'] ?? null, getenv('BAATZO_WEBHOOK_SECRET'))) { // http_response_code(400); // exit; // } // $event = json_decode($raw, true); // // Drop a repeat by $event['id']; ignore a status older than one you already have (status_rank). // http_response_code(200); // answer within 5 seconds, then do the work