@lukadriel/wave-sdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/LICENSE +201 -0
- package/NOTICE +2 -0
- package/README.md +523 -2
- package/dist/client/wave-client-options.d.ts +29 -0
- package/dist/client/wave-client-options.d.ts.map +1 -0
- package/dist/client/wave-client-options.js +58 -0
- package/dist/client/wave-client-options.js.map +1 -0
- package/dist/client/wave-client.d.ts +47 -0
- package/dist/client/wave-client.d.ts.map +1 -0
- package/dist/client/wave-client.js +66 -0
- package/dist/client/wave-client.js.map +1 -0
- package/dist/core/auth/authentication.d.ts +8 -0
- package/dist/core/auth/authentication.d.ts.map +1 -0
- package/dist/core/auth/authentication.js +10 -0
- package/dist/core/auth/authentication.js.map +1 -0
- package/dist/core/auth/request-signer.d.ts +26 -0
- package/dist/core/auth/request-signer.d.ts.map +1 -0
- package/dist/core/auth/request-signer.js +39 -0
- package/dist/core/auth/request-signer.js.map +1 -0
- package/dist/core/errors/error-parser.d.ts +25 -0
- package/dist/core/errors/error-parser.d.ts.map +1 -0
- package/dist/core/errors/error-parser.js +198 -0
- package/dist/core/errors/error-parser.js.map +1 -0
- package/dist/core/errors/redact.d.ts +11 -0
- package/dist/core/errors/redact.d.ts.map +1 -0
- package/dist/core/errors/redact.js +20 -0
- package/dist/core/errors/redact.js.map +1 -0
- package/dist/core/errors/wave-api-error.d.ts +116 -0
- package/dist/core/errors/wave-api-error.d.ts.map +1 -0
- package/dist/core/errors/wave-api-error.js +114 -0
- package/dist/core/errors/wave-api-error.js.map +1 -0
- package/dist/core/errors/wave-error.d.ts +27 -0
- package/dist/core/errors/wave-error.d.ts.map +1 -0
- package/dist/core/errors/wave-error.js +22 -0
- package/dist/core/errors/wave-error.js.map +1 -0
- package/dist/core/errors/wave-network-error.d.ts +24 -0
- package/dist/core/errors/wave-network-error.d.ts.map +1 -0
- package/dist/core/errors/wave-network-error.js +24 -0
- package/dist/core/errors/wave-network-error.js.map +1 -0
- package/dist/core/errors/wave-webhook-verification-error.d.ts +18 -0
- package/dist/core/errors/wave-webhook-verification-error.d.ts.map +1 -0
- package/dist/core/errors/wave-webhook-verification-error.js +17 -0
- package/dist/core/errors/wave-webhook-verification-error.js.map +1 -0
- package/dist/core/http/fetch-transport.d.ts +23 -0
- package/dist/core/http/fetch-transport.d.ts.map +1 -0
- package/dist/core/http/fetch-transport.js +34 -0
- package/dist/core/http/fetch-transport.js.map +1 -0
- package/dist/core/http/http-client.d.ts +57 -0
- package/dist/core/http/http-client.d.ts.map +1 -0
- package/dist/core/http/http-client.js +254 -0
- package/dist/core/http/http-client.js.map +1 -0
- package/dist/core/http/http-request.d.ts +35 -0
- package/dist/core/http/http-request.d.ts.map +1 -0
- package/dist/core/http/http-request.js +2 -0
- package/dist/core/http/http-request.js.map +1 -0
- package/dist/core/http/http-response.d.ts +26 -0
- package/dist/core/http/http-response.d.ts.map +1 -0
- package/dist/core/http/http-response.js +23 -0
- package/dist/core/http/http-response.js.map +1 -0
- package/dist/core/http/query.d.ts +14 -0
- package/dist/core/http/query.d.ts.map +1 -0
- package/dist/core/http/query.js +23 -0
- package/dist/core/http/query.js.map +1 -0
- package/dist/core/http/transport.d.ts +44 -0
- package/dist/core/http/transport.d.ts.map +1 -0
- package/dist/core/http/transport.js +2 -0
- package/dist/core/http/transport.js.map +1 -0
- package/dist/core/idempotency/idempotency.d.ts +16 -0
- package/dist/core/idempotency/idempotency.d.ts.map +1 -0
- package/dist/core/idempotency/idempotency.js +27 -0
- package/dist/core/idempotency/idempotency.js.map +1 -0
- package/dist/core/retry/backoff.d.ts +18 -0
- package/dist/core/retry/backoff.d.ts.map +1 -0
- package/dist/core/retry/backoff.js +46 -0
- package/dist/core/retry/backoff.js.map +1 -0
- package/dist/core/retry/retry-policy.d.ts +43 -0
- package/dist/core/retry/retry-policy.d.ts.map +1 -0
- package/dist/core/retry/retry-policy.js +90 -0
- package/dist/core/retry/retry-policy.js.map +1 -0
- package/dist/core/types/common.d.ts +59 -0
- package/dist/core/types/common.d.ts.map +1 -0
- package/dist/core/types/common.js +2 -0
- package/dist/core/types/common.js.map +1 -0
- package/dist/core/types/configuration.d.ts +82 -0
- package/dist/core/types/configuration.d.ts.map +1 -0
- package/dist/core/types/configuration.js +2 -0
- package/dist/core/types/configuration.js.map +1 -0
- package/dist/core/types/json.d.ts +15 -0
- package/dist/core/types/json.d.ts.map +1 -0
- package/dist/core/types/json.js +2 -0
- package/dist/core/types/json.js.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/aggregated-merchants/aggregated-merchant-types.d.ts +82 -0
- package/dist/resources/aggregated-merchants/aggregated-merchant-types.d.ts.map +1 -0
- package/dist/resources/aggregated-merchants/aggregated-merchant-types.js +2 -0
- package/dist/resources/aggregated-merchants/aggregated-merchant-types.js.map +1 -0
- package/dist/resources/aggregated-merchants/aggregated-merchants.d.ts +67 -0
- package/dist/resources/aggregated-merchants/aggregated-merchants.d.ts.map +1 -0
- package/dist/resources/aggregated-merchants/aggregated-merchants.js +111 -0
- package/dist/resources/aggregated-merchants/aggregated-merchants.js.map +1 -0
- package/dist/resources/balance/balance-types.d.ts +18 -0
- package/dist/resources/balance/balance-types.d.ts.map +1 -0
- package/dist/resources/balance/balance-types.js +2 -0
- package/dist/resources/balance/balance-types.js.map +1 -0
- package/dist/resources/balance/balance.d.ts +20 -0
- package/dist/resources/balance/balance.d.ts.map +1 -0
- package/dist/resources/balance/balance.js +28 -0
- package/dist/resources/balance/balance.js.map +1 -0
- package/dist/resources/checkout/checkout-types.d.ts +96 -0
- package/dist/resources/checkout/checkout-types.d.ts.map +1 -0
- package/dist/resources/checkout/checkout-types.js +2 -0
- package/dist/resources/checkout/checkout-types.js.map +1 -0
- package/dist/resources/checkout/checkout.d.ts +66 -0
- package/dist/resources/checkout/checkout.d.ts.map +1 -0
- package/dist/resources/checkout/checkout.js +95 -0
- package/dist/resources/checkout/checkout.js.map +1 -0
- package/dist/resources/payout-batches/payout-batches.d.ts +31 -0
- package/dist/resources/payout-batches/payout-batches.d.ts.map +1 -0
- package/dist/resources/payout-batches/payout-batches.js +36 -0
- package/dist/resources/payout-batches/payout-batches.js.map +1 -0
- package/dist/resources/payouts/payout-types.d.ts +158 -0
- package/dist/resources/payouts/payout-types.d.ts.map +1 -0
- package/dist/resources/payouts/payout-types.js +2 -0
- package/dist/resources/payouts/payout-types.js.map +1 -0
- package/dist/resources/payouts/payouts.d.ts +63 -0
- package/dist/resources/payouts/payouts.d.ts.map +1 -0
- package/dist/resources/payouts/payouts.js +80 -0
- package/dist/resources/payouts/payouts.js.map +1 -0
- package/dist/resources/reconciliation/reconciliation-types.d.ts +86 -0
- package/dist/resources/reconciliation/reconciliation-types.d.ts.map +1 -0
- package/dist/resources/reconciliation/reconciliation-types.js +2 -0
- package/dist/resources/reconciliation/reconciliation-types.js.map +1 -0
- package/dist/resources/reconciliation/reconciliation.d.ts +41 -0
- package/dist/resources/reconciliation/reconciliation.d.ts.map +1 -0
- package/dist/resources/reconciliation/reconciliation.js +81 -0
- package/dist/resources/reconciliation/reconciliation.js.map +1 -0
- package/dist/resources/webhooks/webhook-types.d.ts +159 -0
- package/dist/resources/webhooks/webhook-types.d.ts.map +1 -0
- package/dist/resources/webhooks/webhook-types.js +36 -0
- package/dist/resources/webhooks/webhook-types.js.map +1 -0
- package/dist/resources/webhooks/webhook-verifier.d.ts +104 -0
- package/dist/resources/webhooks/webhook-verifier.d.ts.map +1 -0
- package/dist/resources/webhooks/webhook-verifier.js +188 -0
- package/dist/resources/webhooks/webhook-verifier.js.map +1 -0
- package/package.json +70 -4
- package/src/client/wave-client-options.ts +91 -0
- package/src/client/wave-client.ts +70 -0
- package/src/core/auth/authentication.ts +9 -0
- package/src/core/auth/request-signer.ts +42 -0
- package/src/core/errors/error-parser.ts +258 -0
- package/src/core/errors/redact.ts +20 -0
- package/src/core/errors/wave-api-error.ts +148 -0
- package/src/core/errors/wave-error.ts +32 -0
- package/src/core/errors/wave-network-error.ts +24 -0
- package/src/core/errors/wave-webhook-verification-error.ts +28 -0
- package/src/core/http/fetch-transport.ts +48 -0
- package/src/core/http/http-client.ts +352 -0
- package/src/core/http/http-request.ts +37 -0
- package/src/core/http/http-response.ts +37 -0
- package/src/core/http/query.ts +31 -0
- package/src/core/http/transport.ts +45 -0
- package/src/core/idempotency/idempotency.ts +31 -0
- package/src/core/retry/backoff.ts +61 -0
- package/src/core/retry/retry-policy.ts +106 -0
- package/src/core/types/common.ts +65 -0
- package/src/core/types/configuration.ts +85 -0
- package/src/core/types/json.ts +16 -0
- package/src/index.ts +146 -0
- package/src/resources/aggregated-merchants/aggregated-merchant-types.ts +87 -0
- package/src/resources/aggregated-merchants/aggregated-merchants.ts +171 -0
- package/src/resources/balance/balance-types.ts +19 -0
- package/src/resources/balance/balance.ts +39 -0
- package/src/resources/checkout/checkout-types.ts +102 -0
- package/src/resources/checkout/checkout.ts +144 -0
- package/src/resources/payout-batches/payout-batches.ts +58 -0
- package/src/resources/payouts/payout-types.ts +171 -0
- package/src/resources/payouts/payouts.ts +120 -0
- package/src/resources/reconciliation/reconciliation-types.ts +102 -0
- package/src/resources/reconciliation/reconciliation.ts +116 -0
- package/src/resources/webhooks/webhook-types.ts +222 -0
- package/src/resources/webhooks/webhook-verifier.ts +297 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { WaveConfigurationError } from '../../core/errors/wave-error.js';
|
|
3
|
+
import { WaveWebhookVerificationError } from '../../core/errors/wave-webhook-verification-error.js';
|
|
4
|
+
import type { WebhookEvent } from './webhook-types.js';
|
|
5
|
+
|
|
6
|
+
/** Default replay window for webhook timestamps, matching Wave's five-minute guidance. */
|
|
7
|
+
export const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Input for verifying a signed webhook request.
|
|
11
|
+
*/
|
|
12
|
+
export interface VerifyWebhookSignatureInput {
|
|
13
|
+
/**
|
|
14
|
+
* Exact request body bytes as received.
|
|
15
|
+
*
|
|
16
|
+
* The signature is only valid for the original bytes; a parsed and re-serialized body will not
|
|
17
|
+
* verify.
|
|
18
|
+
*/
|
|
19
|
+
readonly rawBody: string | Uint8Array;
|
|
20
|
+
/** Value of the `Wave-Signature` header. */
|
|
21
|
+
readonly signatureHeader: string | null | undefined;
|
|
22
|
+
/** Webhook signing secret provided by Wave when the endpoint was registered. */
|
|
23
|
+
readonly secret: string;
|
|
24
|
+
/** Allowed age of the timestamp in seconds; defaults to 300. */
|
|
25
|
+
readonly toleranceSeconds?: number;
|
|
26
|
+
/** Current Unix time in seconds; injectable for tests. */
|
|
27
|
+
readonly nowSeconds?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Input for verifying the shared-secret authentication strategy.
|
|
32
|
+
*/
|
|
33
|
+
export interface VerifySharedSecretInput {
|
|
34
|
+
/** Value of the `Authorization` header, expected to be `Bearer <secret>`. */
|
|
35
|
+
readonly authorizationHeader: string | null | undefined;
|
|
36
|
+
/** Webhook shared secret provided by Wave when the endpoint was registered. */
|
|
37
|
+
readonly secret: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface ParsedSignatureHeader {
|
|
41
|
+
readonly timestamp: number;
|
|
42
|
+
readonly signatures: readonly string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Verifies incoming Wave webhook requests.
|
|
47
|
+
*
|
|
48
|
+
* This is local cryptography only; it never calls the Wave API. Use the signing-secret strategy for
|
|
49
|
+
* production, and always verify against the raw request body.
|
|
50
|
+
*/
|
|
51
|
+
export class WaveWebhooks {
|
|
52
|
+
/**
|
|
53
|
+
* Verifies a `Wave-Signature` header.
|
|
54
|
+
*
|
|
55
|
+
* @param input - Raw body, header, secret and optional clock controls.
|
|
56
|
+
* @throws WaveWebhookVerificationError when the header is missing, malformed, stale, or does not
|
|
57
|
+
* match the raw body.
|
|
58
|
+
*/
|
|
59
|
+
verifySignature(input: VerifyWebhookSignatureInput): void {
|
|
60
|
+
const secret = assertSecret(input.secret);
|
|
61
|
+
const toleranceSeconds = input.toleranceSeconds ?? DEFAULT_WEBHOOK_TOLERANCE_SECONDS;
|
|
62
|
+
if (!Number.isFinite(toleranceSeconds) || toleranceSeconds < 0) {
|
|
63
|
+
throw new WaveConfigurationError('toleranceSeconds must be a non-negative finite number.');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const { timestamp, signatures } = parseSignatureHeader(input.signatureHeader);
|
|
67
|
+
const nowSeconds = input.nowSeconds ?? Math.floor(Date.now() / 1000);
|
|
68
|
+
|
|
69
|
+
if (timestamp < nowSeconds - toleranceSeconds) {
|
|
70
|
+
throw new WaveWebhookVerificationError(
|
|
71
|
+
'timestamp-too-old',
|
|
72
|
+
`Webhook timestamp is older than the ${toleranceSeconds}-second tolerance.`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const expected = computeWebhookSignature(secret, timestamp, input.rawBody);
|
|
77
|
+
for (const signature of signatures) {
|
|
78
|
+
if (timingSafeEqualStrings(signature, expected)) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
throw new WaveWebhookVerificationError(
|
|
84
|
+
'no-matching-signature',
|
|
85
|
+
'Webhook signature does not match the raw request body.',
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Verifies a webhook and parses its event envelope.
|
|
91
|
+
*
|
|
92
|
+
* @typeParam TData - Expected event data type; defaults to `unknown`.
|
|
93
|
+
* @param input - Raw body, header, secret and optional clock controls.
|
|
94
|
+
* @returns The parsed event envelope.
|
|
95
|
+
* @throws WaveWebhookVerificationError when verification or parsing fails.
|
|
96
|
+
*/
|
|
97
|
+
constructEvent<TData = unknown>(input: VerifyWebhookSignatureInput): WebhookEvent<TData> {
|
|
98
|
+
this.verifySignature(input);
|
|
99
|
+
|
|
100
|
+
const text =
|
|
101
|
+
typeof input.rawBody === 'string'
|
|
102
|
+
? input.rawBody
|
|
103
|
+
: Buffer.from(input.rawBody).toString('utf8');
|
|
104
|
+
|
|
105
|
+
let parsed: unknown;
|
|
106
|
+
try {
|
|
107
|
+
parsed = JSON.parse(text) as unknown;
|
|
108
|
+
} catch {
|
|
109
|
+
throw new WaveWebhookVerificationError('invalid-payload', 'Webhook body is not valid JSON.');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!isRecord(parsed)) {
|
|
113
|
+
throw new WaveWebhookVerificationError(
|
|
114
|
+
'invalid-payload',
|
|
115
|
+
'Webhook body must be a JSON object.',
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const id = parsed['id'];
|
|
120
|
+
const type = parsed['type'];
|
|
121
|
+
if (typeof id !== 'string' || typeof type !== 'string' || !('data' in parsed)) {
|
|
122
|
+
throw new WaveWebhookVerificationError(
|
|
123
|
+
'invalid-payload',
|
|
124
|
+
'Webhook body must contain string "id", string "type" and a "data" field.',
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return { id, type, data: parsed['data'] as TData };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Verifies the shared-secret authentication strategy.
|
|
133
|
+
*
|
|
134
|
+
* @param input - Authorization header and expected secret.
|
|
135
|
+
* @throws WaveWebhookVerificationError when the header is missing, malformed, or does not match.
|
|
136
|
+
*/
|
|
137
|
+
verifySharedSecret(input: VerifySharedSecretInput): void {
|
|
138
|
+
const secret = assertSecret(input.secret);
|
|
139
|
+
const header = input.authorizationHeader;
|
|
140
|
+
|
|
141
|
+
if (header === null || header === undefined || header.trim().length === 0) {
|
|
142
|
+
throw new WaveWebhookVerificationError(
|
|
143
|
+
'missing-header',
|
|
144
|
+
'Missing Authorization header on webhook request.',
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const match = /^Bearer\s+(.+)$/iu.exec(header.trim());
|
|
149
|
+
const token = match?.[1];
|
|
150
|
+
if (token === undefined) {
|
|
151
|
+
throw new WaveWebhookVerificationError(
|
|
152
|
+
'invalid-format',
|
|
153
|
+
'Webhook Authorization header must use the Bearer scheme.',
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!timingSafeEqualStrings(token, secret)) {
|
|
158
|
+
throw new WaveWebhookVerificationError(
|
|
159
|
+
'no-matching-signature',
|
|
160
|
+
'Webhook shared secret does not match.',
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Computes the documented webhook signature over `timestamp + rawBody`.
|
|
168
|
+
*
|
|
169
|
+
* @param secret - Webhook signing secret.
|
|
170
|
+
* @param timestampSeconds - Timestamp from the `Wave-Signature` header.
|
|
171
|
+
* @param rawBody - Raw request body bytes.
|
|
172
|
+
* @returns Lowercase hexadecimal HMAC-SHA256 digest.
|
|
173
|
+
*/
|
|
174
|
+
export function computeWebhookSignature(
|
|
175
|
+
secret: string,
|
|
176
|
+
timestampSeconds: number,
|
|
177
|
+
rawBody: string | Uint8Array,
|
|
178
|
+
): string {
|
|
179
|
+
const bodyBytes =
|
|
180
|
+
typeof rawBody === 'string' ? Buffer.from(rawBody, 'utf8') : Buffer.from(rawBody);
|
|
181
|
+
return createHmac('sha256', secret)
|
|
182
|
+
.update(String(timestampSeconds), 'utf8')
|
|
183
|
+
.update(bodyBytes)
|
|
184
|
+
.digest('hex');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Parses the documented `t={timestamp},v1={signature}` header.
|
|
189
|
+
*
|
|
190
|
+
* Multiple `v1` signatures are accepted to support Wave's secret-rotation flow.
|
|
191
|
+
*
|
|
192
|
+
* @param header - Header value.
|
|
193
|
+
*/
|
|
194
|
+
export function parseWebhookSignatureHeader(header: string | null | undefined): {
|
|
195
|
+
readonly timestamp: number;
|
|
196
|
+
readonly signatures: readonly string[];
|
|
197
|
+
} {
|
|
198
|
+
const parsed = parseSignatureHeader(header);
|
|
199
|
+
return { timestamp: parsed.timestamp, signatures: parsed.signatures };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Verifies a webhook signature using the default verifier.
|
|
204
|
+
*
|
|
205
|
+
* @param input - Raw body, header, secret and optional clock controls.
|
|
206
|
+
*/
|
|
207
|
+
export function verifyWaveWebhookSignature(input: VerifyWebhookSignatureInput): void {
|
|
208
|
+
new WaveWebhooks().verifySignature(input);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Verifies and parses a webhook event using the default verifier.
|
|
213
|
+
*
|
|
214
|
+
* @typeParam TData - Expected event data type; defaults to `unknown`.
|
|
215
|
+
* @param input - Raw body, header, secret and optional clock controls.
|
|
216
|
+
*/
|
|
217
|
+
export function constructWaveWebhookEvent<TData = unknown>(
|
|
218
|
+
input: VerifyWebhookSignatureInput,
|
|
219
|
+
): WebhookEvent<TData> {
|
|
220
|
+
return new WaveWebhooks().constructEvent<TData>(input);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Verifies the shared-secret strategy using the default verifier.
|
|
225
|
+
*
|
|
226
|
+
* @param input - Authorization header and expected secret.
|
|
227
|
+
*/
|
|
228
|
+
export function verifyWaveWebhookSharedSecret(input: VerifySharedSecretInput): void {
|
|
229
|
+
new WaveWebhooks().verifySharedSecret(input);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function parseSignatureHeader(header: string | null | undefined): ParsedSignatureHeader {
|
|
233
|
+
if (header === null || header === undefined || header.trim().length === 0) {
|
|
234
|
+
throw new WaveWebhookVerificationError(
|
|
235
|
+
'missing-header',
|
|
236
|
+
'Missing Wave-Signature header on webhook request.',
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
let timestamp: number | undefined;
|
|
241
|
+
const signatures: string[] = [];
|
|
242
|
+
|
|
243
|
+
for (const rawPart of header.split(',')) {
|
|
244
|
+
const part = rawPart.trim();
|
|
245
|
+
if (part.startsWith('t=')) {
|
|
246
|
+
const value = part.slice(2);
|
|
247
|
+
if (!/^\d+$/u.test(value)) {
|
|
248
|
+
throw new WaveWebhookVerificationError(
|
|
249
|
+
'invalid-format',
|
|
250
|
+
'Wave-Signature timestamp must be an integer number of seconds.',
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
timestamp = Number(value);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (part.startsWith('v1=')) {
|
|
258
|
+
const value = part.slice(3);
|
|
259
|
+
if (!/^[0-9a-fA-F]{64}$/u.test(value)) {
|
|
260
|
+
throw new WaveWebhookVerificationError(
|
|
261
|
+
'invalid-format',
|
|
262
|
+
'Wave-Signature v1 value must be a 64-character hexadecimal HMAC-SHA256 digest.',
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
signatures.push(value.toLowerCase());
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (timestamp === undefined || signatures.length === 0) {
|
|
270
|
+
throw new WaveWebhookVerificationError(
|
|
271
|
+
'invalid-format',
|
|
272
|
+
'Wave-Signature header must contain a timestamp and at least one v1 signature.',
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return { timestamp, signatures };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function timingSafeEqualStrings(left: string, right: string): boolean {
|
|
280
|
+
const leftBytes = Buffer.from(left, 'utf8');
|
|
281
|
+
const rightBytes = Buffer.from(right, 'utf8');
|
|
282
|
+
if (leftBytes.length !== rightBytes.length) {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
return timingSafeEqual(leftBytes, rightBytes);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function assertSecret(secret: string): string {
|
|
289
|
+
if (typeof secret !== 'string' || secret.trim().length === 0) {
|
|
290
|
+
throw new WaveConfigurationError('Webhook secret must be a non-empty string.');
|
|
291
|
+
}
|
|
292
|
+
return secret;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
296
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
297
|
+
}
|