@contentstack/cli-utilities 1.5.0 → 1.5.2

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.
@@ -33,6 +33,13 @@ declare class AuthHandler {
33
33
  unsetConfigData(type?: string): Promise<void>;
34
34
  refreshToken(): Promise<object>;
35
35
  getUserDetails(data: any): Promise<object>;
36
+ oauthLogout(): Promise<object>;
37
+ /**
38
+ * Fetches all authorizations for the Oauth App, returns authorizationUid for current user.
39
+ * @returns authorizationUid for the current user
40
+ */
41
+ getOauthAppAuthorization(): Promise<string | undefined>;
42
+ revokeOauthAppAuthorization(authorizationId: any): Promise<object>;
36
43
  isAuthenticated(): boolean;
37
44
  getAuthorisationType(): Promise<any>;
38
45
  isAuthorisationTypeBasic(): Promise<boolean>;
@@ -6,6 +6,7 @@ const logger_1 = tslib_1.__importDefault(require("./logger"));
6
6
  const http_client_1 = tslib_1.__importDefault(require("./http-client"));
7
7
  const config_handler_1 = tslib_1.__importDefault(require("./config-handler"));
8
8
  const ContentstackManagementSDK = tslib_1.__importStar(require("@contentstack/management"));
9
+ const message_handler_1 = tslib_1.__importDefault(require("./message-handler"));
9
10
  const http = require('http');
10
11
  const url = require('url');
11
12
  const open_1 = tslib_1.__importDefault(require("open"));
@@ -371,6 +372,56 @@ class AuthHandler {
371
372
  }
372
373
  });
373
374
  }
