FastComments.com
Here you can edit and run the code snippet from our documentation.
Example Code Snippet "Weryfikacja dostawy"
import { createHmac, timingSafeEqual } from "node:crypto"; async function receive(c) { // Dokładne bajty, które nadeszły. Nie używaj c.req.json() i nie serializuj ponownie. const rawBody = await c.req.raw.text(); const timestamp = c.req.raw.headers.get("X-FastComments-Timestamp"); const signature = c.req.raw.headers.get("X-FastComments-Signature"); if (!timestamp || !signature) return new Response("Missing headers", { status: 400 }); // Odrzuć przestarzałe dostawy, aby przechwycony request nie mógł być odtworzony później. if (Math.abs(Math.floor(Date.now() / 1000) - Number(timestamp)) > 300) { return new Response("Timestamp outside window", { status: 400 }); } const expected = "sha256=" + createHmac("sha256", Deno.env.get("FASTCOMMENTS_API_SECRET")) .update(`${timestamp}.${rawBody}`) .digest("hex"); const a = new TextEncoder().encode(signature); const b = new TextEncoder().encode(expected); if (a.length !== b.length || !timingSafeEqual(a, b)) { return new Response("Signature mismatch", { status: 401 }); } // ...handle JSON.parse(rawBody) return Response.json({ received: true }); } app.put("/", receive); app.delete("/", receive);
Result