|
1 | 1 | import { createHmac } from "crypto"; |
2 | 2 | import { isNaN } from "lodash"; |
| 3 | +import errorMessages from "../libs/constants/errorMessages"; |
3 | 4 | import type { WebhookEvent } from "../libs/interfaces"; |
4 | 5 |
|
5 | | -class WebhookSignatureError extends Error { |
6 | | - constructor(message: string) { |
7 | | - super(message); |
8 | | - this.name = "WebhookSignatureError"; |
9 | | - } |
10 | | -} |
11 | | - |
12 | 6 | /** |
13 | 7 | * @description Enum for Webhook signature item names |
14 | 8 | */ |
@@ -51,17 +45,23 @@ const deserializeSignature = ( |
51 | 45 |
|
52 | 46 | // parse timestamp |
53 | 47 | if (timestampString === undefined) { |
54 | | - throw new WebhookSignatureError("Timestamp missing"); |
| 48 | + throw new Error( |
| 49 | + errorMessages.VERIFY_WEBHOOK_EVENT_TIMESTAMP_MISSING.message |
| 50 | + ); |
55 | 51 | } |
56 | 52 | const timestamp = parseInt(timestampString, 10); |
57 | 53 | if (isNaN(timestamp) || timestamp < 0) { |
58 | | - throw new WebhookSignatureError("Timestamp invalid"); |
| 54 | + throw new Error( |
| 55 | + errorMessages.VERIFY_WEBHOOK_EVENT_TIMESTAMP_INVALID.message |
| 56 | + ); |
59 | 57 | } |
60 | 58 |
|
61 | 59 | // parse v1 signature |
62 | 60 | const v1 = itemMap.find(([key]) => key === SignatureItems.V1)?.[1]; // eg. 'afafafafafaf' |
63 | 61 | if (v1 === undefined) { |
64 | | - throw new WebhookSignatureError("Signature missing"); |
| 62 | + throw new Error( |
| 63 | + errorMessages.VERIFY_WEBHOOK_EVENT_SIGNATURE_MISSING.message |
| 64 | + ); |
65 | 65 | } |
66 | 66 |
|
67 | 67 | return { timestamp, v1 }; |
@@ -92,7 +92,9 @@ export const verify = ( |
92 | 92 | secret |
93 | 93 | ); |
94 | 94 | if (v1 !== computedHmac) { |
95 | | - throw new WebhookSignatureError("Incorrect signature"); |
| 95 | + throw new Error( |
| 96 | + errorMessages.VERIFY_WEBHOOK_EVENT_SIGNATURE_INCORRECT.message |
| 97 | + ); |
96 | 98 | } |
97 | 99 | return { |
98 | 100 | timestamp, |
|
0 commit comments