@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
80 lines (79 loc) • 3.96 kB
JavaScript
;
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.default = handle;
const storage_1 = require("../storage");
const types_1 = require("../types");
const util_1 = require("../util");
const token_1 = require("../util/app-functions/token");
const connection_1 = require("../util/connection");
const jsx_renderer_1 = require("../util/jsx-renderer");
const logger_1 = require("../util/logger");
const subscription_1 = require("../util/subscription");
const views_1 = require("../views");
function handle({ config, allowUnauthorized = false, moduleType, }) {
return (0, util_1.runAsyncWrapper)((req, res, next) => __awaiter(this, void 0, void 0, function* () {
// Allow access to non-HTML files
const isHtmlFile = /\.html?$/i.test(req.path);
if (!isHtmlFile) {
next();
return;
}
if (allowUnauthorized) {
next();
return;
}
if (!(0, util_1.isAuthorizedConfig)(config)) {
throw new Error('Invalid configuration. UI module was configured as authorized but client id and client secret are missing');
}
const jwtToken = req.query.jwtToken;
if (!jwtToken) {
(0, logger_1.temporaryErrorDebug)('Access denied: ui-module', req);
return res.status(403).send({ error: 'Access denied' });
}
(0, logger_1.log)('Validating jwt token from incoming request');
const jwtPayload = yield (0, token_1.validateJwtToken)(jwtToken, config.clientSecret, config.jwtValidationOptions);
const id = `${jwtPayload.domain || jwtPayload.context.organization_id}`;
if (moduleType && jwtPayload.module && jwtPayload.module !== moduleType) {
(0, logger_1.temporaryErrorDebug)('Access denied: ui-module', req);
return res.status(403).send({ error: 'Access denied' });
}
const context = {
jwtPayload,
clientId: (0, token_1.constructCrowdinIdFromJwtPayload)(jwtPayload),
crowdinId: id,
appIdentifier: config.identifier,
};
const logInfo = (0, logger_1.withContext)(context);
logInfo('Loading crowdin credentials');
const credentials = yield (0, storage_1.getStorage)().getCrowdinCredentials(id);
if (!credentials) {
throw new Error("Can't find organization by id");
}
// TODO Skip subscription check here for APP_WITH_CODE
if (config.authenticationType !== types_1.AuthenticationType.APP_WITH_CODE) {
logInfo('Building crowdin client instance');
const { token } = yield (0, connection_1.prepareCrowdinClient)({ config, credentials, context });
const { expired, subscribeLink } = yield (0, subscription_1.checkSubscription)({
config,
token,
organization: credentials.id,
accountType: credentials.type,
});
if (expired) {
const html = (0, jsx_renderer_1.renderJSXOnServer)(views_1.SubscriptionPage, { subscribeLink });
res.setHeader('Content-Type', 'text/html; charset=utf-8');
return res.send(html);
}
}
next();
}));
}