UNPKG

lightrail-client

Version:
246 lines (245 loc) 9.07 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getWebhookSecretId = exports.getWebhookId = exports.deleteWebhookSecret = exports.getWebhookSecret = exports.createWebhookSecret = exports.deleteWebhook = exports.updateWebhook = exports.listWebhooks = exports.getWebhook = exports.createWebhook = exports.verifySignature = void 0; const crypto = require("crypto"); const lightrail = require("./index"); const index_1 = require("./index"); const requestUtils_1 = require("./requestUtils"); /** * Example: * ```js * const signature = "abcdefg"; // webhook signature * const payload = "{}"; // webhook payload JSON string * const secret = "the secret stored in your system"; * const verified = Lightrail.webhooks.verifySignature(signature, payload, secret); * ``` */ function verifySignature(signatureHeader, payload, webhookSecret) { const secret = webhookSecret ? webhookSecret : index_1.configuration.webhookSecret; if (!signatureHeader) { throw new Error("The signatureHeader cannot be null"); } if (!secret) { throw new Error("The webhookSecret cannot be null"); } if (!payload) { throw new Error("The payload cannot be null"); } const eventSignatures = signatureHeader.split(","); const signature = crypto .createHmac("sha256", secret) .update(payload) .digest("hex"); // constant time string comparison to prevent timing attacks (see: https://codahale.com/a-lesson-in-timing-attacks) return eventSignatures.reduce((prev, cur) => prev || (cur.length === signature.length && crypto.timingSafeEqual(Buffer.from(cur), Buffer.from(signature))), false); } exports.verifySignature = verifySignature; /** * See: https://apidocs.lightrail.com/#operation/CreateWebhook * * Example: * ```js * const webhook = await Lightrail.webhooks.createWebhook({ * id: "abcdefg", * url: "https://www.example.com/webhook", * events: ["*"] * }); * ``` */ function createWebhook(params) { return __awaiter(this, void 0, void 0, function* () { if (!params) { throw new Error("params not set"); } else if (!params.id) { throw new Error("params.id not set"); } const resp = yield lightrail.request("POST", "webhooks").send(params); if (requestUtils_1.isSuccessStatus(resp.status)) { return requestUtils_1.formatResponse(resp); } throw new index_1.LightrailRequestError(resp); }); } exports.createWebhook = createWebhook; /** * See: https://apidocs.lightrail.com/#operation/GetWebhook * * Example: * ```js * const webhook = await Lightrail.webhooks.getWebhook("abcdefg"); * ``` */ function getWebhook(webhook) { return __awaiter(this, void 0, void 0, function* () { const webhookId = getWebhookId(webhook); const resp = yield lightrail.request("GET", `webhooks/${encodeURIComponent(webhookId)}`); if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) { return requestUtils_1.formatResponse(resp); } throw new index_1.LightrailRequestError(resp); }); } exports.getWebhook = getWebhook; /** * See: https://apidocs.lightrail.com/#operation/ListWebhooks * * Example: * ```js * const webhooks = await Lightrail.webhooks.listWebhooks(); * ``` */ function listWebhooks() { return __awaiter(this, void 0, void 0, function* () { const resp = yield lightrail.request("GET", "webhooks"); if (requestUtils_1.isSuccessStatus(resp.status)) { return requestUtils_1.formatResponse(resp); } throw new index_1.LightrailRequestError(resp); }); } exports.listWebhooks = listWebhooks; /** * See: https://apidocs.lightrail.com/#operation/UpdateWebhook * * Example: * ```js * const updatedWebhook = await Lightrail.webhooks.updateWebhook("abcdefg", {url: "https://www.example.com/webhookUpdated"}); * ``` */ function updateWebhook(webhook, params) { return __awaiter(this, void 0, void 0, function* () { const webhookId = getWebhookId(webhook); const resp = yield lightrail.request("PATCH", `webhooks/${encodeURIComponent(webhookId)}`).send(params); if (requestUtils_1.isSuccessStatus(resp.status)) { return requestUtils_1.formatResponse(resp); } throw new index_1.LightrailRequestError(resp); }); } exports.updateWebhook = updateWebhook; /** * See: https://apidocs.lightrail.com/#operation/DeleteWebhook * * Example: * ```js * await Lightrail.webhooks.deleteWebhook("abcdefg"); * ``` */ function deleteWebhook(webhook) { return __awaiter(this, void 0, void 0, function* () { const webhookId = getWebhookId(webhook); const resp = yield lightrail.request("DELETE", `webhooks/${encodeURIComponent(webhookId)}`); if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) { return; } throw new index_1.LightrailRequestError(resp); }); } exports.deleteWebhook = deleteWebhook; /** * See: https://apidocs.lightrail.com/#operation/CreateSecret * * Example: * ```js * const webhookSecret = await Lightrail.webhooks.createWebhookSecret("abcdefg"); * ``` */ function createWebhookSecret(webhook) { return __awaiter(this, void 0, void 0, function* () { const webhookId = getWebhookId(webhook); const resp = yield lightrail.request("POST", `webhooks/${encodeURIComponent(webhookId)}/secrets`); if (requestUtils_1.isSuccessStatus(resp.status)) { return requestUtils_1.formatResponse(resp); } throw new index_1.LightrailRequestError(resp); }); } exports.createWebhookSecret = createWebhookSecret; /** * See: https://apidocs.lightrail.com/#operation/GetSecret * * Example: * ```js * const webhookSecret = await Lightrail.webhooks.getWebhookSecret("abcdefg", "hijklmnop"); * ``` */ function getWebhookSecret(webhook, webhookSecret) { return __awaiter(this, void 0, void 0, function* () { const webhookId = getWebhookId(webhook); const webhookSecretId = getWebhookSecretId(webhookSecret); const resp = yield lightrail.request("GET", `webhooks/${encodeURIComponent(webhookId)}/secrets/${encodeURIComponent(webhookSecretId)}`); if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) { return requestUtils_1.formatResponse(resp); } throw new index_1.LightrailRequestError(resp); }); } exports.getWebhookSecret = getWebhookSecret; /** * See: https://apidocs.lightrail.com/#operation/DeleteSecret * * Example: * ```js * await Lightrail.webhooks.deleteWebhookSecret("abcdefg", "hijklmnop"); * ``` */ function deleteWebhookSecret(webhook, webhookSecret) { return __awaiter(this, void 0, void 0, function* () { const webhookId = getWebhookId(webhook); const webhookSecretId = getWebhookSecretId(webhookSecret); const resp = yield lightrail.request("DELETE", `webhooks/${encodeURIComponent(webhookId)}/secrets/${encodeURIComponent(webhookSecretId)}`); if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) { return; } throw new index_1.LightrailRequestError(resp); }); } exports.deleteWebhookSecret = deleteWebhookSecret; /** * @internal * Get webhookId from the string (as the ID itself) or Webhook object. */ function getWebhookId(webhook) { if (!webhook) { throw new Error("webhook not set"); } else if (typeof webhook === "string") { return webhook; } else if (webhook.id) { return webhook.id; } else { throw new Error("webhook must be a string for webhookId or a Webhook object"); } } exports.getWebhookId = getWebhookId; /** * @internal * Get webhookSecretId from the string (as the ID itself) or WebhookSecret object. */ function getWebhookSecretId(webhookSecret) { if (!webhookSecret) { throw new Error("webhookSecret not set"); } else if (typeof webhookSecret === "string") { return webhookSecret; } else if (webhookSecret.id) { return webhookSecret.id; } else { throw new Error("webhookSecret must be a string for webhookSecretId or a WebhookSecret object"); } } exports.getWebhookSecretId = getWebhookSecretId;