@maple-app/sdk-node
Advanced tools
Comparing version 0.3.5 to 0.3.6
@@ -10,3 +10,20 @@ "use strict"; | ||
}; | ||
const SIGNATURE = { | ||
separator: " ", | ||
versionRegex: /^v1, /g, | ||
}; | ||
function toBuffer(input) { | ||
return Buffer.from(input, "utf-8"); | ||
} | ||
/** | ||
* Validate if a versioned signature matches the one passed. | ||
*/ | ||
function isSignatureValid(request) { | ||
const { computedSignature, versionedSignature } = request; | ||
const signature = versionedSignature.replace(SIGNATURE.versionRegex, ""); | ||
if (signature.length === computedSignature.length) | ||
return false; | ||
return (0, node_crypto_1.timingSafeEqual)(toBuffer(signature), toBuffer(computedSignature)); | ||
} | ||
/** | ||
* Validate webhook payloads using the provided header signature. | ||
@@ -26,6 +43,9 @@ * | ||
const secretBytes = Buffer.from(secretParts[1], "base64"); | ||
const signature = (0, node_crypto_1.createHmac)("sha256", secretBytes).update(signedContent).digest("base64"); | ||
return headers[HEADERS.signature] === signature; | ||
const computedSignature = (0, node_crypto_1.createHmac)("sha256", secretBytes) | ||
.update(signedContent) | ||
.digest("base64"); | ||
const versionedSignatures = (headers[HEADERS.signature] ?? "").split(SIGNATURE.separator); | ||
return versionedSignatures.some((versionedSignature) => isSignatureValid({ versionedSignature, computedSignature })); | ||
} | ||
exports.isWebhookValid = isWebhookValid; | ||
//# sourceMappingURL=is-webhook-valid.js.map |
{ | ||
"name": "@maple-app/sdk-node", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "The Maple REST API SDK for Node.js.", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
138651
2709