@replicer/sdk 0.1.1 → 0.2.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/README.md +51 -17
- package/dist/index.cjs +86 -40
- package/dist/index.d.cts +331 -547
- package/dist/index.d.ts +331 -547
- package/dist/index.js +86 -40
- package/dist/types-CmxwElVc.d.cts +932 -0
- package/dist/types-CmxwElVc.d.ts +932 -0
- package/dist/webhooks.cjs +86 -14
- package/dist/webhooks.d.cts +35 -17
- package/dist/webhooks.d.ts +35 -17
- package/dist/webhooks.js +81 -13
- package/package.json +3 -2
package/dist/webhooks.cjs
CHANGED
|
@@ -20,26 +20,98 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/webhooks.ts
|
|
21
21
|
var webhooks_exports = {};
|
|
22
22
|
__export(webhooks_exports, {
|
|
23
|
+
DEFAULT_WEBHOOK_TOLERANCE_SECONDS: () => DEFAULT_WEBHOOK_TOLERANCE_SECONDS,
|
|
24
|
+
WEBHOOK_SIGNATURE_HEADER: () => WEBHOOK_SIGNATURE_HEADER,
|
|
25
|
+
WebhookSignatureError: () => WebhookSignatureError,
|
|
26
|
+
constructEvent: () => constructEvent,
|
|
23
27
|
verifyWebhookSignature: () => verifyWebhookSignature
|
|
24
28
|
});
|
|
25
29
|
module.exports = __toCommonJS(webhooks_exports);
|
|
26
30
|
var import_node_crypto = require("crypto");
|
|
27
|
-
var
|
|
28
|
-
var
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
31
|
+
var WEBHOOK_SIGNATURE_HEADER = "Replicer-Signature";
|
|
32
|
+
var DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
|
|
33
|
+
var WebhookSignatureError = class extends Error {
|
|
34
|
+
constructor(message) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "WebhookSignatureError";
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var HEX_SHA256 = /^[0-9a-f]{64}$/i;
|
|
40
|
+
var UNIX_SECONDS = /^\d{1,12}$/;
|
|
41
|
+
function parseHeader(header) {
|
|
42
|
+
let timestamp = null;
|
|
43
|
+
const signatures = [];
|
|
44
|
+
for (const part of header.split(",")) {
|
|
45
|
+
const eq = part.indexOf("=");
|
|
46
|
+
if (eq <= 0) continue;
|
|
47
|
+
const key = part.slice(0, eq).trim();
|
|
48
|
+
const value = part.slice(eq + 1).trim();
|
|
49
|
+
if (key === "t") {
|
|
50
|
+
if (timestamp !== null || !UNIX_SECONDS.test(value)) return null;
|
|
51
|
+
timestamp = Number(value);
|
|
52
|
+
} else if (key === "v1" && HEX_SHA256.test(value)) {
|
|
53
|
+
signatures.push(Buffer.from(value, "hex"));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (timestamp === null || signatures.length === 0) return null;
|
|
57
|
+
return { timestamp, signatures };
|
|
58
|
+
}
|
|
59
|
+
function assertNoRenamedSignature(input) {
|
|
60
|
+
if (!input || typeof input !== "object") return;
|
|
61
|
+
const fields = input;
|
|
62
|
+
if (fields.header === void 0 && "signature" in fields) {
|
|
63
|
+
throw new TypeError(
|
|
64
|
+
`'signature' was renamed to 'header' in @replicer/sdk 0.2.0: pass the ${WEBHOOK_SIGNATURE_HEADER} header value as 'header'`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function sign(secret, timestamp, payload) {
|
|
69
|
+
return (0, import_node_crypto.createHmac)("sha256", secret).update(`${timestamp}.`).update(payload).digest();
|
|
70
|
+
}
|
|
71
|
+
function verifyWebhookSignature(input) {
|
|
72
|
+
assertNoRenamedSignature(input);
|
|
73
|
+
if (!input || typeof input.header !== "string") return false;
|
|
74
|
+
const payload = input.payload;
|
|
75
|
+
if (typeof payload !== "string" && !(payload instanceof Uint8Array)) return false;
|
|
76
|
+
const secrets = [input.secret, ...Array.isArray(input.secrets) ? input.secrets : []].filter((s) => typeof s === "string" && s !== "");
|
|
77
|
+
if (secrets.length === 0) return false;
|
|
78
|
+
const parsed = parseHeader(input.header);
|
|
79
|
+
if (!parsed) return false;
|
|
80
|
+
const tolerance = typeof input.toleranceSeconds === "number" && Number.isFinite(input.toleranceSeconds) && input.toleranceSeconds >= 0 ? input.toleranceSeconds : DEFAULT_WEBHOOK_TOLERANCE_SECONDS;
|
|
81
|
+
const nowMs = (input.now ?? /* @__PURE__ */ new Date()).getTime();
|
|
82
|
+
if (!Number.isFinite(nowMs)) return false;
|
|
83
|
+
if (Math.abs(nowMs / 1e3 - parsed.timestamp) > tolerance) return false;
|
|
84
|
+
let match = false;
|
|
85
|
+
for (const secret of secrets) {
|
|
86
|
+
const expected = sign(secret, parsed.timestamp, payload);
|
|
87
|
+
for (const received of parsed.signatures) {
|
|
88
|
+
if ((0, import_node_crypto.timingSafeEqual)(expected, received)) match = true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return match;
|
|
92
|
+
}
|
|
93
|
+
function constructEvent(input) {
|
|
94
|
+
assertNoRenamedSignature(input);
|
|
95
|
+
if (!verifyWebhookSignature(input)) {
|
|
96
|
+
throw new WebhookSignatureError("Invalid webhook signature");
|
|
97
|
+
}
|
|
98
|
+
const text = typeof input.payload === "string" ? input.payload : new TextDecoder().decode(input.payload);
|
|
99
|
+
let parsed;
|
|
100
|
+
try {
|
|
101
|
+
parsed = JSON.parse(text);
|
|
102
|
+
} catch {
|
|
103
|
+
throw new WebhookSignatureError("Webhook payload is not valid JSON");
|
|
104
|
+
}
|
|
105
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
106
|
+
throw new WebhookSignatureError("Webhook payload is not a JSON object");
|
|
107
|
+
}
|
|
108
|
+
return parsed;
|
|
41
109
|
}
|
|
42
110
|
// Annotate the CommonJS export names for ESM import in node:
|
|
43
111
|
0 && (module.exports = {
|
|
112
|
+
DEFAULT_WEBHOOK_TOLERANCE_SECONDS,
|
|
113
|
+
WEBHOOK_SIGNATURE_HEADER,
|
|
114
|
+
WebhookSignatureError,
|
|
115
|
+
constructEvent,
|
|
44
116
|
verifyWebhookSignature
|
|
45
117
|
});
|
package/dist/webhooks.d.cts
CHANGED
|
@@ -1,23 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*/
|
|
1
|
+
import { a7 as WebhookEvent } from './types-CmxwElVc.cjs';
|
|
2
|
+
export { a9 as AnyWebhookEvent, aP as WebhookEventDataMap, aR as WebhookEventType } from './types-CmxwElVc.cjs';
|
|
3
|
+
|
|
4
|
+
/** Name of the signature header (HTTP header names are case-insensitive). */
|
|
5
|
+
declare const WEBHOOK_SIGNATURE_HEADER = "Replicer-Signature";
|
|
6
|
+
/** Default accepted distance, in seconds, between the signed timestamp and now. */
|
|
7
|
+
declare const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
|
|
5
8
|
interface VerifyWebhookSignatureInput {
|
|
6
|
-
/** The
|
|
9
|
+
/** The raw request body exactly as received (never re-serialized JSON). */
|
|
7
10
|
payload: string | Uint8Array;
|
|
8
|
-
/** Value of the `
|
|
9
|
-
|
|
10
|
-
/** The endpoint signing secret. */
|
|
11
|
-
secret
|
|
11
|
+
/** Value of the `Replicer-Signature` header: `t=<unix>,v1=<hex>[,v1=<hex>]`. */
|
|
12
|
+
header: string | null | undefined;
|
|
13
|
+
/** The endpoint signing secret (`whsec_…`). One of `secret` / `secrets` is required. */
|
|
14
|
+
secret?: string;
|
|
15
|
+
/** Several secrets, any of which may match (for example the new and the old one during a rotation). */
|
|
16
|
+
secrets?: string[];
|
|
17
|
+
/** Maximum distance between the signed timestamp `t` and now (default 300). */
|
|
18
|
+
toleranceSeconds?: number;
|
|
19
|
+
/** Current time (default `new Date()`), for tests. */
|
|
20
|
+
now?: Date;
|
|
12
21
|
}
|
|
22
|
+
/** Thrown by `constructEvent` for a missing, invalid or expired signature, or a body that is not a JSON event. */
|
|
23
|
+
declare class WebhookSignatureError extends Error {
|
|
24
|
+
constructor(message: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Verify the `Replicer-Signature` header of a webhook delivery: true when any `v1` signature
|
|
28
|
+
* matches any of the given secrets (constant-time compare) and the signed timestamp is within
|
|
29
|
+
* `toleranceSeconds` of now. Returns `false`, never throws, for a missing or malformed header, a
|
|
30
|
+
* stale timestamp, no secret or no match. Throws `TypeError` only for a programmer error: passing
|
|
31
|
+
* the 0.1.0 `signature` field (renamed to `header` in 0.2.0) without `header`.
|
|
32
|
+
*/
|
|
33
|
+
declare function verifyWebhookSignature(input: VerifyWebhookSignatureInput): boolean;
|
|
13
34
|
/**
|
|
14
|
-
* Verify the
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* The `X-Replicer-Timestamp` header is NOT covered by the signature: for replay protection,
|
|
19
|
-
* de-duplicate on `X-Replicer-Delivery-Id` / the event id instead of relying on the timestamp.
|
|
35
|
+
* Verify the signature, then parse the body. Throws `WebhookSignatureError` when the signature is
|
|
36
|
+
* not valid (see `verifyWebhookSignature`) or the body is not a JSON object, and `TypeError` when
|
|
37
|
+
* given the 0.1.0 `signature` field instead of `header`.
|
|
20
38
|
*/
|
|
21
|
-
declare function
|
|
39
|
+
declare function constructEvent<T = Record<string, unknown>>(input: VerifyWebhookSignatureInput): WebhookEvent<T>;
|
|
22
40
|
|
|
23
|
-
export { type VerifyWebhookSignatureInput, verifyWebhookSignature };
|
|
41
|
+
export { DEFAULT_WEBHOOK_TOLERANCE_SECONDS, type VerifyWebhookSignatureInput, WEBHOOK_SIGNATURE_HEADER, WebhookEvent, WebhookSignatureError, constructEvent, verifyWebhookSignature };
|
package/dist/webhooks.d.ts
CHANGED
|
@@ -1,23 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*/
|
|
1
|
+
import { a7 as WebhookEvent } from './types-CmxwElVc.js';
|
|
2
|
+
export { a9 as AnyWebhookEvent, aP as WebhookEventDataMap, aR as WebhookEventType } from './types-CmxwElVc.js';
|
|
3
|
+
|
|
4
|
+
/** Name of the signature header (HTTP header names are case-insensitive). */
|
|
5
|
+
declare const WEBHOOK_SIGNATURE_HEADER = "Replicer-Signature";
|
|
6
|
+
/** Default accepted distance, in seconds, between the signed timestamp and now. */
|
|
7
|
+
declare const DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
|
|
5
8
|
interface VerifyWebhookSignatureInput {
|
|
6
|
-
/** The
|
|
9
|
+
/** The raw request body exactly as received (never re-serialized JSON). */
|
|
7
10
|
payload: string | Uint8Array;
|
|
8
|
-
/** Value of the `
|
|
9
|
-
|
|
10
|
-
/** The endpoint signing secret. */
|
|
11
|
-
secret
|
|
11
|
+
/** Value of the `Replicer-Signature` header: `t=<unix>,v1=<hex>[,v1=<hex>]`. */
|
|
12
|
+
header: string | null | undefined;
|
|
13
|
+
/** The endpoint signing secret (`whsec_…`). One of `secret` / `secrets` is required. */
|
|
14
|
+
secret?: string;
|
|
15
|
+
/** Several secrets, any of which may match (for example the new and the old one during a rotation). */
|
|
16
|
+
secrets?: string[];
|
|
17
|
+
/** Maximum distance between the signed timestamp `t` and now (default 300). */
|
|
18
|
+
toleranceSeconds?: number;
|
|
19
|
+
/** Current time (default `new Date()`), for tests. */
|
|
20
|
+
now?: Date;
|
|
12
21
|
}
|
|
22
|
+
/** Thrown by `constructEvent` for a missing, invalid or expired signature, or a body that is not a JSON event. */
|
|
23
|
+
declare class WebhookSignatureError extends Error {
|
|
24
|
+
constructor(message: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Verify the `Replicer-Signature` header of a webhook delivery: true when any `v1` signature
|
|
28
|
+
* matches any of the given secrets (constant-time compare) and the signed timestamp is within
|
|
29
|
+
* `toleranceSeconds` of now. Returns `false`, never throws, for a missing or malformed header, a
|
|
30
|
+
* stale timestamp, no secret or no match. Throws `TypeError` only for a programmer error: passing
|
|
31
|
+
* the 0.1.0 `signature` field (renamed to `header` in 0.2.0) without `header`.
|
|
32
|
+
*/
|
|
33
|
+
declare function verifyWebhookSignature(input: VerifyWebhookSignatureInput): boolean;
|
|
13
34
|
/**
|
|
14
|
-
* Verify the
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* The `X-Replicer-Timestamp` header is NOT covered by the signature: for replay protection,
|
|
19
|
-
* de-duplicate on `X-Replicer-Delivery-Id` / the event id instead of relying on the timestamp.
|
|
35
|
+
* Verify the signature, then parse the body. Throws `WebhookSignatureError` when the signature is
|
|
36
|
+
* not valid (see `verifyWebhookSignature`) or the body is not a JSON object, and `TypeError` when
|
|
37
|
+
* given the 0.1.0 `signature` field instead of `header`.
|
|
20
38
|
*/
|
|
21
|
-
declare function
|
|
39
|
+
declare function constructEvent<T = Record<string, unknown>>(input: VerifyWebhookSignatureInput): WebhookEvent<T>;
|
|
22
40
|
|
|
23
|
-
export { type VerifyWebhookSignatureInput, verifyWebhookSignature };
|
|
41
|
+
export { DEFAULT_WEBHOOK_TOLERANCE_SECONDS, type VerifyWebhookSignatureInput, WEBHOOK_SIGNATURE_HEADER, WebhookEvent, WebhookSignatureError, constructEvent, verifyWebhookSignature };
|
package/dist/webhooks.js
CHANGED
|
@@ -1,20 +1,88 @@
|
|
|
1
1
|
// src/webhooks.ts
|
|
2
2
|
import { createHmac, timingSafeEqual } from "crypto";
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
hex = hex.slice(SIGNATURE_PREFIX.length);
|
|
3
|
+
var WEBHOOK_SIGNATURE_HEADER = "Replicer-Signature";
|
|
4
|
+
var DEFAULT_WEBHOOK_TOLERANCE_SECONDS = 300;
|
|
5
|
+
var WebhookSignatureError = class extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "WebhookSignatureError";
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
};
|
|
11
|
+
var HEX_SHA256 = /^[0-9a-f]{64}$/i;
|
|
12
|
+
var UNIX_SECONDS = /^\d{1,12}$/;
|
|
13
|
+
function parseHeader(header) {
|
|
14
|
+
let timestamp = null;
|
|
15
|
+
const signatures = [];
|
|
16
|
+
for (const part of header.split(",")) {
|
|
17
|
+
const eq = part.indexOf("=");
|
|
18
|
+
if (eq <= 0) continue;
|
|
19
|
+
const key = part.slice(0, eq).trim();
|
|
20
|
+
const value = part.slice(eq + 1).trim();
|
|
21
|
+
if (key === "t") {
|
|
22
|
+
if (timestamp !== null || !UNIX_SECONDS.test(value)) return null;
|
|
23
|
+
timestamp = Number(value);
|
|
24
|
+
} else if (key === "v1" && HEX_SHA256.test(value)) {
|
|
25
|
+
signatures.push(Buffer.from(value, "hex"));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (timestamp === null || signatures.length === 0) return null;
|
|
29
|
+
return { timestamp, signatures };
|
|
30
|
+
}
|
|
31
|
+
function assertNoRenamedSignature(input) {
|
|
32
|
+
if (!input || typeof input !== "object") return;
|
|
33
|
+
const fields = input;
|
|
34
|
+
if (fields.header === void 0 && "signature" in fields) {
|
|
35
|
+
throw new TypeError(
|
|
36
|
+
`'signature' was renamed to 'header' in @replicer/sdk 0.2.0: pass the ${WEBHOOK_SIGNATURE_HEADER} header value as 'header'`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function sign(secret, timestamp, payload) {
|
|
41
|
+
return createHmac("sha256", secret).update(`${timestamp}.`).update(payload).digest();
|
|
42
|
+
}
|
|
43
|
+
function verifyWebhookSignature(input) {
|
|
44
|
+
assertNoRenamedSignature(input);
|
|
45
|
+
if (!input || typeof input.header !== "string") return false;
|
|
46
|
+
const payload = input.payload;
|
|
47
|
+
if (typeof payload !== "string" && !(payload instanceof Uint8Array)) return false;
|
|
48
|
+
const secrets = [input.secret, ...Array.isArray(input.secrets) ? input.secrets : []].filter((s) => typeof s === "string" && s !== "");
|
|
49
|
+
if (secrets.length === 0) return false;
|
|
50
|
+
const parsed = parseHeader(input.header);
|
|
51
|
+
if (!parsed) return false;
|
|
52
|
+
const tolerance = typeof input.toleranceSeconds === "number" && Number.isFinite(input.toleranceSeconds) && input.toleranceSeconds >= 0 ? input.toleranceSeconds : DEFAULT_WEBHOOK_TOLERANCE_SECONDS;
|
|
53
|
+
const nowMs = (input.now ?? /* @__PURE__ */ new Date()).getTime();
|
|
54
|
+
if (!Number.isFinite(nowMs)) return false;
|
|
55
|
+
if (Math.abs(nowMs / 1e3 - parsed.timestamp) > tolerance) return false;
|
|
56
|
+
let match = false;
|
|
57
|
+
for (const secret of secrets) {
|
|
58
|
+
const expected = sign(secret, parsed.timestamp, payload);
|
|
59
|
+
for (const received of parsed.signatures) {
|
|
60
|
+
if (timingSafeEqual(expected, received)) match = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return match;
|
|
64
|
+
}
|
|
65
|
+
function constructEvent(input) {
|
|
66
|
+
assertNoRenamedSignature(input);
|
|
67
|
+
if (!verifyWebhookSignature(input)) {
|
|
68
|
+
throw new WebhookSignatureError("Invalid webhook signature");
|
|
69
|
+
}
|
|
70
|
+
const text = typeof input.payload === "string" ? input.payload : new TextDecoder().decode(input.payload);
|
|
71
|
+
let parsed;
|
|
72
|
+
try {
|
|
73
|
+
parsed = JSON.parse(text);
|
|
74
|
+
} catch {
|
|
75
|
+
throw new WebhookSignatureError("Webhook payload is not valid JSON");
|
|
76
|
+
}
|
|
77
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
78
|
+
throw new WebhookSignatureError("Webhook payload is not a JSON object");
|
|
79
|
+
}
|
|
80
|
+
return parsed;
|
|
17
81
|
}
|
|
18
82
|
export {
|
|
83
|
+
DEFAULT_WEBHOOK_TOLERANCE_SECONDS,
|
|
84
|
+
WEBHOOK_SIGNATURE_HEADER,
|
|
85
|
+
WebhookSignatureError,
|
|
86
|
+
constructEvent,
|
|
19
87
|
verifyWebhookSignature
|
|
20
88
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@replicer/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Typed JavaScript/TypeScript client for the Replicer API: calls, campaigns, knowledge bases, contacts, calendar, messages and
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Typed JavaScript/TypeScript client for the Replicer API: calls, campaigns, knowledge bases, contacts, calendar, messages, TTS and webhooks.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"replicer",
|
|
7
7
|
"sdk",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"calls",
|
|
11
11
|
"campaigns",
|
|
12
12
|
"knowledge-base",
|
|
13
|
+
"webhooks",
|
|
13
14
|
"api"
|
|
14
15
|
],
|
|
15
16
|
"license": "MIT",
|