@idealite/web-services
Version:
Comprehensive web services library with webhook system and Mux integration
56 lines (55 loc) • 2.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.MuxWebhookVerifier = void 0;
const mux_node_1 = require("@mux/mux-node");
/**
* Mux-specific webhook signature verifier
* Uses the official Mux Node.js library for signature verification
* Based on https://docs.mux.com/guides/webhooks/verify-webhook-signatures
*/
class MuxWebhookVerifier {
muxClient;
constructor(webhookSecret) {
// Create a Mux client instance with webhook secret
this.muxClient = new mux_node_1.Mux({
tokenId: '', // Not needed for webhook verification
tokenSecret: '', // Not needed for webhook verification
webhookSecret,
});
}
verify(payload, signature, _secret) {
try {
// Use Mux's official webhook verification
// The verifySignature method expects headers object
const headers = { 'mux-signature': signature };
this.muxClient.webhooks.verifySignature(payload, headers);
return true;
}
catch (error) {
console.error('Mux webhook verification failed:', error);
return false;
}
}
/**
* Verify webhook with timestamp for additional security
*/
verifyWithTimestamp(payload, signature, timestamp) {
try {
// Check timestamp is within acceptable window (5 minutes)
const now = Math.floor(Date.now() / 1000);
const webhookTimestamp = parseInt(timestamp, 10);
const timeDiff = Math.abs(now - webhookTimestamp);
if (timeDiff > 300) { // 5 minutes
console.warn('Mux webhook timestamp is too old');
return false;
}
// Verify the signature
return this.verify(payload, signature);
}
catch (error) {
console.error('Mux webhook verification with timestamp failed:', error);
return false;
}
}
}
exports.MuxWebhookVerifier = MuxWebhookVerifier;
;