vayu-ts 0.2.10 → 0.2.12
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 +27 -0
- package/dist/openapi/models/GetProductConsumptionResponseProductConsumption.d.ts +4 -1
- package/dist/openapi/models/NotificationEventType.d.ts +2 -1
- package/dist/openapi/models/NotificationEventType.js +1 -0
- package/dist/openapi/models/ProductConsumption.d.ts +4 -1
- package/dist/sdk/clients/WebhooksClient.d.ts +7 -1
- package/dist/sdk/clients/WebhooksClient.js +18 -2
- package/dist/sdk/consts/public-webhook-key.d.ts +1 -0
- package/dist/sdk/consts/public-webhook-key.js +12 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -193,6 +193,33 @@ await vayu.webhooks.subscribeToOverage('https://your-webhook-url.com/overage');
|
|
|
193
193
|
// Subscribe to anonymous customer creation notifications
|
|
194
194
|
await vayu.webhooks.subscribeToAnonymousCustomerCreated('https://your-webhook-url.com/your_path');
|
|
195
195
|
|
|
196
|
+
// Validate webhook security
|
|
197
|
+
app.post('/webhooks', (req, res) => {
|
|
198
|
+
const payload = JSON.stringify(req.body);
|
|
199
|
+
const timestamp = Number(req.headers['x-timestamp']);
|
|
200
|
+
const signature = String(req.headers['x-signature']);
|
|
201
|
+
|
|
202
|
+
// Important to login before verifying webhook signatures.
|
|
203
|
+
await vayu.login();
|
|
204
|
+
|
|
205
|
+
const isValid = vayu.verifyWebhookSignature({
|
|
206
|
+
payload,
|
|
207
|
+
timestamp,
|
|
208
|
+
signature,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
if (!isValid) {
|
|
212
|
+
// Handle invalid token.
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Proceed with webhook handling
|
|
216
|
+
console.log('Webhook is valid', JSON.parse(payload));
|
|
217
|
+
|
|
218
|
+
// ... Handle event.
|
|
219
|
+
|
|
220
|
+
res.sendStatus(200);
|
|
221
|
+
});
|
|
222
|
+
|
|
196
223
|
```
|
|
197
224
|
|
|
198
225
|
## Features
|
|
@@ -14,7 +14,10 @@ export declare class GetProductConsumptionResponseProductConsumption {
|
|
|
14
14
|
* The id of the product.
|
|
15
15
|
*/
|
|
16
16
|
'productId': string;
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The name of the product.
|
|
19
|
+
*/
|
|
20
|
+
'productName': string;
|
|
18
21
|
/**
|
|
19
22
|
* The amount of units provisioned to the customer.
|
|
20
23
|
*/
|
|
@@ -14,5 +14,6 @@ export declare enum NotificationEventType {
|
|
|
14
14
|
Overage = "Overage",
|
|
15
15
|
UpcomingRenewal = "UpcomingRenewal",
|
|
16
16
|
InvoiceApproved = "InvoiceApproved",
|
|
17
|
-
CustomerPortalLinkSent = "CustomerPortalLinkSent"
|
|
17
|
+
CustomerPortalLinkSent = "CustomerPortalLinkSent",
|
|
18
|
+
CommitmentCrossed = "CommitmentCrossed"
|
|
18
19
|
}
|
|
@@ -19,4 +19,5 @@ var NotificationEventType;
|
|
|
19
19
|
NotificationEventType["UpcomingRenewal"] = "UpcomingRenewal";
|
|
20
20
|
NotificationEventType["InvoiceApproved"] = "InvoiceApproved";
|
|
21
21
|
NotificationEventType["CustomerPortalLinkSent"] = "CustomerPortalLinkSent";
|
|
22
|
+
NotificationEventType["CommitmentCrossed"] = "CommitmentCrossed";
|
|
22
23
|
})(NotificationEventType = exports.NotificationEventType || (exports.NotificationEventType = {}));
|
|
@@ -14,7 +14,10 @@ export declare class ProductConsumption {
|
|
|
14
14
|
* The id of the product.
|
|
15
15
|
*/
|
|
16
16
|
'productId': string;
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The name of the product.
|
|
19
|
+
*/
|
|
20
|
+
'productName': string;
|
|
18
21
|
/**
|
|
19
22
|
* The amount of units provisioned to the customer.
|
|
20
23
|
*/
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
export declare class WebhooksClient {
|
|
2
2
|
private client;
|
|
3
3
|
constructor();
|
|
4
|
-
|
|
4
|
+
verifyWebhookSignature({ payload, timestamp, signature, tolerance, }: {
|
|
5
|
+
payload: string;
|
|
6
|
+
timestamp: number;
|
|
7
|
+
signature: string;
|
|
8
|
+
tolerance?: number;
|
|
9
|
+
}): boolean;
|
|
10
|
+
subscribeToCommitmentCrossed(callbackUrl: string): Promise<void>;
|
|
5
11
|
subscribeToAnonymousCustomerCreated(callbackUrl: string): Promise<void>;
|
|
6
12
|
private subscribe;
|
|
7
13
|
}
|
|
@@ -1,16 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.WebhooksClient = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
4
8
|
const openapi_1 = require("../../openapi");
|
|
9
|
+
const public_webhook_key_1 = require("../consts/public-webhook-key");
|
|
5
10
|
const services_1 = require("../services");
|
|
6
11
|
class WebhooksClient {
|
|
7
12
|
constructor() {
|
|
8
13
|
this.client = services_1.ConfigurationService.instance.generateNewClient(openapi_1.WebhooksApi);
|
|
9
14
|
}
|
|
10
|
-
|
|
15
|
+
verifyWebhookSignature({ payload, timestamp, signature, tolerance = 300, }) {
|
|
16
|
+
const now = Math.floor(Date.now() / 1000);
|
|
17
|
+
if (Math.abs(now - timestamp) > tolerance) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const message = `${timestamp}.${JSON.stringify(payload)}`;
|
|
21
|
+
const verifier = crypto_1.default.createVerify('RSA-SHA256');
|
|
22
|
+
verifier.update(message);
|
|
23
|
+
verifier.end();
|
|
24
|
+
return verifier.verify(public_webhook_key_1.PUBLIC_WEBHOOK_KEY, signature, 'base64');
|
|
25
|
+
}
|
|
26
|
+
subscribeToCommitmentCrossed(callbackUrl) {
|
|
11
27
|
return this.subscribe({
|
|
12
28
|
callbackUrl,
|
|
13
|
-
eventType: openapi_1.NotificationEventType.
|
|
29
|
+
eventType: openapi_1.NotificationEventType.CommitmentCrossed,
|
|
14
30
|
});
|
|
15
31
|
}
|
|
16
32
|
subscribeToAnonymousCustomerCreated(callbackUrl) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PUBLIC_WEBHOOK_KEY = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1JNmKwmOMQ9S7JyQFg2j\n5OHFhLBjqZbgSturKADF9sEH0X21RqzGXefMDmKbXMkcQiamqK9Puoj3N1bFr5lE\nf7EeUUihpcpfaeMNbNr0J8fkaq4YBO6cU3UvexLYr8WKkhaWkxB45TnysVl9FxIw\ne9iI3+QMAs/5DTIc+hAstXim+HfFW8teWYGI55KPjqqAIda3GPpFd4JnlI86TQux\np2nStncGasBpXCww7qXtE1SsM/o3/WQabzmmk4ox6Dt1DeTfMhtK/aOkz9/Cpx8V\nKCxwrKliqSM5Ies/2R4lTIKntqPsIcxxEQnVydL8YAY1uMiePVwgJFP6xRSY6J1H\nBwIDAQAB\n-----END PUBLIC KEY-----";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PUBLIC_WEBHOOK_KEY = void 0;
|
|
4
|
+
exports.PUBLIC_WEBHOOK_KEY = `-----BEGIN PUBLIC KEY-----
|
|
5
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1JNmKwmOMQ9S7JyQFg2j
|
|
6
|
+
5OHFhLBjqZbgSturKADF9sEH0X21RqzGXefMDmKbXMkcQiamqK9Puoj3N1bFr5lE
|
|
7
|
+
f7EeUUihpcpfaeMNbNr0J8fkaq4YBO6cU3UvexLYr8WKkhaWkxB45TnysVl9FxIw
|
|
8
|
+
e9iI3+QMAs/5DTIc+hAstXim+HfFW8teWYGI55KPjqqAIda3GPpFd4JnlI86TQux
|
|
9
|
+
p2nStncGasBpXCww7qXtE1SsM/o3/WQabzmmk4ox6Dt1DeTfMhtK/aOkz9/Cpx8V
|
|
10
|
+
KCxwrKliqSM5Ies/2R4lTIKntqPsIcxxEQnVydL8YAY1uMiePVwgJFP6xRSY6J1H
|
|
11
|
+
BwIDAQAB
|
|
12
|
+
-----END PUBLIC KEY-----`;
|