@instabrain-sdks/api-client 2.5.0 → 2.7.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/LICENSE.md +31 -0
- package/README.md +684 -66
- package/dist/client/instabrain-client.d.ts +96 -14
- package/dist/client/instabrain-client.js +102 -13
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -1
- package/dist/services/adb.service.d.ts +1 -1
- package/dist/services/adb.service.js +1 -1
- package/dist/services/application.service.d.ts +9 -2
- package/dist/services/application.service.js +9 -2
- package/dist/services/finalexpense.service.d.ts +24 -6
- package/dist/services/finalexpense.service.js +24 -6
- package/dist/services/gigdb.service.d.ts +19 -8
- package/dist/services/gigdb.service.js +20 -9
- package/dist/services/ibterm.service.d.ts +17 -5
- package/dist/services/ibterm.service.js +17 -5
- package/dist/services/pivot.service.d.ts +104 -0
- package/dist/services/pivot.service.js +116 -0
- package/dist/services/pureterm.service.d.ts +12 -4
- package/dist/services/pureterm.service.js +12 -4
- package/dist/types/application.types.d.ts +27 -5
- package/dist/types/constants.d.ts +11 -0
- package/dist/types/constants.js +12 -1
- package/dist/types/enums.d.ts +55 -5
- package/dist/types/enums.js +66 -5
- package/dist/types/errors.d.ts +4 -4
- package/dist/types/errors.js +8 -2
- package/dist/types/gigdb.types.d.ts +34 -7
- package/dist/types/ibterm.types.d.ts +9 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/pivot.types.d.ts +122 -0
- package/dist/types/pivot.types.js +2 -0
- package/dist/types/webhook.types.d.ts +55 -0
- package/dist/types/webhook.types.js +3 -0
- package/dist/utils/webhook-signature.d.ts +72 -0
- package/dist/utils/webhook-signature.js +172 -0
- package/package.json +27 -16
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WEBHOOK_HEADERS = void 0;
|
|
4
|
+
exports.verifyWebhookSignature = verifyWebhookSignature;
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
/**
|
|
7
|
+
* Header names InstaBrain sends on every webhook delivery, in the lowercase form
|
|
8
|
+
* Node.js uses for `req.headers`.
|
|
9
|
+
*/
|
|
10
|
+
exports.WEBHOOK_HEADERS = {
|
|
11
|
+
/** Unix time in seconds (UTC) the signature was computed at. Fresh on every attempt. */
|
|
12
|
+
TIMESTAMP: 'x-instabrain-timestamp',
|
|
13
|
+
/** Lowercase hex HMAC-SHA256 of `{timestamp}.{rawBody}`. */
|
|
14
|
+
SIGNATURE: 'x-instabrain-signature',
|
|
15
|
+
/**
|
|
16
|
+
* Same value as the envelope `eventId`. **Not covered by the signature** — after
|
|
17
|
+
* verifying, deduplicate on the `eventId` inside the body, not on this header.
|
|
18
|
+
*/
|
|
19
|
+
EVENT_ID: 'x-instabrain-event-id',
|
|
20
|
+
};
|
|
21
|
+
const DEFAULT_TOLERANCE_SECONDS = 300;
|
|
22
|
+
/** Plain ASCII digits only, as InstaBrain sends them. Bounded to stay a safe integer. */
|
|
23
|
+
const TIMESTAMP_PATTERN = /^[0-9]{1,15}$/;
|
|
24
|
+
/** 32-byte HMAC-SHA256 digest as lowercase hex (the header is lowercased first). */
|
|
25
|
+
const SIGNATURE_PATTERN = /^[0-9a-f]{64}$/;
|
|
26
|
+
/** A "seconds" value this large is almost certainly `Date.now()` milliseconds. */
|
|
27
|
+
const MILLISECONDS_THRESHOLD = 1e11;
|
|
28
|
+
/**
|
|
29
|
+
* Verify that a webhook delivery really came from InstaBrain.
|
|
30
|
+
*
|
|
31
|
+
* Computes HMAC-SHA256 of `{timestamp}.{rawBody}` with the `secretKey` you sent
|
|
32
|
+
* in `webhookRegistrationDetails`, and compares it to `X-InstaBrain-Signature` in
|
|
33
|
+
* constant time. It also rejects a timestamp more than 5 minutes from now
|
|
34
|
+
* (configurable via `options.toleranceSeconds`).
|
|
35
|
+
*
|
|
36
|
+
* Pass the body **exactly as received** — never `JSON.stringify(JSON.parse(body))`,
|
|
37
|
+
* which can change spacing or escaping and break the signature. Parse the JSON
|
|
38
|
+
* only after this returns `true`, then deduplicate on the body's `eventId`.
|
|
39
|
+
*
|
|
40
|
+
* Returns `false` for a missing, malformed, stale, or wrong signature or
|
|
41
|
+
* timestamp. Throws `TypeError` for programming mistakes — an empty secret, a
|
|
42
|
+
* body that is not a string or bytes (e.g. an already-parsed object), or invalid
|
|
43
|
+
* options — so a misconfigured endpoint fails loudly instead of silently
|
|
44
|
+
* rejecting every event. Note that a stray request with a non-JSON
|
|
45
|
+
* `Content-Type` or no body can also reach the body check, because body parsers
|
|
46
|
+
* then leave `req.body` as `{}` or `undefined`. A single `TypeError` is
|
|
47
|
+
* therefore not proof of misconfiguration; one on every InstaBrain delivery is.
|
|
48
|
+
*
|
|
49
|
+
* @param rawBody - Raw request body: a string, or the raw bytes (`Buffer`,
|
|
50
|
+
* `Uint8Array`, or `ArrayBuffer`).
|
|
51
|
+
* @param timestampHeader - Value of the `X-InstaBrain-Timestamp` header.
|
|
52
|
+
* @param signatureHeader - Value of the `X-InstaBrain-Signature` header.
|
|
53
|
+
* @param secretKey - The `secretKey` registered on start-application.
|
|
54
|
+
* @param options - Optional timestamp tolerance and clock override.
|
|
55
|
+
* @returns `true` only when the signature matches and the timestamp is within tolerance.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import express from 'express';
|
|
60
|
+
* import { verifyWebhookSignature, WEBHOOK_HEADERS } from '@instabrain-sdks/api-client';
|
|
61
|
+
*
|
|
62
|
+
* // express.raw() on this route, ahead of any app-wide express.json(), keeps
|
|
63
|
+
* // req.body as the raw Buffer. If that is ever misconfigured, the helper throws,
|
|
64
|
+
* // Express answers 500, and InstaBrain retries (5 times, over ~31 minutes) —
|
|
65
|
+
* // a 4xx would drop the event.
|
|
66
|
+
* app.post('/webhooks/instabrain', express.raw({ type: 'application/json' }), (req, res) => {
|
|
67
|
+
* const ok = verifyWebhookSignature(
|
|
68
|
+
* req.body,
|
|
69
|
+
* req.headers[WEBHOOK_HEADERS.TIMESTAMP],
|
|
70
|
+
* req.headers[WEBHOOK_HEADERS.SIGNATURE],
|
|
71
|
+
* process.env.INSTABRAIN_WEBHOOK_SECRET!
|
|
72
|
+
* );
|
|
73
|
+
* if (!ok) {
|
|
74
|
+
* res.sendStatus(401);
|
|
75
|
+
* return;
|
|
76
|
+
* }
|
|
77
|
+
* const event = JSON.parse(req.body.toString('utf8'));
|
|
78
|
+
* // Store event.eventId, then respond 2xx quickly and process asynchronously.
|
|
79
|
+
* res.sendStatus(200);
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
function verifyWebhookSignature(rawBody, timestampHeader, signatureHeader, secretKey, options) {
|
|
84
|
+
if (typeof secretKey !== 'string' || secretKey.length === 0) {
|
|
85
|
+
throw new TypeError('verifyWebhookSignature: secretKey must be the non-empty string registered on start-application.');
|
|
86
|
+
}
|
|
87
|
+
const body = toBytes(rawBody);
|
|
88
|
+
const toleranceSeconds = resolveTolerance(options);
|
|
89
|
+
const nowSeconds = resolveNow(options);
|
|
90
|
+
const timestamp = singleHeaderValue(timestampHeader);
|
|
91
|
+
if (timestamp === null || !TIMESTAMP_PATTERN.test(timestamp)) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
const rawSignature = singleHeaderValue(signatureHeader);
|
|
95
|
+
if (rawSignature === null) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const signature = rawSignature.toLowerCase();
|
|
99
|
+
if (!SIGNATURE_PATTERN.test(signature)) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
if (Math.abs(nowSeconds - Number(timestamp)) > toleranceSeconds) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const expected = (0, crypto_1.createHmac)('sha256', secretKey)
|
|
106
|
+
.update(`${timestamp}.`, 'utf8')
|
|
107
|
+
.update(body)
|
|
108
|
+
.digest();
|
|
109
|
+
return (0, crypto_1.timingSafeEqual)(expected, Buffer.from(signature, 'hex'));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Accept exactly one header value. Node's `req.headers` joins a repeated header
|
|
113
|
+
* into one `"a, b"` string, which the patterns then reject; `req.headersDistinct`
|
|
114
|
+
* and some frameworks give an array instead, so accept a one-element array too.
|
|
115
|
+
*/
|
|
116
|
+
function singleHeaderValue(value) {
|
|
117
|
+
if (typeof value === 'string') {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
if (Array.isArray(value) && value.length === 1 && typeof value[0] === 'string') {
|
|
121
|
+
return value[0];
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
function toBytes(rawBody) {
|
|
126
|
+
if (typeof rawBody === 'string') {
|
|
127
|
+
return Buffer.from(rawBody, 'utf8');
|
|
128
|
+
}
|
|
129
|
+
if (ArrayBuffer.isView(rawBody)) {
|
|
130
|
+
return Buffer.from(rawBody.buffer, rawBody.byteOffset, rawBody.byteLength);
|
|
131
|
+
}
|
|
132
|
+
if (Object.prototype.toString.call(rawBody) === '[object ArrayBuffer]') {
|
|
133
|
+
return Buffer.from(rawBody);
|
|
134
|
+
}
|
|
135
|
+
throw new TypeError('verifyWebhookSignature: rawBody must be the raw request body as a string, Buffer, Uint8Array, or ArrayBuffer — ' +
|
|
136
|
+
'not a parsed JSON object. Read the body raw (for example express.raw({ type: "application/json" })) before verifying.');
|
|
137
|
+
}
|
|
138
|
+
function resolveTolerance(options) {
|
|
139
|
+
const toleranceSeconds = readOptions(options).toleranceSeconds;
|
|
140
|
+
if (toleranceSeconds === undefined) {
|
|
141
|
+
return DEFAULT_TOLERANCE_SECONDS;
|
|
142
|
+
}
|
|
143
|
+
if (toleranceSeconds === Infinity) {
|
|
144
|
+
return Infinity;
|
|
145
|
+
}
|
|
146
|
+
if (typeof toleranceSeconds !== 'number' || !Number.isFinite(toleranceSeconds) || toleranceSeconds <= 0) {
|
|
147
|
+
throw new TypeError('verifyWebhookSignature: options.toleranceSeconds must be a positive number of seconds, or Infinity to disable the check.');
|
|
148
|
+
}
|
|
149
|
+
return toleranceSeconds;
|
|
150
|
+
}
|
|
151
|
+
function resolveNow(options) {
|
|
152
|
+
const nowSeconds = readOptions(options).nowSeconds;
|
|
153
|
+
if (nowSeconds === undefined) {
|
|
154
|
+
return Math.floor(Date.now() / 1000);
|
|
155
|
+
}
|
|
156
|
+
if (typeof nowSeconds !== 'number' || !Number.isFinite(nowSeconds) || nowSeconds < 0) {
|
|
157
|
+
throw new TypeError('verifyWebhookSignature: options.nowSeconds must be a non-negative Unix time in seconds.');
|
|
158
|
+
}
|
|
159
|
+
if (nowSeconds >= MILLISECONDS_THRESHOLD) {
|
|
160
|
+
throw new TypeError('verifyWebhookSignature: options.nowSeconds looks like milliseconds. Pass seconds, e.g. Math.floor(Date.now() / 1000).');
|
|
161
|
+
}
|
|
162
|
+
return nowSeconds;
|
|
163
|
+
}
|
|
164
|
+
function readOptions(options) {
|
|
165
|
+
if (options === undefined) {
|
|
166
|
+
return {};
|
|
167
|
+
}
|
|
168
|
+
if (options === null || typeof options !== 'object') {
|
|
169
|
+
throw new TypeError('verifyWebhookSignature: options must be an object when provided.');
|
|
170
|
+
}
|
|
171
|
+
return options;
|
|
172
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@instabrain-sdks/api-client",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "Official Node.js SDK for InstaBrain APIs - Insurance quotes and application processing",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist",
|
|
9
9
|
"README.md",
|
|
10
|
-
"LICENSE"
|
|
10
|
+
"LICENSE.md"
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
13
|
"build": "tsc",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"test": "jest",
|
|
16
16
|
"test:watch": "jest --watch",
|
|
17
17
|
"test:coverage": "jest --coverage",
|
|
18
|
-
"lint": "eslint src
|
|
19
|
-
"lint:fix": "eslint src
|
|
18
|
+
"lint": "eslint src",
|
|
19
|
+
"lint:fix": "eslint src --fix",
|
|
20
20
|
"prepare": "npm run build",
|
|
21
21
|
"docs": "typedoc src/index.ts"
|
|
22
22
|
},
|
|
@@ -30,25 +30,36 @@
|
|
|
30
30
|
"typescript"
|
|
31
31
|
],
|
|
32
32
|
"author": "InstaBrain",
|
|
33
|
-
"license": "
|
|
33
|
+
"license": "LicenseRef-InstaBrain-Proprietary",
|
|
34
|
+
"homepage": "https://www.instabrain.io",
|
|
35
|
+
"bugs": {
|
|
36
|
+
"email": "helpdesk@instabrain.io",
|
|
37
|
+
"url": "https://www.instabrain.io"
|
|
38
|
+
},
|
|
34
39
|
"publishConfig": {
|
|
35
|
-
"access": "
|
|
40
|
+
"access": "public"
|
|
36
41
|
},
|
|
37
42
|
"engines": {
|
|
38
43
|
"node": ">=14.0.0"
|
|
39
44
|
},
|
|
40
45
|
"dependencies": {
|
|
41
|
-
"axios": "^1.
|
|
46
|
+
"axios": "^1.20.0"
|
|
42
47
|
},
|
|
43
48
|
"devDependencies": {
|
|
44
|
-
"@
|
|
45
|
-
"@types/
|
|
46
|
-
"@
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"jest": "^
|
|
50
|
-
"ts-jest": "^29.
|
|
51
|
-
"typedoc": "^0.
|
|
52
|
-
"typescript": "
|
|
49
|
+
"@eslint/js": "^10.0.1",
|
|
50
|
+
"@types/jest": "^30.0.0",
|
|
51
|
+
"@types/node": "^22.20.5",
|
|
52
|
+
"eslint": "^10.12.0",
|
|
53
|
+
"globals": "^17.13.0",
|
|
54
|
+
"jest": "^30.5.2",
|
|
55
|
+
"ts-jest": "^29.4.14",
|
|
56
|
+
"typedoc": "^0.28.20",
|
|
57
|
+
"typescript": "~6.0.3",
|
|
58
|
+
"typescript-eslint": "^8.71.1"
|
|
59
|
+
},
|
|
60
|
+
"overrides": {
|
|
61
|
+
"@istanbuljs/load-nyc-config": {
|
|
62
|
+
"js-yaml": "^4.3.2"
|
|
63
|
+
}
|
|
53
64
|
}
|
|
54
65
|
}
|