375
+ async oauthLogout() {
376
+ const authorization = await this.getOauthAppAuthorization() || "";
377
+ const response = await this.revokeOauthAppAuthorization(authorization);
378
+ return response || {};
379
+ }
380
+ /**
381
+ * Fetches all authorizations for the Oauth App, returns authorizationUid for current user.
382
+ * @returns authorizationUid for the current user
383
+ */
384
+ async getOauthAppAuthorization() {
385
+ const headers = {
386
+ authorization: `Bearer ${config_handler_1.default.get(this.oauthAccessTokenKeyName)}`,
387
+ organization_uid: config_handler_1.default.get(this.oauthOrgUidKeyName),
388
+ 'Content-type': 'application/json'
389
+ };
390
+ const httpClient = new http_client_1.default().headers(headers);
391
+ await this.setOAuthBaseURL();
392
+ return httpClient
393
+ .get(`${this.OAuthBaseURL}/apps-api/manifests/${this.OAuthAppId}/authorizations`)
394
+ .then(({ data }) => {
395
+ var _a, _b;
396
+ if (((_a = data === null || data === void 0 ? void 0 : data.data) === null || _a === void 0 ? void 0 : _a.length) > 0) {
397
+ const userUid = config_handler_1.default.get(this.oauthUserUidKeyName);
398
+ const currentUserAuthorization = ((_b = data === null || data === void 0 ? void 0 : data.data) === null || _b === void 0 ? void 0 : _b.filter(element => element.user.uid === userUid)) || [];
399
+ if (currentUserAuthorization.length === 0) {
400
+ throw new Error(message_handler_1.default.parse("CLI_AUTH_LOGOUT_NO_AUTHORIZATIONS_USER"));
401
+ }
402
+ return currentUserAuthorization[0].authorization_uid; // filter authorizations by current logged in user
403
+ }
404
+ else {
405
+ throw new Error(message_handler_1.default.parse("CLI_AUTH_LOGOUT_NO_AUTHORIZATIONS"));
406
+ }
407
+ });
408
+ }
409
+ async revokeOauthAppAuthorization(authorizationId) {
410
+ if (authorizationId.length > 1) {
411
+ const headers = {
412
+ authorization: `Bearer ${config_handler_1.default.get(this.oauthAccessTokenKeyName)}`,
413
+ organization_uid: config_handler_1.default.get(this.oauthOrgUidKeyName),
414
+ 'Content-type': 'application/json'
415
+ };
416
+ const httpClient = new http_client_1.default().headers(headers);
417
+ await this.setOAuthBaseURL();
418
+ return httpClient
419
+ .delete(`${this.OAuthBaseURL}/apps-api/manifests/${this.OAuthAppId}/authorizations/${authorizationId}`)
420
+ .then(({ data }) => {
421
+ return data;
422
+ });
423
+ }
424
+ }
374
425
  isAuthenticated() {
375
426
  const authorizationType = config_handler_1.default.get(this.authorisationTypeKeyName);
376
427
  return (authorizationType === this.authorisationTypeOAUTHValue || authorizationType === this.authorisationTypeAUTHValue);
@@ -13,7 +13,7 @@ declare class Config {
13
13
  private getEncryptedConfig;
14
14
  private getDecryptedConfig;
15
15
  get(key: any): string | any;
16
- set(key: any, value: any): Promise<Conf<Record<string, unknown>>>;
16
+ set(key: any, value: any): Conf<Record<string, unknown>>;
17
17
  delete(key: any): Conf<Record<string, unknown>>;
18
18
  clear(): void;
19
19
  }
@@ -19,6 +19,7 @@ const uniqueString = require('unique-string');
19
19
  const oldConfigDirectory = xdgBasedir.config || path.join(os.tmpdir(), uniqueString());
20
20
  const pathPrefix = path.join('configstore', `${CONFIG_NAME}.json`);
21
21
  const oldConfigPath = path.join(oldConfigDirectory, pathPrefix);
22
+ const cwd = process.env.CS_CLI_CONFIG_PATH;
22
23
  class Config {
23
24
  constructor() {
24
25
  this.init();
@@ -73,7 +74,7 @@ class Config {
73
74
  }
74
75
  getObfuscationKey() {
75
76
  const obfuscationKeyName = 'obfuscation_key';
76
- const encConfig = new conf_1.default({ configName: ENC_CONFIG_NAME });
77
+ const encConfig = new conf_1.default({ configName: ENC_CONFIG_NAME, cwd });
77
78
  let obfuscationKey = encConfig === null || encConfig === void 0 ? void 0 : encConfig.get(obfuscationKeyName);
78
79
  if (!obfuscationKey) {
79
80
  encConfig.set(obfuscationKeyName, (0, uuid_1.v4)());
@@ -97,7 +98,7 @@ class Config {
97
98
  try {
98
99
  // NOTE reading current code base encrypted file if exist
99
100
  const encryptionKey = this.getObfuscationKey();
100
- this.config = new conf_1.default({ configName: CONFIG_NAME, encryptionKey });
101
+ this.config = new conf_1.default({ configName: CONFIG_NAME, encryptionKey, cwd });
101
102
  if ((_a = Object.keys(configData || {})) === null || _a === void 0 ? void 0 : _a.length) {
102
103
  this.config.set(configData); // NOTE set config data if passed any
103
104
  }
@@ -110,7 +111,7 @@ class Config {
110
111
  this.getEncryptedConfig(oldConfigData, true);
111
112
  }
112
113
  catch (_error) {
113
- _1.cliux.print(chalk_1.default.red("Error: Config file is corrupted"));
114
+ _1.cliux.print(chalk_1.default.red('Error: Config file is corrupted'));
114
115
  _1.cliux.print(_error);
115
116
  process.exit(1);
116
117
  }
@@ -136,7 +137,7 @@ class Config {
136
137
  getDecryptedConfig(configData) {
137
138
  var _a;
138
139
  try {
139
- this.config = new conf_1.default({ configName: CONFIG_NAME });
140
+ this.config = new conf_1.default({ configName: CONFIG_NAME, cwd });
140
141
  if ((_a = Object.keys(configData || {})) === null || _a === void 0 ? void 0 : _a.length) {
141
142
  this.config.set(configData); // NOTE set config data if passed any
142
143
  }
@@ -145,7 +146,7 @@ class Config {
145
146
  // console.trace(error.message)
146
147
  try {
147
148
  const encryptionKey = this.getObfuscationKey();
148
- let config = new conf_1.default({ configName: CONFIG_NAME, encryptionKey });
149
+ let config = new conf_1.default({ configName: CONFIG_NAME, encryptionKey, cwd });
149
150
  const oldConfigData = this.getConfigDataAndUnlinkConfigFile(config);
150
151
  this.getDecryptedConfig(oldConfigData); // NOTE NOTE reinitialize the config with old data and new decrypted file
151
152
  }
@@ -158,7 +159,7 @@ class Config {
158
159
  }
159
160
  catch (__error) {
160
161
  // console.trace(error.message)
161
- _1.cliux.print(chalk_1.default.red("Error: Config file is corrupted"));
162
+ _1.cliux.print(chalk_1.default.red('Error: Config file is corrupted'));
162
163
  _1.cliux.print(_error);
163
164
  process.exit(1);
164
165
  }
@@ -170,7 +171,7 @@ class Config {
170
171
  var _a;
171
172
  return (_a = this.config) === null || _a === void 0 ? void 0 : _a.get(key);
172
173
  }
173
- async set(key, value) {
174
+ set(key, value) {
174
175
  var _a;
175
176
  (_a = this.config) === null || _a === void 0 ? void 0 : _a.set(key, value);
176
177
  return this.config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentstack/cli-utilities",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "Utilities for contentstack projects",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -32,8 +32,8 @@
32
32
  "author": "contentstack",
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@contentstack/management": "^1.8.0",
36
- "@oclif/core": "^2.8.2",
35
+ "@contentstack/management": "~1.10.0",
36
+ "@oclif/core": "^2.9.3",
37
37
  "axios": "1.3.4",
38
38
  "chalk": "^4.0.0",
39
39
  "cli-cursor": "^3.1.0",
@@ -78,4 +78,4 @@
78
78
  "tslib": "^1.13.0",
79
79
  "typescript": "^4.9.3"
80
80
  }
81
- }
81
+ }