@withaevum/sdk 1.3.7 → 1.3.9
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/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/webhooks.d.ts +20 -0
- package/dist/webhooks.js +50 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,5 +8,7 @@ export { CalendarAPI } from './calendar';
|
|
|
8
8
|
export { AnalyticsAPI } from './analytics';
|
|
9
9
|
export { AvailabilityAPI } from './availability';
|
|
10
10
|
export { PaymentsAPI } from './payments';
|
|
11
|
+
export { verifyWebhook, WebhookVerificationError } from './webhooks';
|
|
12
|
+
export type { WebhookEvent } from './webhooks';
|
|
11
13
|
export * from './types';
|
|
12
14
|
export * from './errors';
|
package/dist/index.js
CHANGED
|
@@ -9,5 +9,6 @@ export { CalendarAPI } from './calendar';
|
|
|
9
9
|
export { AnalyticsAPI } from './analytics';
|
|
10
10
|
export { AvailabilityAPI } from './availability';
|
|
11
11
|
export { PaymentsAPI } from './payments';
|
|
12
|
+
export { verifyWebhook, WebhookVerificationError } from './webhooks';
|
|
12
13
|
export * from './types';
|
|
13
14
|
export * from './errors';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface WebhookEvent {
|
|
2
|
+
type: string;
|
|
3
|
+
version: string;
|
|
4
|
+
id: string;
|
|
5
|
+
delivery_id: string;
|
|
6
|
+
timestamp: string;
|
|
7
|
+
data: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
export declare class WebhookVerificationError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Verify webhook signature and parse event
|
|
14
|
+
* @param body - Raw request body as string
|
|
15
|
+
* @param signature - Signature from X-Aevum-Signature header
|
|
16
|
+
* @param secret - Webhook secret
|
|
17
|
+
* @returns Parsed webhook event
|
|
18
|
+
* @throws WebhookVerificationError if signature verification fails
|
|
19
|
+
*/
|
|
20
|
+
export declare function verifyWebhook(body: string, signature: string, secret: string): WebhookEvent;
|
package/dist/webhooks.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// packages/sdk/src/webhooks.ts
|
|
2
|
+
// Webhook verification utilities
|
|
3
|
+
import crypto from 'crypto';
|
|
4
|
+
export class WebhookVerificationError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'WebhookVerificationError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Verify webhook signature and parse event
|
|
12
|
+
* @param body - Raw request body as string
|
|
13
|
+
* @param signature - Signature from X-Aevum-Signature header
|
|
14
|
+
* @param secret - Webhook secret
|
|
15
|
+
* @returns Parsed webhook event
|
|
16
|
+
* @throws WebhookVerificationError if signature verification fails
|
|
17
|
+
*/
|
|
18
|
+
export function verifyWebhook(body, signature, secret) {
|
|
19
|
+
if (!body || !signature || !secret) {
|
|
20
|
+
throw new WebhookVerificationError('Missing required parameters: body, signature, or secret');
|
|
21
|
+
}
|
|
22
|
+
// Extract signature from format: sha256=<hex>
|
|
23
|
+
const signatureMatch = signature.match(/^sha256=(.+)$/);
|
|
24
|
+
if (!signatureMatch) {
|
|
25
|
+
throw new WebhookVerificationError('Invalid signature format. Expected format: sha256=<hex>');
|
|
26
|
+
}
|
|
27
|
+
const providedSignature = signatureMatch[1];
|
|
28
|
+
// Compute signature with secret
|
|
29
|
+
const computedSignature = crypto
|
|
30
|
+
.createHmac('sha256', secret)
|
|
31
|
+
.update(body)
|
|
32
|
+
.digest('hex');
|
|
33
|
+
// Compare signatures using constant-time comparison
|
|
34
|
+
if (!crypto.timingSafeEqual(Buffer.from(providedSignature, 'hex'), Buffer.from(computedSignature, 'hex'))) {
|
|
35
|
+
throw new WebhookVerificationError('Invalid webhook signature');
|
|
36
|
+
}
|
|
37
|
+
// Parse and validate event
|
|
38
|
+
let event;
|
|
39
|
+
try {
|
|
40
|
+
event = JSON.parse(body);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
throw new WebhookVerificationError(`Invalid JSON payload: ${error instanceof Error ? error.message : String(error)}`);
|
|
44
|
+
}
|
|
45
|
+
// Validate event structure
|
|
46
|
+
if (!event.type || !event.version || !event.id || !event.delivery_id) {
|
|
47
|
+
throw new WebhookVerificationError('Invalid event structure: missing required fields');
|
|
48
|
+
}
|
|
49
|
+
return event;
|
|
50
|
+
}
|