UNPKG

@davidbolaji/termii-node

Version:

Node.js SDK for Termii API – send SMS, voice, OTP, and manage messaging with ease.

44 lines (43 loc) 1.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventService = void 0; const crypto_1 = __importDefault(require("crypto")); /** * Service for verifying and handling Termii webhook events */ class EventService { constructor() { } /** * Verify that an incoming webhook event really came from Termii * * @param rawPayload - The raw request body (as a string) * @param signature - The "X-Termii-Signature" header from the request * @param secretKey - The Termii secretkey * @returns true if the signature is valid, false otherwise */ verifySignature(rawPayload, signature, secretKey) { const computed = crypto_1.default .createHmac("sha512", secretKey) .update(rawPayload) .digest("hex"); const computedBuf = Buffer.from(computed, "hex"); const receivedBuf = Buffer.from(signature, "hex"); if (computedBuf.length !== receivedBuf.length) { return false; // avoid RangeError } return crypto_1.default.timingSafeEqual(computedBuf, receivedBuf); } /** * Parse the incoming webhook event into a strongly typed object * * @param payload - Parsed JSON body of the webhook * @returns Expanded TermiiEvent with event details */ parseEvent(payload) { return payload; } } exports.EventService = EventService;