@contentstack/cli-auth 1.3.9 → 1.3.10

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.
package/README.md CHANGED
@@ -18,7 +18,7 @@ $ npm install -g @contentstack/cli-auth
18
18
  $ csdx COMMAND
19
19
  running command...
20
20
  $ csdx (--version)
21
- @contentstack/cli-auth/1.3.9 linux-x64 node-v16.20.0
21
+ @contentstack/cli-auth/1.3.10 linux-x64 node-v16.20.1
22
22
  $ csdx --help [COMMAND]
23
23
  USAGE
24
24
  $ csdx COMMAND
@@ -16,6 +16,7 @@ class TokensAddCommand extends cli_command_1.Command {
16
16
  let isDelivery = addTokenFlags.delivery;
17
17
  let isManagement = addTokenFlags.management;
18
18
  let environment = addTokenFlags.environment;
19
+ let branch = addTokenFlags.branch;
19
20
  const configKeyTokens = 'tokens';
20
21
  if (!isDelivery && !isManagement && !Boolean(environment)) {
21
22
  let tokenType = await (0, interactive_1.askTokenType)();
@@ -47,6 +48,36 @@ class TokensAddCommand extends cli_command_1.Command {
47
48
  if (!token) {
48
49
  token = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_TOKEN', name: 'token' });
49
50
  }
51
+ const managementAPIClient = await (0, cli_utilities_1.managementSDKClient)({ host: this.cmaHost });
52
+ let doBranchesExistInPlan = false;
53
+ if (isManagement && apiKey && token) {
54
+ await managementAPIClient
55
+ .stack({ api_key: apiKey, management_token: token })
56
+ .branch()
57
+ .query()
58
+ .find()
59
+ .then(() => (doBranchesExistInPlan = true))
60
+ .catch((err) => {
61
+ if (err.errorCode && err.errorMessage && branch) {
62
+ throw new Error(err.errorMessage);
63
+ }
64
+ });
65
+ }
66
+ else {
67
+ if (!apiKey) {
68
+ throw new Error('Api key is required');
69
+ }
70
+ if (!token) {
71
+ throw new Error('Token is required');
72
+ }
73
+ }
74
+ if (doBranchesExistInPlan && !branch) {
75
+ branch = await cli_utilities_1.cliux.inquire({
76
+ type: 'input',
77
+ message: 'CLI_AUTH_ENTER_BRANCH',
78
+ name: 'branch',
79
+ });
80
+ }
50
81
  if (isDelivery && !environment) {
51
82
  environment = await cli_utilities_1.cliux.inquire({
52
83
  type: 'input',
@@ -56,11 +87,11 @@ class TokensAddCommand extends cli_command_1.Command {
56
87
  }
57
88
  let tokenValidationResult;
58
89
  if (type === 'delivery') {
59
- tokenValidationResult = await utils_1.tokenValidation.validateDeliveryToken(this.deliveryAPIClient, apiKey, token, environment, this.region.name, this.cdaHost);
90
+ branch = branch || 'main';
91
+ tokenValidationResult = await utils_1.tokenValidation.validateDeliveryToken(this.deliveryAPIClient, apiKey, token, environment, this.region.name, this.cdaHost, branch);
60
92
  }
61
93
  else if (type === 'management') {
62
- const managementAPIClient = await (0, cli_utilities_1.managementSDKClient)({ host: this.cmaHost });
63
- tokenValidationResult = await utils_1.tokenValidation.validateManagementToken(managementAPIClient, apiKey, token);
94
+ tokenValidationResult = await utils_1.tokenValidation.validateManagementToken(managementAPIClient, apiKey, token, doBranchesExistInPlan ? branch : null);
64
95
  }
65
96
  if (!tokenValidationResult.valid) {
66
97
  throw new cli_utilities_1.CLIError(tokenValidationResult.message);
@@ -139,5 +170,11 @@ TokensAddCommand.flags = {
139
170
  description: 'Force adding',
140
171
  parse: (0, cli_utilities_1.printFlagDeprecation)(['-f', '--force'], ['-y', '--yes']),
141
172
  }),
173
+ branch: cli_utilities_1.flags.string({
174
+ required: false,
175
+ multiple: false,
176
+ description: 'Branch name',
177
+ hidden: true,
178
+ }),
142
179
  };
143
180
  TokensAddCommand.usage = 'auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]';
@@ -7,7 +7,7 @@
7
7
  * @param region
8
8
  * @returns
9
9
  */
10
- export declare const validateDeliveryToken: (contentStackClient: any, apiKey: string, deliveryToken: string, environment: string, region: string, host: string) => Promise<any>;
10
+ export declare const validateDeliveryToken: (contentStackClient: any, apiKey: string, deliveryToken: string, environment: string, region: string, host: string, branch: string) => Promise<any>;
11
11
  /**
12
12
  * Validate environment
13
13
  * @param contentStackClient
@@ -24,7 +24,7 @@ export declare const validateEnvironment: (contentStackClient: any, apiKey: stri
24
24
  * @returns { valid: boolean; message: any }
25
25
  * Note: Fetching one content type using the management token to check whether it is valid or not
26
26
  */
27
- export declare const validateManagementToken: (contentStackClient: any, apiKey: string, managementToken: string) => Promise<any>;
27
+ export declare const validateManagementToken: (contentStackClient: any, apiKey: string, managementToken: string, branch: string) => Promise<any>;
28
28
  /**
29
29
  * Validate API key
30
30
  * @param contentStackClient
@@ -11,7 +11,7 @@ const cli_utilities_1 = require("@contentstack/cli-utilities");
11
11
  * @param region
12
12
  * @returns
13
13
  */
14
- const validateDeliveryToken = async (contentStackClient, apiKey, deliveryToken, environment, region, host) => {
14
+ const validateDeliveryToken = async (contentStackClient, apiKey, deliveryToken, environment, region, host, branch) => {
15
15
  let result;
16
16
  try {
17
17
  const regionMap = {
@@ -20,13 +20,13 @@ const validateDeliveryToken = async (contentStackClient, apiKey, deliveryToken,
20
20
  AZURE_NA: 'azure-na',
21
21
  AZURE_EU: 'azure-eu',
22
22
  };
23
- const stack = contentStackClient
24
- .Stack({
23
+ const stack = contentStackClient.Stack({
25
24
  api_key: apiKey,
26
25
  delivery_token: deliveryToken,
27
26
  environment,
28
27
  region: regionMap[region],
29
28
  host,
29
+ branch,
30
30
  });
31
31
  const parsedHost = host.replace(/^https?:\/\//, '');
32
32
  stack.setHost(parsedHost);
@@ -47,6 +47,9 @@ const validateDeliveryToken = async (contentStackClient, apiKey, deliveryToken,
47
47
  else if (error.error_code === 141) {
48
48
  result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME') };
49
49
  }
50
+ else if (error === null || error === void 0 ? void 0 : error.error_code) {
51
+ throw new Error(error === null || error === void 0 ? void 0 : error.error_message);
52
+ }
50
53
  else {
51
54
  result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_DELIVERY_TOKEN') };
52
55
  }
@@ -88,11 +91,12 @@ exports.validateEnvironment = validateEnvironment;
88
91
  * @returns { valid: boolean; message: any }
89
92
  * Note: Fetching one content type using the management token to check whether it is valid or not
90
93
  */
91
- const validateManagementToken = async (contentStackClient, apiKey, managementToken) => {
94
+ const validateManagementToken = async (contentStackClient, apiKey, managementToken, branch) => {
95
+ var _a, _b;
92
96
  let result;
93
97
  try {
94
- const validationResuslt = await contentStackClient.axiosInstance.get('/content_types?limit=1', {
95
- headers: { api_key: apiKey, authorization: managementToken },
98
+ const validationResuslt = await contentStackClient.axiosInstance.get('/content_types?limit=1&include_branch=true', {
99
+ headers: { api_key: apiKey, authorization: managementToken, branch },
96
100
  });
97
101
  cli_utilities_1.logger.debug('Management validation result', validationResuslt);
98
102
  if (validationResuslt.status === 200) {
@@ -107,6 +111,9 @@ const validateManagementToken = async (contentStackClient, apiKey, managementTok
107
111
  if (error.response && error.response.status === 401) {
108
112
  result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN') };
109
113
  }
114
+ if ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) {
115
+ throw new Error((_b = error.response.data) === null || _b === void 0 ? void 0 : _b.error_message);
116
+ }
110
117
  else {
111
118
  result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
112
119
  }
@@ -3,6 +3,7 @@
3
3
  "CLI_AUTH_LOGIN_ENTER_PASSWORD": "Enter your password",
4
4
  "CLI_AUTH_LOGIN_SUCCESS": "Successfully logged in!!",
5
5
  "CLI_AUTH_LOGIN_FAILED": "Login Error",
6
+ "CLI_AUTH_AUTHENTICATION_FAILED": "You need to login first. See: auth:login --help",
6
7
  "CLI_AUTH_LOGIN_DESCRIPTION": "User session login",
7
8
  "CLI_AUTH_LOGIN_FLAG_USERNAME": "User name",
8
9
  "CLI_AUTH_LOGIN_FLAG_PASSWORD": "Password",
@@ -43,5 +44,6 @@
43
44
  "CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN": "Invalid management token",
44
45
  "CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY": "Invalid api key",
45
46
  "CLI_AUTH_EXIT_PROCESS": "Exiting the process...",
46
- "CLI_SELECT_TOKEN_TYPE": "Select the type of token to add"
47
+ "CLI_SELECT_TOKEN_TYPE": "Select the type of token to add",
48
+ "CLI_AUTH_ENTER_BRANCH": "Enter branch name"
47
49
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.3.9",
2
+ "version": "1.3.10",
3
3
  "commands": {
4
4
  "auth:login": {
5
5
  "id": "auth:login",
@@ -203,6 +203,14 @@
203
203
  "description": "Force adding",
204
204
  "hidden": true,
205
205
  "allowNo": false
206
+ },
207
+ "branch": {
208
+ "name": "branch",
209
+ "type": "option",
210
+ "description": "Branch name",
211
+ "hidden": true,
212
+ "required": false,
213
+ "multiple": false
206
214
  }
207
215
  },
208
216
  "args": {}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@contentstack/cli-auth",
3
3
  "description": "Contentstack CLI plugin for authentication activities",
4
- "version": "1.3.9",
4
+ "version": "1.3.10",
5
5
  "author": "Contentstack",
6
6
  "bugs": "https://github.com/contentstack/cli/issues",
7
7
  "scripts": {
@@ -18,7 +18,8 @@
18
18
  "lint": "eslint src/**/*.ts",
19
19
  "format": "eslint src/**/*.ts --fix",
20
20
  "test:integration": "mocha --forbid-only \"test/integration/*.test.ts\"",
21
- "test:unit": "mocha --forbid-only \"test/unit/*.test.ts\""
21
+ "test:unit": "mocha --forbid-only \"test/unit/**/*.test.ts\"",
22
+ "test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\""
22
23
  },
23
24
  "dependencies": {
24
25
  "@contentstack/cli-command": "^1.2.9",
@@ -85,4 +86,4 @@
85
86
  }
86
87
  },
87
88
  "repository": "contentstack/cli"
88
- }
89
+ }