@crowdin/app-project-module 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,12 +13,29 @@ const crowdin_apps_functions_1 = require("@crowdin/crowdin-apps-functions");
13
13
  const models_1 = require("../models");
14
14
  const storage_1 = require("../storage");
15
15
  const util_1 = require("../util");
16
+ function fetchToken(config, event) {
17
+ var _a, _b;
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ if (config.authenticationType === models_1.AuthenticationType.APP) {
20
+ const token = yield (0, crowdin_apps_functions_1.fetchAppToken)(config.identifier, event.appSecret, config.clientId, config.clientSecret, event.domain || '', event.userId, (_a = config.crowdinUrls) === null || _a === void 0 ? void 0 : _a.accountUrl);
21
+ return {
22
+ accessToken: (0, util_1.encryptData)(config, token.accessToken),
23
+ expiresIn: token.expiresIn,
24
+ refreshToken: '',
25
+ };
26
+ }
27
+ const token = yield (0, crowdin_apps_functions_1.generateOAuthToken)(config.clientId, config.clientSecret, event.code || '', (_b = config.crowdinUrls) === null || _b === void 0 ? void 0 : _b.accountUrl);
28
+ return {
29
+ accessToken: (0, util_1.encryptData)(config, token.accessToken),
30
+ refreshToken: (0, util_1.encryptData)(config, token.refreshToken),
31
+ expiresIn: token.expiresIn,
32
+ };
33
+ });
34
+ }
16
35
  function handle(config) {
17
36
  return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
18
- var _a;
19
37
  const event = req.body;
20
- (0, util_1.log)(`Recieved install request ${JSON.stringify(event, null, 2)}`, config.logger);
21
- const token = yield (0, crowdin_apps_functions_1.generateOAuthToken)(config.clientId, config.clientSecret, event.code, (_a = config.crowdinUrls) === null || _a === void 0 ? void 0 : _a.accountUrl);
38
+ const token = yield fetchToken(config, event);
22
39
  const credentials = {
23
40
  id: (event.domain || event.organizationId).toString(),
24
41
  appSecret: event.appSecret,
@@ -26,8 +43,8 @@ function handle(config) {
26
43
  userId: event.userId,
27
44
  organizationId: event.organizationId,
28
45
  baseUrl: event.baseUrl,
29
- accessToken: (0, util_1.encryptData)(config, token.accessToken),
30
- refreshToken: (0, util_1.encryptData)(config, token.refreshToken),
46
+ accessToken: token.accessToken,
47
+ refreshToken: token.refreshToken,
31
48
  expire: (new Date().getTime() / 1000 + token.expiresIn).toString(),
32
49
  type: event.domain ? models_1.AccountType.ENTERPRISE : models_1.AccountType.NORMAL,
33
50
  };
@@ -113,7 +113,7 @@ function handle(config) {
113
113
  logo: '/logo.png',
114
114
  baseUrl: config.baseUrl,
115
115
  authentication: {
116
- type: 'authorization_code',
116
+ type: config.authenticationType || models_1.AuthenticationType.CODE,
117
117
  clientId: config.clientId,
118
118
  },
119
119
  events,
@@ -4,6 +4,10 @@ import { Request } from 'express';
4
4
  import { MySQLStorageConfig } from '../storage/mysql';
5
5
  import { PostgreStorageConfig } from '../storage/postgre';
6
6
  export interface Config extends ImagePath {
7
+ /**
8
+ * Authentication Crowdin App type: "authorization_code", "crowdin_app". Default: "authorization_code"
9
+ */
10
+ authenticationType?: AuthenticationType;
7
11
  /**
8
12
  * client id that we received when registering the app
9
13
  */
@@ -116,6 +120,10 @@ export interface Config extends ImagePath {
116
120
  */
117
121
  pricing?: Pricing;
118
122
  }
123
+ export declare enum AuthenticationType {
124
+ CODE = "authorization_code",
125
+ APP = "crowdin_app"
126
+ }
119
127
  export interface CrowdinUrls {
120
128
  apiUrl?: string;
121
129
  accountUrl?: string;
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EditorPanelsMode = exports.ProcessFileJobType = exports.SubscriptionInfoType = exports.AccountType = exports.Scope = void 0;
3
+ exports.EditorPanelsMode = exports.ProcessFileJobType = exports.SubscriptionInfoType = exports.AccountType = exports.Scope = exports.AuthenticationType = void 0;
4
+ var AuthenticationType;
5
+ (function (AuthenticationType) {
6
+ AuthenticationType["CODE"] = "authorization_code";
7
+ AuthenticationType["APP"] = "crowdin_app";
8
+ })(AuthenticationType = exports.AuthenticationType || (exports.AuthenticationType = {}));
4
9
  var Scope;
5
10
  (function (Scope) {
6
11
  Scope["ALL_SCOPES"] = "all";
@@ -1,23 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -33,13 +14,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
14
  Object.defineProperty(exports, "__esModule", { value: true });
34
15
  exports.checkSubscription = exports.isAppFree = exports.clearCache = exports.prepareIntegrationCredentials = exports.prepareCrowdinClient = void 0;
35
16
  const crowdin_api_client_1 = __importDefault(require("@crowdin/crowdin-api-client"));
36
- const crowdinAppFunctions = __importStar(require("@crowdin/crowdin-apps-functions"));
17
+ const crowdin_apps_functions_1 = require("@crowdin/crowdin-apps-functions");
37
18
  const axios_1 = __importDefault(require("axios"));
38
19
  const _1 = require(".");
39
20
  const models_1 = require("../models");
40
21
  const storage_1 = require("../storage");
22
+ function refreshToken(config, credentials) {
23
+ var _a, _b;
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ if (config.authenticationType === models_1.AuthenticationType.APP) {
26
+ const token = yield (0, crowdin_apps_functions_1.fetchAppToken)(config.identifier, credentials.appSecret, config.clientId, config.clientSecret, credentials.domain || '', credentials.userId, (_a = config.crowdinUrls) === null || _a === void 0 ? void 0 : _a.accountUrl);
27
+ return {
28
+ accessToken: (0, _1.encryptData)(config, token.accessToken),
29
+ expiresIn: token.expiresIn,
30
+ refreshToken: '',
31
+ };
32
+ }
33
+ const token = yield (0, crowdin_apps_functions_1.refreshOAuthToken)(config.clientId, config.clientSecret, (0, _1.decryptData)(config, credentials.refreshToken), (_b = config.crowdinUrls) === null || _b === void 0 ? void 0 : _b.accountUrl);
34
+ return {
35
+ accessToken: (0, _1.encryptData)(config, token.accessToken),
36
+ refreshToken: (0, _1.encryptData)(config, token.refreshToken),
37
+ expiresIn: token.expiresIn,
38
+ };
39
+ });
40
+ }
41
41
  function prepareCrowdinClient(config, credentials) {
42
- var _a, _b, _c;
42
+ var _a, _b;
43
43
  return __awaiter(this, void 0, void 0, function* () {
44
44
  const isExpired = +credentials.expire < +new Date().getTime() / 1000;
45
45
  const organization = credentials.type === models_1.AccountType.ENTERPRISE ? credentials.id : undefined;
@@ -51,7 +51,7 @@ function prepareCrowdinClient(config, credentials) {
51
51
  };
52
52
  }
53
53
  (0, _1.log)('Crowdin credentials have expired. Requesting a new credentials', config.logger);
54
- const newCredentials = yield crowdinAppFunctions.refreshOAuthToken(config.clientId, config.clientSecret, (0, _1.decryptData)(config, credentials.refreshToken), (_b = config.crowdinUrls) === null || _b === void 0 ? void 0 : _b.accountUrl);
54
+ const newCredentials = yield refreshToken(config, credentials);
55
55
  (0, _1.log)('Saving updated crowdin credentials in the database', config.logger);
56
56
  yield (0, storage_1.getStorage)().updateCrowdinCredentials({
57
57
  id: credentials.id,
@@ -60,18 +60,15 @@ function prepareCrowdinClient(config, credentials) {
60
60
  userId: credentials.userId,
61
61
  organizationId: credentials.organizationId,
62
62
  baseUrl: credentials.baseUrl,
63
- refreshToken: (0, _1.encryptData)(config, newCredentials.refreshToken),
64
- accessToken: (0, _1.encryptData)(config, newCredentials.accessToken),
63
+ refreshToken: newCredentials.refreshToken,
64
+ accessToken: newCredentials.accessToken,
65
65
  expire: (new Date().getTime() / 1000 + newCredentials.expiresIn).toString(),
66
66
  type: credentials.type,
67
67
  });
68
+ const token = (0, _1.decryptData)(config, credentials.accessToken);
68
69
  return {
69
- client: new crowdin_api_client_1.default({
70
- token: newCredentials.accessToken,
71
- organization,
72
- baseUrl: (_c = config.crowdinUrls) === null || _c === void 0 ? void 0 : _c.apiUrl,
73
- }),
74
- token: newCredentials.accessToken,
70
+ client: new crowdin_api_client_1.default({ token, organization, baseUrl: (_b = config.crowdinUrls) === null || _b === void 0 ? void 0 : _b.apiUrl }),
71
+ token: token,
75
72
  };
76
73
  });
77
74
  }
@@ -170,7 +167,7 @@ function checkSubscription(config, token, organization, accountType) {
170
167
  }
171
168
  }
172
169
  try {
173
- const subscription = yield crowdinAppFunctions.getSubscription({
170
+ const subscription = yield (0, crowdin_apps_functions_1.getSubscription)({
174
171
  appIdentifier,
175
172
  organization: accountType === models_1.AccountType.ENTERPRISE ? organization : undefined,
176
173
  token,
@@ -183,7 +180,7 @@ function checkSubscription(config, token, organization, accountType) {
183
180
  return { expired, daysLeft, type: models_1.SubscriptionInfoType.SUBSCRIPTION };
184
181
  }
185
182
  catch (e) {
186
- if (e instanceof crowdinAppFunctions.PaymentRequiredError) {
183
+ if (e instanceof crowdin_apps_functions_1.PaymentRequiredError) {
187
184
  const { initializedAt, subscribeLink } = e;
188
185
  (0, _1.log)(`Recieved 402 payment error. initializedAt ${initializedAt}`, config.logger);
189
186
  //default 2 weeks
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "description": "Module that generates for you all common endpoints for serving standalone Crowdin App",
5
5
  "main": "out/index.js",
6
6
  "types": "out/index.d.ts",
@@ -12,7 +12,7 @@
12
12
  "test": "echo \"test not implemented\""
13
13
  },
14
14
  "dependencies": {
15
- "@crowdin/crowdin-apps-functions": "0.1.6",
15
+ "@crowdin/crowdin-apps-functions": "~0.2.1",
16
16
  "@types/pg": "^8.6.5",
17
17
  "crypto-js": "^4.0.0",
18
18
  "express": "4.17.1",