@crowdin/app-project-module 0.60.1 → 0.60.3

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.
@@ -19,52 +19,51 @@ const logger_1 = require("../../../util/logger");
19
19
  const storage_1 = require("../../../storage");
20
20
  function handle(config, integration) {
21
21
  return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
22
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
22
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
23
23
  const message = {
24
24
  uid: 'oauth_popup',
25
25
  };
26
26
  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'];
27
- const state = ((_c = integration.oauthLogin) === null || _c === void 0 ? void 0 : _c.mode) === 'polling'
28
- ? req.query[((_e = (_d = integration.oauthLogin) === null || _d === void 0 ? void 0 : _d.fieldsMapping) === null || _e === void 0 ? void 0 : _e.state) || 'state']
29
- : undefined;
27
+ 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'];
30
28
  (0, logger_1.log)(`Received request from OAuth login callback. Code ${code}`);
31
- if (state) {
32
- (0, logger_1.log)(`Received request from OAuth login callback. State ${state}`);
33
- }
29
+ (0, logger_1.log)(`Received request from OAuth login callback. State ${state}`);
30
+ const clientId = Buffer.from(state, 'base64').toString();
31
+ const redirectUri = `${config.baseUrl}${(0, defaults_1.getOauthRoute)(integration)}`;
34
32
  try {
35
33
  const oauthLogin = integration.oauthLogin;
36
34
  let credentials;
37
35
  if (oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.performGetTokenRequest) {
38
36
  (0, logger_1.log)('Performing custom get bearer token request');
39
- credentials = yield oauthLogin.performGetTokenRequest(code, req.query, req.originalUrl);
37
+ const loginForm = yield (0, storage_1.getStorage)().getMetadata((0, defaults_1.getOAuthLoginFormId)(clientId));
38
+ credentials = yield oauthLogin.performGetTokenRequest(code, req.query, req.originalUrl, redirectUri, loginForm);
40
39
  }
41
40
  else {
42
41
  const request = {};
43
42
  const oauthLogin = integration.oauthLogin;
44
- request[((_f = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _f === void 0 ? void 0 : _f.code) || 'code'] = code;
45
- request[((_g = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _g === void 0 ? void 0 : _g.clientId) || 'client_id'] = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.clientId;
46
- request[((_h = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _h === void 0 ? void 0 : _h.clientSecret) || 'client_secret'] = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.clientSecret;
47
- request[((_j = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _j === void 0 ? void 0 : _j.redirectUri) || 'redirect_uri'] = `${config.baseUrl}${(0, defaults_1.getOauthRoute)(integration)}`;
43
+ request[((_e = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _e === void 0 ? void 0 : _e.code) || 'code'] = code;
44
+ 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;
45
+ 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;
46
+ request[((_h = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _h === void 0 ? void 0 : _h.redirectUri) || 'redirect_uri'] = redirectUri;
48
47
  if (oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.extraAccessTokenParameters) {
49
48
  Object.entries(oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.extraAccessTokenParameters).forEach(([key, value]) => (request[key] = value));
50
49
  }
51
- credentials = (yield axios_1.default.post(((_k = integration.oauthLogin) === null || _k === void 0 ? void 0 : _k.accessTokenUrl) || '', request, {
50
+ credentials = (yield axios_1.default.post(((_j = integration.oauthLogin) === null || _j === void 0 ? void 0 : _j.accessTokenUrl) || '', request, {
52
51
  headers: { Accept: 'application/json' },
53
52
  })).data;
54
53
  }
55
54
  const oauthCredentials = { originalUrl: req.originalUrl };
56
- oauthCredentials.accessToken = credentials[((_l = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _l === void 0 ? void 0 : _l.accessToken) || 'access_token'];
55
+ oauthCredentials.accessToken = credentials[((_k = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _k === void 0 ? void 0 : _k.accessToken) || 'access_token'];
57
56
  if (oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.refresh) {
58
- oauthCredentials.refreshToken = credentials[((_m = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _m === void 0 ? void 0 : _m.refreshToken) || 'refresh_token'];
57
+ oauthCredentials.refreshToken = credentials[((_l = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _l === void 0 ? void 0 : _l.refreshToken) || 'refresh_token'];
59
58
  oauthCredentials.expireIn =
60
- Number(credentials[((_o = oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.fieldsMapping) === null || _o === void 0 ? void 0 : _o.expiresIn) || 'expires_in']) + Date.now() / 1000;
59
+ 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;
61
60
  }
62
61
  message.data = oauthCredentials;
63
- if (((_p = integration.oauthLogin) === null || _p === void 0 ? void 0 : _p.mode) === 'polling' && state) {
64
- yield (0, storage_1.getStorage)().deleteMetadata(state);
65
- yield (0, storage_1.getStorage)().saveMetadata(state, oauthCredentials);
62
+ if (((_o = integration.oauthLogin) === null || _o === void 0 ? void 0 : _o.mode) === 'polling') {
63
+ yield (0, storage_1.getStorage)().deleteMetadata((0, defaults_1.getOAuthPollingId)(clientId));
64
+ yield (0, storage_1.getStorage)().saveMetadata((0, defaults_1.getOAuthPollingId)(clientId), oauthCredentials);
66
65
  }
67
- return res.render('oauth', { message: JSON.stringify(message), oauthMode: (_q = integration.oauthLogin) === null || _q === void 0 ? void 0 : _q.mode });
66
+ return res.render('oauth', { message: JSON.stringify(message), oauthMode: (_p = integration.oauthLogin) === null || _p === void 0 ? void 0 : _p.mode });
68
67
  }
69
68
  catch (e) {
70
69
  (0, logger_1.logError)(e);
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
+ const storage_1 = require("../../../storage");
12
13
  const util_1 = require("../../../util");
13
14
  const defaults_1 = require("../util/defaults");
14
15
  function handle(config, integration) {
@@ -21,6 +22,8 @@ function handle(config, integration) {
21
22
  return;
22
23
  }
23
24
  const { loginForm } = req.body;
25
+ yield (0, storage_1.getStorage)().deleteMetadata((0, defaults_1.getOAuthLoginFormId)(req.crowdinContext.clientId));
26
+ yield (0, storage_1.getStorage)().saveMetadata((0, defaults_1.getOAuthLoginFormId)(req.crowdinContext.clientId), loginForm);
24
27
  const url = (0, defaults_1.constructOauthUrl)({ config, integration, clientId: req.crowdinContext.clientId, loginForm });
25
28
  res.send({ url });
26
29
  }));
@@ -233,7 +233,7 @@ export interface OAuthLogin {
233
233
  */
234
234
  expiresIn?: string;
235
235
  /**
236
- * default 'state', used for `polling' mode
236
+ * default 'state'
237
237
  */
238
238
  state?: string;
239
239
  };
@@ -264,11 +264,11 @@ export interface OAuthLogin {
264
264
  */
265
265
  performGetTokenRequest?: (code: string, query: {
266
266
  [key: string]: any;
267
- }, url: string) => Promise<any>;
267
+ }, url: string, redirectUri: string, loginForm?: any) => Promise<any>;
268
268
  /**
269
269
  * Override to implement request for refreshing token (only if 'refresh' is enabled)
270
270
  */
271
- performRefreshTokenRequest?: (currentCredentials: any) => Promise<any>;
271
+ performRefreshTokenRequest?: (currentCredentials: any, loginForm?: any) => Promise<any>;
272
272
  }
273
273
  export interface File {
274
274
  id: string;
@@ -11,3 +11,4 @@ export declare function constructOauthUrl({ config, integration, clientId, login
11
11
  loginForm?: any;
12
12
  }): string | undefined;
13
13
  export declare function getOAuthPollingId(clientId: string): string;
14
+ export declare function getOAuthLoginFormId(clientId: string): string;
@@ -32,7 +32,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
32
32
  });
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.getOAuthPollingId = exports.constructOauthUrl = exports.applyIntegrationModuleDefaults = exports.getOauthRoute = exports.getRootFolder = void 0;
35
+ exports.getOAuthLoginFormId = exports.getOAuthPollingId = exports.constructOauthUrl = exports.applyIntegrationModuleDefaults = exports.getOauthRoute = exports.getRootFolder = void 0;
36
36
  const crowdinAppFunctions = __importStar(require("@crowdin/crowdin-apps-functions"));
37
37
  function getRootFolder(config, integration, client, projectId) {
38
38
  return __awaiter(this, void 0, void 0, function* () {
@@ -258,27 +258,28 @@ function applyIntegrationModuleDefaults(config, integration) {
258
258
  }
259
259
  exports.applyIntegrationModuleDefaults = applyIntegrationModuleDefaults;
260
260
  function constructOauthUrl({ config, integration, clientId, loginForm, }) {
261
- var _a, _b, _c, _d;
261
+ var _a, _b, _c, _d, _e;
262
262
  const oauth = integration.oauthLogin;
263
263
  if (!oauth) {
264
264
  return;
265
265
  }
266
266
  if (oauth.getAuthorizationUrl) {
267
- return loginForm
268
- ? oauth.getAuthorizationUrl(`${config.baseUrl}${getOauthRoute(integration)}`, loginForm)
269
- : undefined;
267
+ if (!loginForm) {
268
+ return;
269
+ }
270
+ let url = oauth.getAuthorizationUrl(`${config.baseUrl}${getOauthRoute(integration)}`, loginForm);
271
+ url += `&${((_a = oauth.fieldsMapping) === null || _a === void 0 ? void 0 : _a.state) || 'state'}=${Buffer.from(clientId).toString('base64')}`;
272
+ return url;
270
273
  }
271
274
  if (!oauth.authorizationUrl) {
272
275
  return;
273
276
  }
274
277
  let url = oauth.authorizationUrl || '';
275
- url += `?${((_a = oauth.fieldsMapping) === null || _a === void 0 ? void 0 : _a.clientId) || 'client_id'}=${oauth.clientId}`;
276
- url += `&${((_b = oauth.fieldsMapping) === null || _b === void 0 ? void 0 : _b.redirectUri) || 'redirect_uri'}=${config.baseUrl}${getOauthRoute(integration)}`;
278
+ url += `?${((_b = oauth.fieldsMapping) === null || _b === void 0 ? void 0 : _b.clientId) || 'client_id'}=${oauth.clientId}`;
279
+ url += `&${((_c = oauth.fieldsMapping) === null || _c === void 0 ? void 0 : _c.redirectUri) || 'redirect_uri'}=${config.baseUrl}${getOauthRoute(integration)}`;
280
+ url += `&${((_d = oauth.fieldsMapping) === null || _d === void 0 ? void 0 : _d.state) || 'state'}=${Buffer.from(clientId).toString('base64')}`;
277
281
  if (oauth.scope) {
278
- url += `&${((_c = oauth.fieldsMapping) === null || _c === void 0 ? void 0 : _c.scope) || 'scope'}=${oauth.scope}`;
279
- }
280
- if (oauth.mode === 'polling') {
281
- url += `&${((_d = oauth.fieldsMapping) === null || _d === void 0 ? void 0 : _d.state) || 'state'}=${getOAuthPollingId(clientId)}`;
282
+ url += `&${((_e = oauth.fieldsMapping) === null || _e === void 0 ? void 0 : _e.scope) || 'scope'}=${oauth.scope}`;
282
283
  }
283
284
  if (oauth.extraAutorizationUrlParameters) {
284
285
  Object.entries(oauth.extraAutorizationUrlParameters).forEach(([key, value]) => (url += `&${key}=${value}`));
@@ -290,3 +291,7 @@ function getOAuthPollingId(clientId) {
290
291
  return `oauth_${clientId}`;
291
292
  }
292
293
  exports.getOAuthPollingId = getOAuthPollingId;
294
+ function getOAuthLoginFormId(clientId) {
295
+ return `oauth_form_${clientId}`;
296
+ }
297
+ exports.getOAuthLoginFormId = getOAuthLoginFormId;
@@ -45,6 +45,7 @@ const types_1 = require("../types");
45
45
  const axios_2 = require("./axios");
46
46
  const logger_1 = require("./logger");
47
47
  const os = __importStar(require("os"));
48
+ const defaults_1 = require("../modules/integration/util/defaults");
48
49
  const axiosCustom = new axios_2.AxiosProvider().axios;
49
50
  function prepareCrowdinClient({ config, credentials, autoRenew = false, context, }) {
50
51
  var _a, _b;
@@ -231,7 +232,8 @@ function prepareIntegrationCredentials(config, integration, integrationCredentia
231
232
  (0, logger_1.log)('Integration credentials have expired. Requesting a new credentials');
232
233
  let newCredentials;
233
234
  if (performRefreshTokenRequest) {
234
- newCredentials = yield performRefreshTokenRequest(credentials);
235
+ const loginForm = yield (0, storage_1.getStorage)().getMetadata((0, defaults_1.getOAuthLoginFormId)(integrationCredentials.id));
236
+ newCredentials = yield performRefreshTokenRequest(credentials, loginForm);
235
237
  }
236
238
  else if (oauthLogin) {
237
239
  const url = oauthLogin.refreshTokenUrl || oauthLogin.accessTokenUrl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.60.1",
3
+ "version": "0.60.3",
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",