UNPKG

@crowdin/crowdin-apps-functions

Version:

Utility library to easily and quickly develop Crowdin App

172 lines (171 loc) 6.63 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.validateJwtToken = exports.parseCrowdinId = exports.getProjectId = exports.constructCrowdinIdFromJwtPayload = exports.refreshOAuthToken = exports.generateOAuthToken = exports.fetchAgentToken = exports.fetchAppToken = void 0; const axios_1 = require("axios"); const jwt = require("jsonwebtoken"); const crowdinAuthUrl = 'https://accounts.crowdin.com/oauth/token'; function isFetchAppTokenArgs(args) { return typeof args !== 'string'; } function fetchAppToken(appIdOrArgs, appSecret, clientId, clientSecret, domain, userId, url) { return __awaiter(this, void 0, void 0, function* () { let options; if (isFetchAppTokenArgs(appIdOrArgs)) { options = appIdOrArgs; } else { // @ts-expect-error: Handling potential undefined value options = { appId: appIdOrArgs, appSecret, clientId, clientSecret, domain, userId, url }; } const token = yield axios_1.default.post(options.url || crowdinAuthUrl, { grant_type: 'crowdin_app', client_id: options.clientId, client_secret: options.clientSecret, app_id: options.appId, app_secret: options.appSecret, domain: options.domain, user_id: options.userId, }); return { accessToken: token.data.access_token, expiresIn: +token.data.expires_in, }; }); } exports.fetchAppToken = fetchAppToken; function fetchAgentToken(args) { return __awaiter(this, void 0, void 0, function* () { const token = yield axios_1.default.post(args.url || crowdinAuthUrl, { grant_type: 'crowdin_agent', client_id: args.clientId, client_secret: args.clientSecret, app_id: args.appId, app_secret: args.appSecret, domain: args.domain, user_id: args.userId, agent_id: args.agentId, }); return { accessToken: token.data.access_token, expiresIn: +token.data.expires_in, }; }); } exports.fetchAgentToken = fetchAgentToken; function isGenerateOAuthTokenArgs(args) { return typeof args !== 'string'; } function generateOAuthToken(clientIdOrArgs, clientSecret, code, url) { return __awaiter(this, void 0, void 0, function* () { let options; if (isGenerateOAuthTokenArgs(clientIdOrArgs)) { options = clientIdOrArgs; } else { // @ts-expect-error: Handling potential undefined value options = { clientId: clientIdOrArgs, clientSecret, code, url }; } const token = yield axios_1.default.post(options.url || crowdinAuthUrl, { grant_type: 'authorization_code', client_id: options.clientId, client_secret: options.clientSecret, code: options.code, }); return { accessToken: token.data.access_token, refreshToken: token.data.refresh_token, expiresIn: +token.data.expires_in, }; }); } exports.generateOAuthToken = generateOAuthToken; function isRefreshOAuthTokenArgs(args) { return typeof args !== 'string'; } function refreshOAuthToken(clientIdOrArgs, clientSecret, refreshToken, url) { return __awaiter(this, void 0, void 0, function* () { let options; if (isRefreshOAuthTokenArgs(clientIdOrArgs)) { options = clientIdOrArgs; } else { // @ts-expect-error: Handling potential undefined value options = { clientId: clientIdOrArgs, clientSecret, refreshToken, url }; } const token = yield axios_1.default.post(options.url || crowdinAuthUrl, { grant_type: 'refresh_token', client_id: options.clientId, client_secret: options.clientSecret, refresh_token: options.refreshToken, }); return { accessToken: token.data.access_token, refreshToken: token.data.refresh_token, expiresIn: +token.data.expires_in, }; }); } exports.refreshOAuthToken = refreshOAuthToken; /** * * @param jwtPayload jwt token payload * @returns unique identifier of crowdin user and project context */ function constructCrowdinIdFromJwtPayload(jwtPayload) { return `${jwtPayload.domain || jwtPayload.context.organization_id}__${jwtPayload.context.project_id}__${jwtPayload.sub}`; } exports.constructCrowdinIdFromJwtPayload = constructCrowdinIdFromJwtPayload; /** * * @param crowdinId crowdin id (from {@link constructCrowdinIdFromJwtPayload}) * @returns crowdin project id */ function getProjectId(crowdinId) { return Number(crowdinId.split('__')[1]); } exports.getProjectId = getProjectId; /** * * @param crowdinId crowdin id (from {@link constructCrowdinIdFromJwtPayload}) * @returns object with organization(id|domain), project id and user id */ function parseCrowdinId(crowdinId) { const crowdinIdParts = crowdinId.split('__'); return { organization: crowdinIdParts[0], projectId: Number(crowdinIdParts[1]), userId: Number(crowdinIdParts[2]), }; } exports.parseCrowdinId = parseCrowdinId; /** * * @param jwtToken jwt token which Crowdin adds to app iframe * @param clientSecret OAuth client secret of the app * @param options extra options for verification * @returns jwt payload */ function validateJwtToken(jwtToken, clientSecret, options) { return __awaiter(this, void 0, void 0, function* () { return new Promise((res, rej) => { jwt.verify(jwtToken, clientSecret, options, (err, decoded) => { if (err) { rej(err); } else { res(decoded); } }); }); }); } exports.validateJwtToken = validateJwtToken;