@contentstack/cli-auth 1.3.10 → 1.3.12
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 +1 -1
- package/lib/commands/auth/tokens/add.js +11 -42
- package/lib/utils/tokens-validation.d.ts +0 -19
- package/lib/utils/tokens-validation.js +2 -95
- package/messages/index.json +1 -0
- package/oclif.manifest.json +1 -1
- package/package.json +14 -4
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.
|
|
21
|
+
@contentstack/cli-auth/1.3.12 linux-x64 node-v18.17.0
|
|
22
22
|
$ csdx --help [COMMAND]
|
|
23
23
|
USAGE
|
|
24
24
|
$ csdx COMMAND
|
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const cli_command_1 = require("@contentstack/cli-command");
|
|
4
4
|
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
5
|
const interactive_1 = require("../../../utils/interactive");
|
|
6
|
-
const utils_1 = require("../../../utils");
|
|
7
6
|
class TokensAddCommand extends cli_command_1.Command {
|
|
8
7
|
async run() {
|
|
9
8
|
// @ts-ignore
|
|
@@ -48,36 +47,6 @@ class TokensAddCommand extends cli_command_1.Command {
|
|
|
48
47
|
if (!token) {
|
|
49
48
|
token = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_TOKEN', name: 'token' });
|
|
50
49
|
}
|
|
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
|
-
}
|
|
81
50
|
if (isDelivery && !environment) {
|
|
82
51
|
environment = await cli_utilities_1.cliux.inquire({
|
|
83
52
|
type: 'input',
|
|
@@ -85,16 +54,16 @@ class TokensAddCommand extends cli_command_1.Command {
|
|
|
85
54
|
name: 'env',
|
|
86
55
|
});
|
|
87
56
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
57
|
+
if (type === 'management') {
|
|
58
|
+
// FIXME - Once the SDK refresh token issue is resolved, need to revert this back to SDK call
|
|
59
|
+
const httpClient = new cli_utilities_1.HttpClient({ headers: { api_key: apiKey, authorization: token } });
|
|
60
|
+
const response = (await httpClient.get(`https://${this.cmaHost}/v3/environments?limit=1`)).data;
|
|
61
|
+
if ((response === null || response === void 0 ? void 0 : response.error_code) === 105) {
|
|
62
|
+
throw new Error(cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN'));
|
|
63
|
+
}
|
|
64
|
+
else if (response === null || response === void 0 ? void 0 : response.error_message) {
|
|
65
|
+
throw new Error(response.error_message);
|
|
66
|
+
}
|
|
98
67
|
}
|
|
99
68
|
if (isManagement) {
|
|
100
69
|
cli_utilities_1.configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, type });
|
|
@@ -112,7 +81,7 @@ class TokensAddCommand extends cli_command_1.Command {
|
|
|
112
81
|
catch (error) {
|
|
113
82
|
cli_utilities_1.logger.error('token add error', error.message);
|
|
114
83
|
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_ADD_FAILED', { color: 'yellow' });
|
|
115
|
-
cli_utilities_1.cliux.
|
|
84
|
+
cli_utilities_1.cliux.error(error.message.message ? error.message.message : error.message);
|
|
116
85
|
}
|
|
117
86
|
}
|
|
118
87
|
}
|
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validate delivery token
|
|
3
|
-
* @param contentStackClient
|
|
4
|
-
* @param apiKey
|
|
5
|
-
* @param deliveryToken
|
|
6
|
-
* @param environment
|
|
7
|
-
* @param region
|
|
8
|
-
* @returns
|
|
9
|
-
*/
|
|
10
|
-
export declare const validateDeliveryToken: (contentStackClient: any, apiKey: string, deliveryToken: string, environment: string, region: string, host: string, branch: string) => Promise<any>;
|
|
11
1
|
/**
|
|
12
2
|
* Validate environment
|
|
13
3
|
* @param contentStackClient
|
|
@@ -16,15 +6,6 @@ export declare const validateDeliveryToken: (contentStackClient: any, apiKey: st
|
|
|
16
6
|
* @returns
|
|
17
7
|
*/
|
|
18
8
|
export declare const validateEnvironment: (contentStackClient: any, apiKey: string, environment: string) => Promise<any>;
|
|
19
|
-
/**
|
|
20
|
-
* Validate management token
|
|
21
|
-
* @param contentStackClient
|
|
22
|
-
* @param apiKey
|
|
23
|
-
* @param managementToken
|
|
24
|
-
* @returns { valid: boolean; message: any }
|
|
25
|
-
* Note: Fetching one content type using the management token to check whether it is valid or not
|
|
26
|
-
*/
|
|
27
|
-
export declare const validateManagementToken: (contentStackClient: any, apiKey: string, managementToken: string, branch: string) => Promise<any>;
|
|
28
9
|
/**
|
|
29
10
|
* Validate API key
|
|
30
11
|
* @param contentStackClient
|
|
@@ -1,62 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateAPIKey = exports.
|
|
3
|
+
exports.validateAPIKey = exports.validateEnvironment = void 0;
|
|
4
4
|
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
-
/**
|
|
6
|
-
* Validate delivery token
|
|
7
|
-
* @param contentStackClient
|
|
8
|
-
* @param apiKey
|
|
9
|
-
* @param deliveryToken
|
|
10
|
-
* @param environment
|
|
11
|
-
* @param region
|
|
12
|
-
* @returns
|
|
13
|
-
*/
|
|
14
|
-
const validateDeliveryToken = async (contentStackClient, apiKey, deliveryToken, environment, region, host, branch) => {
|
|
15
|
-
let result;
|
|
16
|
-
try {
|
|
17
|
-
const regionMap = {
|
|
18
|
-
EU: 'eu',
|
|
19
|
-
NA: 'us',
|
|
20
|
-
AZURE_NA: 'azure-na',
|
|
21
|
-
AZURE_EU: 'azure-eu',
|
|
22
|
-
};
|
|
23
|
-
const stack = contentStackClient.Stack({
|
|
24
|
-
api_key: apiKey,
|
|
25
|
-
delivery_token: deliveryToken,
|
|
26
|
-
environment,
|
|
27
|
-
region: regionMap[region],
|
|
28
|
-
host,
|
|
29
|
-
branch,
|
|
30
|
-
});
|
|
31
|
-
const parsedHost = host.replace(/^https?:\/\//, '');
|
|
32
|
-
stack.setHost(parsedHost);
|
|
33
|
-
const deliveryTokenResult = await stack.getContentTypes({ limit: 1 });
|
|
34
|
-
cli_utilities_1.logger.debug('delivery token validation result', deliveryTokenResult);
|
|
35
|
-
if (deliveryTokenResult === null || deliveryTokenResult === void 0 ? void 0 : deliveryTokenResult.content_types) {
|
|
36
|
-
result = { valid: true, message: deliveryTokenResult };
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_DELIVERY_TOKEN') };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
cli_utilities_1.logger.debug('validate delivery token error', error);
|
|
44
|
-
if (error.error_code === 109) {
|
|
45
|
-
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
|
|
46
|
-
}
|
|
47
|
-
else if (error.error_code === 141) {
|
|
48
|
-
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME') };
|
|
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
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_DELIVERY_TOKEN') };
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return result;
|
|
58
|
-
};
|
|
59
|
-
exports.validateDeliveryToken = validateDeliveryToken;
|
|
60
5
|
/**
|
|
61
6
|
* Validate environment
|
|
62
7
|
* @param contentStackClient
|
|
@@ -67,7 +12,7 @@ exports.validateDeliveryToken = validateDeliveryToken;
|
|
|
67
12
|
const validateEnvironment = async (contentStackClient, apiKey, environment) => {
|
|
68
13
|
let result;
|
|
69
14
|
try {
|
|
70
|
-
const validationResult = await contentStackClient.
|
|
15
|
+
const validationResult = await contentStackClient.Stack({ api_key: apiKey }).environment(environment).fetch();
|
|
71
16
|
cli_utilities_1.logger.debug('environment validation result', validationResult);
|
|
72
17
|
if (validationResult.name === environment) {
|
|
73
18
|
result = { valid: true, message: validationResult };
|
|
@@ -83,44 +28,6 @@ const validateEnvironment = async (contentStackClient, apiKey, environment) => {
|
|
|
83
28
|
return result;
|
|
84
29
|
};
|
|
85
30
|
exports.validateEnvironment = validateEnvironment;
|
|
86
|
-
/**
|
|
87
|
-
* Validate management token
|
|
88
|
-
* @param contentStackClient
|
|
89
|
-
* @param apiKey
|
|
90
|
-
* @param managementToken
|
|
91
|
-
* @returns { valid: boolean; message: any }
|
|
92
|
-
* Note: Fetching one content type using the management token to check whether it is valid or not
|
|
93
|
-
*/
|
|
94
|
-
const validateManagementToken = async (contentStackClient, apiKey, managementToken, branch) => {
|
|
95
|
-
var _a, _b;
|
|
96
|
-
let result;
|
|
97
|
-
try {
|
|
98
|
-
const validationResuslt = await contentStackClient.axiosInstance.get('/content_types?limit=1&include_branch=true', {
|
|
99
|
-
headers: { api_key: apiKey, authorization: managementToken, branch },
|
|
100
|
-
});
|
|
101
|
-
cli_utilities_1.logger.debug('Management validation result', validationResuslt);
|
|
102
|
-
if (validationResuslt.status === 200) {
|
|
103
|
-
result = { valid: true, message: validationResuslt };
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN') };
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
cli_utilities_1.logger.error('Failed to validate management token', error);
|
|
111
|
-
if (error.response && error.response.status === 401) {
|
|
112
|
-
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN') };
|
|
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
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return result;
|
|
122
|
-
};
|
|
123
|
-
exports.validateManagementToken = validateManagementToken;
|
|
124
31
|
/**
|
|
125
32
|
* Validate API key
|
|
126
33
|
* @param contentStackClient
|
package/messages/index.json
CHANGED
package/oclif.manifest.json
CHANGED
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.
|
|
4
|
+
"version": "1.3.12",
|
|
5
5
|
"author": "Contentstack",
|
|
6
6
|
"bugs": "https://github.com/contentstack/cli/issues",
|
|
7
7
|
"scripts": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\""
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@contentstack/cli-command": "^1.2.
|
|
26
|
-
"@contentstack/cli-utilities": "^1.
|
|
25
|
+
"@contentstack/cli-command": "^1.2.11",
|
|
26
|
+
"@contentstack/cli-utilities": "^1.5.1",
|
|
27
27
|
"chalk": "^4.0.0",
|
|
28
28
|
"debug": "^4.1.1",
|
|
29
29
|
"inquirer": "8.2.4",
|
|
@@ -85,5 +85,15 @@
|
|
|
85
85
|
"auth:tokens:remove": "RMTKN"
|
|
86
86
|
}
|
|
87
87
|
},
|
|
88
|
+
"csdxConfig": {
|
|
89
|
+
"shortCommandName": {
|
|
90
|
+
"auth:login": "LOGIN",
|
|
91
|
+
"auth:logout": "LOGOUT",
|
|
92
|
+
"auth:whoami": "WHOMAI",
|
|
93
|
+
"auth:tokens": "LSTTKN",
|
|
94
|
+
"auth:tokens:add": "ADDTKN",
|
|
95
|
+
"auth:tokens:remove": "RMVTKN"
|
|
96
|
+
}
|
|
97
|
+
},
|
|
88
98
|
"repository": "contentstack/cli"
|
|
89
|
-
}
|
|
99
|
+
}
|