UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

79 lines (78 loc) 6.29 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const util_1 = require("../../../util"); const defaults_1 = require("../util/defaults"); const logger_1 = require("../../../util/logger"); const storage_1 = require("../../../storage"); const crowdin_apps_functions_1 = require("@crowdin/crowdin-apps-functions"); function handle(config, integration) { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; const message = { uid: 'oauth_popup', }; const code = req.query[((_b = (_a = integration.oauthLogin) === null || _a === void 0 ? void 0 : _a.fieldsMapping) === null || _b === void 0 ? void 0 : _b.code) || 'code']; const state = req.query[((_d = (_c = integration.oauthLogin) === null || _c === void 0 ? void 0 : _c.fieldsMapping) === null || _d === void 0 ? void 0 : _d.state) || 'state']; (0, logger_1.log)(`Received request from OAuth login callback. Code ${code}`); (0, logger_1.log)(`Received request from OAuth login callback. State ${state}`); const clientId = Buffer.from(state, 'base64').toString(); const { organization } = (0, crowdin_apps_functions_1.parseCrowdinId)(clientId); const redirectUri = `${config.baseUrl}${(0, defaults_1.getOauthRoute)(integration)}`; try { const oauthLogin = integration.oauthLogin; let credentials; if (oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.performGetTokenRequest) { (0, logger_1.log)('Performing custom get bearer token request'); const loginForm = yield (0, storage_1.getStorage)().getMetadata((0, defaults_1.getOAuthLoginFormId)(clientId)); credentials = yield oauthLogin.performGetTokenRequest(code, req.query, req.originalUrl, redirectUri, loginForm); } else { const request = {}; const oauthLogin = integration.oauthLogin; request[((_e = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _e === void 0 ? void 0 : _e.code) || 'code'] = code; request[((_f = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _f === void 0 ? void 0 : _f.clientId) || 'client_id'] = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.clientId; request[((_g = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _g === void 0 ? void 0 : _g.clientSecret) || 'client_secret'] = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.clientSecret; request[((_h = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _h === void 0 ? void 0 : _h.redirectUri) || 'redirect_uri'] = redirectUri; if (oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.extraAccessTokenParameters) { Object.entries(oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.extraAccessTokenParameters).forEach(([key, value]) => (request[key] = value)); } credentials = (yield axios_1.default.post(((_j = integration.oauthLogin) === null || _j === void 0 ? void 0 : _j.accessTokenUrl) || '', request, { headers: { Accept: 'application/json' }, })).data; } const oauthCredentials = { originalUrl: req.originalUrl }; oauthCredentials.accessToken = credentials[((_k = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _k === void 0 ? void 0 : _k.accessToken) || 'access_token']; if (oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.refresh) { oauthCredentials.refreshToken = credentials[((_l = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _l === void 0 ? void 0 : _l.refreshToken) || 'refresh_token']; oauthCredentials.expireIn = Number(credentials[((_m = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _m === void 0 ? void 0 : _m.expiresIn) || 'expires_in']) + Date.now() / 1000; } message.data = oauthCredentials; if (((_o = integration.oauthLogin) === null || _o === void 0 ? void 0 : _o.mode) === 'polling') { yield (0, storage_1.getStorage)().deleteMetadata((0, defaults_1.getOAuthPollingId)(clientId)); const encryptedCredentials = (0, util_1.encryptData)(config, JSON.stringify(oauthCredentials)); yield (0, storage_1.getStorage)().saveMetadata((0, defaults_1.getOAuthPollingId)(clientId), encryptedCredentials, organization); } return res.render('oauth', { message: JSON.stringify(message), oauthMode: (_p = integration.oauthLogin) === null || _p === void 0 ? void 0 : _p.mode }); } catch (e) { (0, logger_1.logError)(e); message.data = { error: (0, logger_1.getErrorMessage)(e) }; return res.render('oauth', { message: JSON.stringify(message) }); } })); } exports.default = handle;