Skip to content

Commit db98b80

Browse files
committed
Use errorMessages map verifyWebhookEvent errors
1 parent 6a691ef commit db98b80

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

libs/constants/errorMessages.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ export default {
4242
"INVALID_FILE_PATH": { message: "Invalid value for filePath", help: "Pass the full path of the file. For example - /path/to/file.jpg" },
4343
"INVALID_NEW_FILE_NAME": { message: "Invalid value for newFileName. It should be a string.", help: "" },
4444
"INVALID_PURGE_CACHE": { message: "Invalid value for purgeCache. It should be boolean.", help: "" },
45+
// Webhook signature
46+
"VERIFY_WEBHOOK_EVENT_SIGNATURE_INCORRECT": { message: "Incorrect signature", help: "Please pass x-ik-signature header as utf8 string" },
47+
"VERIFY_WEBHOOK_EVENT_SIGNATURE_MISSING": { message: "Signature missing", help: "Please pass x-ik-signature header as utf8 string" },
48+
"VERIFY_WEBHOOK_EVENT_TIMESTAMP_MISSING": { message: "Timestamp missing", help: "Please pass x-ik-signature header as utf8 string" },
49+
"VERIFY_WEBHOOK_EVENT_TIMESTAMP_INVALID": { message: "Timestamp invalid", help: "Please pass x-ik-signature header as utf8 string" },
4550
};

utils/webhook-signature.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import { createHmac } from "crypto";
22
import { isNaN } from "lodash";
3+
import errorMessages from "../libs/constants/errorMessages";
34
import type { WebhookEvent } from "../libs/interfaces";
45

5-
class WebhookSignatureError extends Error {
6-
constructor(message: string) {
7-
super(message);
8-
this.name = "WebhookSignatureError";
9-
}
10-
}
11-
126
/**
137
* @description Enum for Webhook signature item names
148
*/
@@ -51,17 +45,23 @@ const deserializeSignature = (
5145

5246
// parse timestamp
5347
if (timestampString === undefined) {
54-
throw new WebhookSignatureError("Timestamp missing");
48+
throw new Error(
49+
errorMessages.VERIFY_WEBHOOK_EVENT_TIMESTAMP_MISSING.message
50+
);
5551
}
5652
const timestamp = parseInt(timestampString, 10);
5753
if (isNaN(timestamp) || timestamp < 0) {
58-
throw new WebhookSignatureError("Timestamp invalid");
54+
throw new Error(
55+
errorMessages.VERIFY_WEBHOOK_EVENT_TIMESTAMP_INVALID.message
56+
);
5957
}
6058

6159
// parse v1 signature
6260
const v1 = itemMap.find(([key]) => key === SignatureItems.V1)?.[1]; // eg. 'afafafafafaf'
6361
if (v1 === undefined) {
64-
throw new WebhookSignatureError("Signature missing");
62+
throw new Error(
63+
errorMessages.VERIFY_WEBHOOK_EVENT_SIGNATURE_MISSING.message
64+
);
6565
}
6666

6767
return { timestamp, v1 };
@@ -92,7 +92,9 @@ export const verify = (
9292
secret
9393
);
9494
if (v1 !== computedHmac) {
95-
throw new WebhookSignatureError("Incorrect signature");
95+
throw new Error(
96+
errorMessages.VERIFY_WEBHOOK_EVENT_SIGNATURE_INCORRECT.message
97+
);
9698
}
9799
return {
98100
timestamp,

0 commit comments

Comments
 (0)