@contentstack/cli-auth 0.1.1-beta.3 → 1.0.1
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/LICENSE +21 -0
- package/README.md +62 -53
- package/lib/commands/auth/login.js +56 -0
- package/lib/commands/auth/logout.js +61 -0
- package/lib/commands/auth/tokens/add.js +142 -0
- package/lib/commands/auth/tokens/index.js +54 -0
- package/lib/commands/auth/tokens/remove.js +55 -0
- package/lib/commands/auth/whoami.js +27 -0
- package/lib/config/index.js +0 -0
- package/lib/interfaces/index.js +2 -0
- package/lib/utils/auth-handler.js +118 -0
- package/lib/utils/index.js +7 -0
- package/lib/utils/interactive.js +55 -0
- package/lib/utils/tokens-validation.js +141 -0
- package/messages/index.json +46 -42
- package/oclif.manifest.json +1 -1
- package/package.json +51 -28
- package/src/commands/auth/login.js +0 -50
- package/src/commands/auth/logout.js +0 -44
- package/src/commands/auth/tokens/add.js +0 -86
- package/src/commands/auth/tokens/index.js +0 -59
- package/src/commands/auth/tokens/remove.js +0 -60
- package/src/commands/auth/whoami.js +0 -19
- package/src/hooks/validate-authtoken.js +0 -19
- package/src/util/auth-handler.js +0 -108
- package/src/util/messages.js +0 -18
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
4
|
+
const interactive_1 = require("./interactive");
|
|
5
|
+
/**
|
|
6
|
+
* @class
|
|
7
|
+
* Auth handler
|
|
8
|
+
*/
|
|
9
|
+
class AuthHandler {
|
|
10
|
+
set client(contentStackClient) {
|
|
11
|
+
this._client = contentStackClient;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
*
|
|
16
|
+
* Login into Contentstack
|
|
17
|
+
* @param {string} email Contentstack email address
|
|
18
|
+
* @param {string} password User's password for contentstack account
|
|
19
|
+
* @returns {Promise} Promise object returns authtoken on success
|
|
20
|
+
* TBD: take out the otp implementation from login and create a new method/function to handle otp
|
|
21
|
+
*/
|
|
22
|
+
async login(email, password, tfaToken) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
if (email && password) {
|
|
25
|
+
const loginPayload = { email, password };
|
|
26
|
+
if (tfaToken) {
|
|
27
|
+
loginPayload.tfa_token = tfaToken;
|
|
28
|
+
}
|
|
29
|
+
this._client
|
|
30
|
+
.login(loginPayload)
|
|
31
|
+
.then(async (result) => {
|
|
32
|
+
cli_utilities_1.logger.debug('login result', result);
|
|
33
|
+
if (result.user) {
|
|
34
|
+
resolve(result.user);
|
|
35
|
+
}
|
|
36
|
+
else if (result.error_code === 294) {
|
|
37
|
+
const otpChannel = await (0, interactive_1.askOTPChannel)();
|
|
38
|
+
// need to send sms to the mobile
|
|
39
|
+
if (otpChannel === 'sms') {
|
|
40
|
+
try {
|
|
41
|
+
await this._client.axiosInstance.post('/user/request_token_sms', { user: loginPayload });
|
|
42
|
+
cli_utilities_1.cliux.print('CLI_AUTH_LOGIN_SECURITY_CODE_SEND_SUCCESS');
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
cli_utilities_1.logger.error('Failed to send the security code', error);
|
|
46
|
+
reject(new cli_utilities_1.CLIError({ message: 'Failed to login - failed to send the security code' }));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const tfToken = await (0, interactive_1.askOTP)();
|
|
51
|
+
try {
|
|
52
|
+
resolve(await this.login(email, password, tfToken));
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
cli_utilities_1.logger.error('Failed to login with tfa token', error);
|
|
56
|
+
reject(new cli_utilities_1.CLIError({ message: 'Failed to login with the tf token' }));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
reject(new cli_utilities_1.CLIError({ message: 'No user found with the credentials' }));
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
.catch((error) => {
|
|
64
|
+
cli_utilities_1.logger.error('Failed to login', error);
|
|
65
|
+
reject(new cli_utilities_1.CLIError({ message: error.errorMessage }));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
reject(new cli_utilities_1.CLIError({ message: 'No credential found to login' }));
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Logout from Contentstack
|
|
75
|
+
* @param {string} authtoken authtoken that needs to invalidated when logging out
|
|
76
|
+
* @returns {Promise} Promise object returns response object from Contentstack
|
|
77
|
+
*/
|
|
78
|
+
async logout(authtoken) {
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
if (authtoken) {
|
|
81
|
+
this._client
|
|
82
|
+
.logout(authtoken)
|
|
83
|
+
.then(function (response) {
|
|
84
|
+
return resolve(response);
|
|
85
|
+
})
|
|
86
|
+
.catch((error) => {
|
|
87
|
+
cli_utilities_1.logger.error('Failed to logout', error);
|
|
88
|
+
return reject(new cli_utilities_1.CLIError({ message: 'Failed to logout - ' + error.message }));
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
reject(new cli_utilities_1.CLIError({ message: 'No auth token found to logout' }));
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Validate token
|
|
98
|
+
* @param {string} authtoken
|
|
99
|
+
* @returns {Promise} Promise object returns response object from Contentstack
|
|
100
|
+
*/
|
|
101
|
+
async validateAuthtoken(authtoken) {
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
if (authtoken) {
|
|
104
|
+
this._client
|
|
105
|
+
.getUser()
|
|
106
|
+
.then((user) => resolve(user))
|
|
107
|
+
.catch((error) => {
|
|
108
|
+
cli_utilities_1.logger.error('Failed to validate token', error);
|
|
109
|
+
reject(new cli_utilities_1.CLIError({ message: 'Failed to validate token - ' + error.message }));
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
reject(new cli_utilities_1.CLIError({ message: 'No auth token found to validate' }));
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.default = new AuthHandler();
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tokenValidation = exports.interactive = exports.authHandler = void 0;
|
|
4
|
+
var auth_handler_1 = require("./auth-handler");
|
|
5
|
+
Object.defineProperty(exports, "authHandler", { enumerable: true, get: function () { return auth_handler_1.default; } });
|
|
6
|
+
exports.interactive = require("./interactive");
|
|
7
|
+
exports.tokenValidation = require("./tokens-validation");
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.askTokenType = exports.askUsername = exports.askOTP = exports.askOTPChannel = exports.askPassword = void 0;
|
|
4
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
+
const askPassword = async () => {
|
|
6
|
+
return cli_utilities_1.cliux.inquire({
|
|
7
|
+
type: 'input',
|
|
8
|
+
message: 'CLI_AUTH_LOGIN_ENTER_PASSWORD',
|
|
9
|
+
name: 'password',
|
|
10
|
+
transformer: (pswd) => {
|
|
11
|
+
return '*'.repeat(pswd.length);
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
exports.askPassword = askPassword;
|
|
16
|
+
const askOTPChannel = async () => {
|
|
17
|
+
return cli_utilities_1.cliux.inquire({
|
|
18
|
+
type: 'list',
|
|
19
|
+
name: 'otpChannel',
|
|
20
|
+
message: 'CLI_AUTH_LOGIN_ASK_CHANNEL_FOR_OTP',
|
|
21
|
+
choices: [
|
|
22
|
+
{ name: 'Authy App', value: 'authy' },
|
|
23
|
+
{ name: 'SMS', value: 'sms' },
|
|
24
|
+
],
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
exports.askOTPChannel = askOTPChannel;
|
|
28
|
+
const askOTP = async () => {
|
|
29
|
+
return cli_utilities_1.cliux.inquire({
|
|
30
|
+
type: 'input',
|
|
31
|
+
message: 'CLI_AUTH_LOGIN_ENTER_SECURITY_CODE',
|
|
32
|
+
name: 'tfaToken',
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
exports.askOTP = askOTP;
|
|
36
|
+
const askUsername = async () => {
|
|
37
|
+
return cli_utilities_1.cliux.inquire({
|
|
38
|
+
type: 'input',
|
|
39
|
+
message: 'CLI_AUTH_LOGIN_ENTER_EMAIL_ADDRESS',
|
|
40
|
+
name: 'username',
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
exports.askUsername = askUsername;
|
|
44
|
+
const askTokenType = async () => {
|
|
45
|
+
return cli_utilities_1.cliux.inquire({
|
|
46
|
+
type: 'list',
|
|
47
|
+
name: 'tokenType',
|
|
48
|
+
message: 'CLI_SELECT_TOKEN_TYPE',
|
|
49
|
+
choices: [
|
|
50
|
+
{ name: 'Management Token', value: 'management' },
|
|
51
|
+
{ name: 'Delivery Token', value: 'delivery' },
|
|
52
|
+
]
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
exports.askTokenType = askTokenType;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateAPIKey = exports.validateManagementToken = exports.validateEnvironment = exports.validateDeliveryToken = void 0;
|
|
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) => {
|
|
15
|
+
var _a;
|
|
16
|
+
let result;
|
|
17
|
+
try {
|
|
18
|
+
const regionMap = {
|
|
19
|
+
EU: 'eu',
|
|
20
|
+
NA: 'us',
|
|
21
|
+
AZURE_NA: 'azure-na',
|
|
22
|
+
};
|
|
23
|
+
const stack = contentStackClient
|
|
24
|
+
.Stack({
|
|
25
|
+
api_key: apiKey,
|
|
26
|
+
delivery_token: deliveryToken,
|
|
27
|
+
environment,
|
|
28
|
+
region: regionMap[region],
|
|
29
|
+
host,
|
|
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 ((_a = deliveryTokenResult === null || deliveryTokenResult === void 0 ? void 0 : deliveryTokenResult.content_types) === null || _a === void 0 ? void 0 : _a.length) {
|
|
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 {
|
|
51
|
+
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_DELIVERY_TOKEN') };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
};
|
|
56
|
+
exports.validateDeliveryToken = validateDeliveryToken;
|
|
57
|
+
/**
|
|
58
|
+
* Validate environment
|
|
59
|
+
* @param contentStackClient
|
|
60
|
+
* @param apiKey
|
|
61
|
+
* @param environment
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
const validateEnvironment = async (contentStackClient, apiKey, environment) => {
|
|
65
|
+
let result;
|
|
66
|
+
try {
|
|
67
|
+
const validationResult = await contentStackClient.stack({ api_key: apiKey }).environment(environment).fetch();
|
|
68
|
+
cli_utilities_1.logger.debug('environment validation result', validationResult);
|
|
69
|
+
if (validationResult.name === environment) {
|
|
70
|
+
result = { valid: true, message: validationResult };
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME') };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
cli_utilities_1.logger.error('validate environment error', error);
|
|
78
|
+
result = { valid: false, message: 'CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME' };
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
};
|
|
82
|
+
exports.validateEnvironment = validateEnvironment;
|
|
83
|
+
/**
|
|
84
|
+
* Validate management token
|
|
85
|
+
* @param contentStackClient
|
|
86
|
+
* @param apiKey
|
|
87
|
+
* @param managementToken
|
|
88
|
+
* @returns { valid: boolean; message: any }
|
|
89
|
+
* Note: Fetching one content type using the management token to check whether it is valid or not
|
|
90
|
+
*/
|
|
91
|
+
const validateManagementToken = async (contentStackClient, apiKey, managementToken) => {
|
|
92
|
+
let result;
|
|
93
|
+
try {
|
|
94
|
+
const validationResuslt = await contentStackClient.axiosInstance.get('/content_types?limit=1', {
|
|
95
|
+
headers: { api_key: apiKey, authorization: managementToken },
|
|
96
|
+
});
|
|
97
|
+
cli_utilities_1.logger.debug('Management validation result', validationResuslt);
|
|
98
|
+
if (validationResuslt.status === 200) {
|
|
99
|
+
result = { valid: true, message: validationResuslt };
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN') };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
cli_utilities_1.logger.error('Failed to validate management token', error);
|
|
107
|
+
if (error.response && error.response.status === 401) {
|
|
108
|
+
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN') };
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
};
|
|
116
|
+
exports.validateManagementToken = validateManagementToken;
|
|
117
|
+
/**
|
|
118
|
+
* Validate API key
|
|
119
|
+
* @param contentStackClient
|
|
120
|
+
* @param apiKey
|
|
121
|
+
* @returns
|
|
122
|
+
*/
|
|
123
|
+
const validateAPIKey = async (contentStackClient, apiKey) => {
|
|
124
|
+
let result;
|
|
125
|
+
try {
|
|
126
|
+
const validateAPIKeyResult = await contentStackClient.stack({ api_key: apiKey }).fetch();
|
|
127
|
+
cli_utilities_1.logger.debug('api key validation result', validateAPIKeyResult);
|
|
128
|
+
if (validateAPIKeyResult.api_key === apiKey) {
|
|
129
|
+
result = { valid: true, message: validateAPIKeyResult };
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
cli_utilities_1.logger.error('validate api key error', error);
|
|
137
|
+
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
};
|
|
141
|
+
exports.validateAPIKey = validateAPIKey;
|
package/messages/index.json
CHANGED
|
@@ -1,43 +1,47 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
2
|
+
"CLI_AUTH_LOGIN_ENTER_EMAIL_ADDRESS": "Enter your email address",
|
|
3
|
+
"CLI_AUTH_LOGIN_ENTER_PASSWORD": "Enter your password",
|
|
4
|
+
"CLI_AUTH_LOGIN_SUCCESS": "Successfully logged in!!",
|
|
5
|
+
"CLI_AUTH_LOGIN_FAILED": "Login Error",
|
|
6
|
+
"CLI_AUTH_LOGIN_DESCRIPTION": "User session login",
|
|
7
|
+
"CLI_AUTH_LOGIN_FLAG_USERNAME": "User name",
|
|
8
|
+
"CLI_AUTH_LOGIN_FLAG_PASSWORD": "Password",
|
|
9
|
+
"CLI_AUTH_LOGIN_SECURITY_CODE_SEND_SUCCESS": "Security code sent to your mobile",
|
|
10
|
+
"CLI_AUTH_LOGIN_ASK_CHANNEL_FOR_SECURITY_CODE": "Two factor authentication enabled, please select a way to get the security code",
|
|
11
|
+
"CLI_AUTH_LOGIN_ENTER_SECURITY_CODE": "Please provide the security code",
|
|
12
|
+
"CLI_AUTH_LOGOUT_CONFIRM": "Are you sure you want to log out?",
|
|
13
|
+
"CLI_AUTH_LOGOUT_LOADER_START": "Logging out....",
|
|
14
|
+
"CLI_AUTH_LOGOUT_SUCCESS": "Successfully logged out",
|
|
15
|
+
"CLI_AUTH_LOGOUT_FAILED": "Error in logout, please login again",
|
|
16
|
+
"CLI_AUTH_LOGOUT_DESCRIPTION": "User session logout",
|
|
17
|
+
"CLI_AUTH_LOGOUT_FLAG_FORCE": "Force logging out for skipping the confirmation",
|
|
18
|
+
"CLI_AUTH_WHOAMI_LOGGED_IN_AS": "You are currently logged in with email",
|
|
19
|
+
"CLI_AUTH_WHOAMI_FAILED": "Failed to get the current user details",
|
|
20
|
+
"CLI_AUTH_WHOAMI_DESCRIPTION": "Display current users email address",
|
|
21
|
+
"CLI_AUTH_TOKENS_ADD_ASK_TOKEN_ALIAS": "Provide alias to store token",
|
|
22
|
+
"CLI_AUTH_TOKENS_ADD_CONFIRM_ALIAS_REPLACE": "Alias is already exists, do you want to replace?",
|
|
23
|
+
"CLI_AUTH_TOKENS_ADD_ENTER_API_KEY": "Enter the api key",
|
|
24
|
+
"CLI_AUTH_TOKENS_ADD_ENTER_TOKEN": "Enter the token",
|
|
25
|
+
"CLI_AUTH_TOKENS_ADD_ENTER_ENVIRONMENT": "Enter the environment name",
|
|
26
|
+
"CLI_AUTH_TOKENS_ADD_REPLACE_SUCCESS": "Successfully replaced the token",
|
|
27
|
+
"CLI_AUTH_TOKENS_ADD_SUCCESS": "Successfully added the token",
|
|
28
|
+
"CLI_AUTH_TOKENS_ADD_FAILED": "Failed to add the token",
|
|
29
|
+
"CLI_AUTH_TOKENS_ADD_DESCRIPTION": "Adds management/delivery tokens to your session to use it with further CLI command by default it adds management token if either of management or delivery flags are not set",
|
|
30
|
+
"CLI_AUTH_TOKENS_ADD_FLAG_DELIVERY_TOKEN": "Set this while saving delivery token",
|
|
31
|
+
"CLI_AUTH_TOKENS_ADD_FLAG_MANAGEMENT_TOKEN": "Set this while saving management token",
|
|
32
|
+
"CLI_AUTH_TOKENS_ADD_FLAG_ENVIRONMENT_NAME": "Environment name for delivery token",
|
|
33
|
+
"CLI_AUTH_TOKENS_REMOVE_SUCCESS": "Token removed successfully !!",
|
|
34
|
+
"CLI_AUTH_TOKENS_REMOVE_FAILED": "Failed to remove the selected token",
|
|
35
|
+
"CLI_AUTH_TOKENS_NOT_FOUND": "No tokens are added yet!",
|
|
36
|
+
"CLI_AUTH_TOKENS_REMOVE_SELECT_TOKEN": "Select tokens to remove",
|
|
37
|
+
"CLI_AUTH_TOKENS_REMOVE_DESCRIPTION": "Removes selected tokens",
|
|
38
|
+
"CLI_AUTH_TOKENS_LIST_NO_TOKENS": "No tokens are added. Use auth:tokens:add command to add tokens.",
|
|
39
|
+
"CLI_AUTH_TOKENS_LIST_FAILED": "Failed to list the tokens",
|
|
40
|
+
"CLI_AUTH_TOKENS_LIST_DESCRIPTION": "Lists all existing tokens added to the session",
|
|
41
|
+
"CLI_AUTH_TOKENS_VALIDATION_INVALID_DELIVERY_TOKEN": "Invalid delivery token",
|
|
42
|
+
"CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME": "Invalid environment name",
|
|
43
|
+
"CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN": "Invalid management token",
|
|
44
|
+
"CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY": "Invalid api key",
|
|
45
|
+
"CLI_AUTH_EXIT_PROCESS": "Exiting the process...",
|
|
46
|
+
"CLI_SELECT_TOKEN_TYPE": "Select the type of token to add"
|
|
47
|
+
}
|
package/oclif.manifest.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.1
|
|
1
|
+
{"version":"1.0.1","commands":{"auth:login":{"id":"auth:login","description":"User sessions login","pluginName":"@contentstack/cli-auth","pluginType":"core","aliases":["login"],"examples":["$ csdx auth:login","$ csdx auth:login -u <username>","$ csdx auth:login -u <username> -p <password>","$ csdx auth:login --username <username>","$ csdx auth:login --username <username> --password <password>"],"flags":{"username":{"name":"username","type":"option","char":"u","description":"User name","required":false},"password":{"name":"password","type":"option","char":"p","description":"Password","required":false}},"args":[]},"auth:logout":{"id":"auth:logout","description":"User session logout","pluginName":"@contentstack/cli-auth","pluginType":"core","aliases":["logout"],"examples":["$ csdx auth:logout","$ csdx auth:logout -y","$ csdx auth:logout --yes"],"flags":{"yes":{"name":"yes","type":"boolean","char":"y","description":"Force log out by skipping the confirmation","required":false,"allowNo":false},"force":{"name":"force","type":"boolean","char":"f","description":"Force log out by skipping the confirmation","hidden":true,"required":false,"allowNo":false}},"args":[]},"auth:whoami":{"id":"auth:whoami","description":"Display current users email address","pluginName":"@contentstack/cli-auth","pluginType":"core","aliases":["whoami"],"examples":["$ csdx auth:whoami"],"flags":{},"args":[]},"auth:tokens:add":{"id":"auth:tokens:add","description":"Adds management/delivery tokens to your session to use it with other CLI commands","usage":"auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]","pluginName":"@contentstack/cli-auth","pluginType":"core","aliases":[],"examples":["$ csdx auth:tokens:add","$ csdx auth:tokens:add -a <alias>","$ csdx auth:tokens:add -k <stack api key>","$ csdx auth:tokens:add --delivery","$ csdx auth:tokens:add --management","$ csdx auth:tokens:add -e <environment>","$ csdx auth:tokens:add --token <token>","$ csdx auth:tokens:add -a <alias> -k <stack api key> --management --token <management token>","$ csdx auth:tokens:add -a <alias> -k <stack api key> --delivery -e <environment> --token <delivery token>","$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --management --token <management token>","$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --delivery -e <environment> --token <delivery token>"],"flags":{"alias":{"name":"alias","type":"option","char":"a","description":"Name of the token alias"},"delivery":{"name":"delivery","type":"boolean","char":"d","description":"Set this flag to save delivery token","allowNo":false},"management":{"name":"management","type":"boolean","char":"m","description":"Set this flag to save management token","allowNo":false},"environment":{"name":"environment","type":"option","char":"e","description":"Environment name for delivery token"},"stack-api-key":{"name":"stack-api-key","type":"option","char":"k","description":"Stack API Key"},"yes":{"name":"yes","type":"boolean","char":"y","description":"Use this flag to skip confirmation","allowNo":false},"token":{"name":"token","type":"option","char":"t","description":"Add the token name"},"api-key":{"name":"api-key","type":"option","description":"API Key","hidden":true},"force":{"name":"force","type":"boolean","char":"f","description":"Force adding","hidden":true,"allowNo":false}},"args":[]},"auth:tokens":{"id":"auth:tokens","description":"Lists all existing tokens added to the session","pluginName":"@contentstack/cli-auth","pluginType":"core","aliases":["tokens"],"examples":["$ csdx auth:tokens"],"flags":{"columns":{"name":"columns","type":"option","description":"only show provided columns (comma-separated)"},"sort":{"name":"sort","type":"option","description":"property to sort by (prepend '-' for descending)"},"filter":{"name":"filter","type":"option","description":"filter property by partial string matching, ex: name=foo"},"csv":{"name":"csv","type":"boolean","description":"output is csv format [alias: --output=csv]","allowNo":false},"output":{"name":"output","type":"option","description":"output in a more machine friendly format","options":["csv","json","yaml"]},"extended":{"name":"extended","type":"boolean","char":"x","description":"show extra columns","allowNo":false},"no-truncate":{"name":"no-truncate","type":"boolean","description":"do not truncate output to fit screen","allowNo":false},"no-header":{"name":"no-header","type":"boolean","description":"hide table header from output","allowNo":false}},"args":[]},"auth:tokens:remove":{"id":"auth:tokens:remove","description":"Removes selected tokens","pluginName":"@contentstack/cli-auth","pluginType":"core","aliases":[],"examples":["$ csdx auth:tokens:remove","$ csdx auth:tokens:remove -a <alias>"],"flags":{"alias":{"name":"alias","type":"option","char":"a","description":"Token alias"},"ignore":{"name":"ignore","type":"boolean","char":"i","description":"Ignore","allowNo":false}},"args":[]}}}
|
package/package.json
CHANGED
|
@@ -1,38 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentstack/cli-auth",
|
|
3
3
|
"description": "Contentstack CLI plugin for authentication activities",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"author": "Contentstack",
|
|
6
6
|
"bugs": "https://github.com/contentstack/cli/issues",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "npm run clean && npm run compile",
|
|
9
|
+
"clean": "rm -rf ./lib && rm -rf tsconfig.build.tsbuildinfo",
|
|
10
|
+
"compile": "tsc -b tsconfig.json",
|
|
11
|
+
"postpack": "rm -f oclif.manifest.json",
|
|
12
|
+
"prepack": "npm run build && oclif-dev manifest && oclif-dev readme",
|
|
13
|
+
"version": "oclif-dev readme && git add README.md",
|
|
14
|
+
"test:report": "tsc -p test && nyc --reporter=lcov --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
|
|
15
|
+
"pretest": "tsc -p test",
|
|
16
|
+
"test": "nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
|
|
17
|
+
"posttest": "npm run lint",
|
|
18
|
+
"lint": "eslint src/**/*.ts",
|
|
19
|
+
"format": "eslint src/**/*.ts --fix"
|
|
20
|
+
},
|
|
7
21
|
"dependencies": {
|
|
8
|
-
"@contentstack/cli-command": "^
|
|
9
|
-
"@
|
|
10
|
-
"@oclif/
|
|
22
|
+
"@contentstack/cli-command": "^1.0.0",
|
|
23
|
+
"@contentstack/cli-utilities": "^1.0.0",
|
|
24
|
+
"@oclif/command": "^1.8.16",
|
|
25
|
+
"@oclif/config": "^1.18.3",
|
|
11
26
|
"chalk": "^4.0.0",
|
|
12
|
-
"cli-ux": "^5.4.6",
|
|
13
|
-
"configstore": "^5.0.1",
|
|
14
27
|
"debug": "^4.1.1",
|
|
15
|
-
"inquirer": "^7.1.0"
|
|
28
|
+
"inquirer": "^7.1.0",
|
|
29
|
+
"winston": "^3.7.2"
|
|
16
30
|
},
|
|
17
31
|
"devDependencies": {
|
|
18
32
|
"@oclif/dev-cli": "^1.22.2",
|
|
19
|
-
"@oclif/plugin-help": "^
|
|
20
|
-
"@oclif/test": "^1.2.
|
|
21
|
-
"chai": "^4.2.
|
|
22
|
-
"
|
|
33
|
+
"@oclif/plugin-help": "^5.1.12",
|
|
34
|
+
"@oclif/test": "^1.2.8",
|
|
35
|
+
"@types/chai": "^4.2.18",
|
|
36
|
+
"@types/inquirer": "^7.3.1",
|
|
37
|
+
"@types/mkdirp": "^1.0.1",
|
|
38
|
+
"@types/mocha": "^8.2.2",
|
|
39
|
+
"@types/node": "^14.14.32",
|
|
40
|
+
"@types/sinon": "^10.0.2",
|
|
41
|
+
"@types/tar": "^4.0.3",
|
|
42
|
+
"@types/winston": "^2.4.4",
|
|
43
|
+
"chai": "^4.3.4",
|
|
44
|
+
"eslint": "^8.18.0",
|
|
23
45
|
"eslint-config-oclif": "^3.1.0",
|
|
46
|
+
"eslint-config-oclif-typescript": "^0.1.0",
|
|
24
47
|
"globby": "^10.0.2",
|
|
25
|
-
"mocha": "^
|
|
26
|
-
"nyc": "^
|
|
27
|
-
"
|
|
48
|
+
"mocha": "^9.2.2",
|
|
49
|
+
"nyc": "^15.1.0",
|
|
50
|
+
"rimraf": "^2.7.1",
|
|
51
|
+
"sinon": "^11.1.1",
|
|
52
|
+
"tmp": "^0.2.1",
|
|
53
|
+
"ts-node": "^8.10.2",
|
|
54
|
+
"typescript": "^4.7.4"
|
|
28
55
|
},
|
|
29
56
|
"engines": {
|
|
30
57
|
"node": ">=8.0.0"
|
|
31
58
|
},
|
|
32
59
|
"files": [
|
|
60
|
+
"/lib",
|
|
33
61
|
"/npm-shrinkwrap.json",
|
|
34
62
|
"/oclif.manifest.json",
|
|
35
|
-
"/src",
|
|
36
63
|
"/messages"
|
|
37
64
|
],
|
|
38
65
|
"homepage": "https://github.com/contentstack/cli",
|
|
@@ -43,20 +70,16 @@
|
|
|
43
70
|
],
|
|
44
71
|
"license": "MIT",
|
|
45
72
|
"oclif": {
|
|
46
|
-
"commands": "./
|
|
73
|
+
"commands": "./lib/commands",
|
|
47
74
|
"bin": "csdx",
|
|
48
75
|
"devPlugins": [
|
|
49
76
|
"@oclif/plugin-help"
|
|
50
|
-
]
|
|
51
|
-
"repositoryPrefix": "<%- repo %>/blob/v<%- version %>/packages/auth/<%- commandPath %>"
|
|
77
|
+
]
|
|
52
78
|
},
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
"version": "oclif-dev readme && git add README.md"
|
|
61
|
-
}
|
|
62
|
-
}
|
|
79
|
+
"husky": {
|
|
80
|
+
"hooks": {
|
|
81
|
+
"pre-commit": "npm run lint"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"repository": "contentstack/cli"
|
|
85
|
+
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
const chalk = require('chalk')
|
|
2
|
-
const {Command, flags} = require('@contentstack/cli-command')
|
|
3
|
-
const {cli} = require('cli-ux')
|
|
4
|
-
const {AuthHandler} = require('../../util/auth-handler')
|
|
5
|
-
const Configstore = require('configstore')
|
|
6
|
-
const config = new Configstore('contentstack_cli')
|
|
7
|
-
const Messages = require('../../util/messages')
|
|
8
|
-
const messages = new Messages('login').msgs
|
|
9
|
-
const debug = require('debug')('csdx:auth:login')
|
|
10
|
-
|
|
11
|
-
class LoginCommand extends Command {
|
|
12
|
-
async run() {
|
|
13
|
-
const opts = {
|
|
14
|
-
contentstackClient: this.managementAPIClient, // get client added in 'this' context via cli plugin init hook
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const authHandler = new AuthHandler(opts)
|
|
18
|
-
const loginCommandFlags = this.parse(LoginCommand).flags
|
|
19
|
-
let username = loginCommandFlags.username ? loginCommandFlags.username : await cli.prompt(messages.promptEmailAddress)
|
|
20
|
-
|
|
21
|
-
const password = await cli.prompt(messages.promptPassword, {type: 'hide'})
|
|
22
|
-
try {
|
|
23
|
-
const user = await authHandler.login(username, password)
|
|
24
|
-
debug('User object ', user)
|
|
25
|
-
config.set('authtoken', user.authtoken)
|
|
26
|
-
config.set('email', user.email)
|
|
27
|
-
cli.log(chalk.green(messages.msgLoginSuccess))
|
|
28
|
-
} catch (error) {
|
|
29
|
-
debug('Error occurred: ', error)
|
|
30
|
-
if (error.message) {
|
|
31
|
-
try {
|
|
32
|
-
let res = JSON.parse(error.message)
|
|
33
|
-
cli.log(chalk.red(res.errorMessage))
|
|
34
|
-
} catch (e) {
|
|
35
|
-
cli.log(chalk.red(error.message))
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
LoginCommand.description = messages.commandDescription
|
|
43
|
-
|
|
44
|
-
LoginCommand.flags = {
|
|
45
|
-
username: flags.string({char: 'u', description: messages.flagUsernameDescription}),
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
LoginCommand.aliases = ['login']
|
|
49
|
-
|
|
50
|
-
module.exports = LoginCommand
|