$maxAgeSeconds) { json_error('Request expired. Check your system clock.', 401); } // 4. Build the expected signature $body = file_get_contents('php://input'); $payload = $timestamp . '.' . $body; $secret = env('HMAC_SECRET_KEY'); if (!$secret || strlen($secret) < 32) { error_log('FATAL: HMAC_SECRET_KEY is missing or too short in .env'); json_error('Server configuration error', 500); } // 5. Verify using constant-time comparison (prevents timing attacks) if (!Security::verifySignature($payload, $signature, $secret)) { error_log("HMAC verification failed for " . ($_SERVER['REQUEST_URI'] ?? '')); json_error('Invalid request signature', 401); } } }