@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
87 lines (86 loc) • 4.32 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.webhookHandler = void 0;
const util_1 = require("../../../util");
const lodash_isstring_1 = __importDefault(require("lodash.isstring"));
const crypto = __importStar(require("node:crypto"));
const storage = __importStar(require("../../../storage"));
const connection_1 = require("../../../util/connection");
function webhookHandler(config, webhooks) {
return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
const domain = req.headers['x-crowdin-domain'];
const organizationId = req.headers['x-crowdin-id'];
const signature = req.headers['x-crowdin-signature'];
const moduleKey = req.headers['x-module-key'];
if (!(0, lodash_isstring_1.default)(organizationId) || !(0, lodash_isstring_1.default)(signature) || !(0, lodash_isstring_1.default)(moduleKey)) {
res.status(400).send({ error: 'Invalid request' });
return;
}
const crowdinId = domain && (0, lodash_isstring_1.default)(domain) ? domain : organizationId;
const credentials = yield storage.getStorage().getCrowdinCredentials(crowdinId);
if (!credentials) {
res.status(401).send({ error: 'Unable to load Crowdin credentials' });
return;
}
const hmac = crypto.createHmac('sha256', credentials.appSecret);
hmac.update(req.body.toString());
const generatedSignature = hmac.digest('hex');
if (generatedSignature !== signature.replace('sha256=', '')) {
res.status(403).send({ error: 'Invalid signature' });
return;
}
res.status(200).send();
const { client } = yield (0, connection_1.prepareCrowdinClient)({ config, credentials, autoRenew: true });
const json = JSON.parse(req.body.toString());
for (const webhook of webhooks) {
if (webhook.key === moduleKey) {
yield webhook.callback({
events: json.events,
client,
webhookContext: {
domain: credentials.domain,
organizationId: credentials.organizationId,
userId: credentials.userId,
agentId: credentials.agentId,
},
});
}
}
}));
}
exports.webhookHandler = webhookHandler;