@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
193 lines (192 loc) • 7.33 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__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.fetchAppToken = fetchAppToken;
exports.fetchAgentToken = fetchAgentToken;
exports.fetchAppWithCodeToken = fetchAppWithCodeToken;
exports.generateOAuthToken = generateOAuthToken;
exports.refreshOAuthToken = refreshOAuthToken;
exports.constructCrowdinIdFromJwtPayload = constructCrowdinIdFromJwtPayload;
exports.getProjectId = getProjectId;
exports.parseCrowdinId = parseCrowdinId;
exports.validateJwtToken = validateJwtToken;
const axios_1 = __importDefault(require("axios"));
const jwt = __importStar(require("jsonwebtoken"));
const crowdinAuthUrl = 'https://accounts.crowdin.com/oauth/token';
function fetchAppToken(options) {
return __awaiter(this, void 0, void 0, function* () {
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,
};
});
}
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,
};
});
}
function fetchAppWithCodeToken(args) {
return __awaiter(this, void 0, void 0, function* () {
const token = yield axios_1.default.post(args.url || crowdinAuthUrl, {
grant_type: 'crowdin_app_with_code',
client_id: args.clientId,
client_secret: args.clientSecret,
app_id: args.appId,
app_secret: args.appSecret,
domain: args.domain,
user_id: args.userId,
code: args.code,
});
return {
accessToken: token.data.access_token,
expiresIn: +token.data.expires_in,
};
});
}
function generateOAuthToken(options) {
return __awaiter(this, void 0, void 0, function* () {
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,
};
});
}
function refreshOAuthToken(options) {
return __awaiter(this, void 0, void 0, function* () {
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,
};
});
}
/**
*
* @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}`;
}
/**
*
* @param crowdinId crowdin id (from {@link constructCrowdinIdFromJwtPayload})
* @returns crowdin project id
*/
function getProjectId(crowdinId) {
return Number(crowdinId.split('__')[1]);
}
/**
*
* @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]),
};
}
/**
*
* @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);
}
});
});
});
}