@davidbolaji/termii-node
Version:
Node.js SDK for Termii API – send SMS, voice, OTP, and manage messaging with ease.
37 lines (36 loc) • 1.24 kB
JavaScript
import crypto from "crypto";
/**
* Service for verifying and handling Termii webhook events
*/
export 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
.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.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;
}
}