@contentstack/cli-auth 1.4.1 → 1.5.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/README.md +1 -1
- package/lib/base-command.d.ts +4 -2
- package/lib/base-command.js +14 -2
- package/lib/commands/auth/login.js +21 -10
- package/lib/commands/auth/logout.js +25 -8
- package/lib/commands/auth/tokens/add.js +44 -6
- package/lib/commands/auth/tokens/index.js +13 -3
- package/lib/commands/auth/tokens/remove.js +25 -6
- package/lib/commands/auth/whoami.js +8 -5
- package/lib/interfaces/index.d.ts +10 -0
- package/lib/utils/auth-handler.d.ts +0 -3
- package/lib/utils/auth-handler.js +64 -19
- package/lib/utils/tokens-validation.js +16 -7
- package/messages/index.json +1 -1
- package/oclif.manifest.json +1 -1
- package/package.json +5 -3
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.
|
|
21
|
+
@contentstack/cli-auth/1.5.1 linux-x64 node-v22.17.1
|
|
22
22
|
$ csdx --help [COMMAND]
|
|
23
23
|
USAGE
|
|
24
24
|
$ csdx COMMAND
|
package/lib/base-command.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Command } from '@contentstack/cli-command';
|
|
2
|
-
import { Interfaces
|
|
2
|
+
import { Interfaces } from '@contentstack/cli-utilities';
|
|
3
|
+
import { Context } from './interfaces';
|
|
3
4
|
export type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
|
|
4
5
|
export type Flags<T extends typeof Command> = Interfaces.InferredFlags<(typeof BaseCommand)['baseFlags'] & T['flags']>;
|
|
5
6
|
export declare abstract class BaseCommand<T extends typeof Command> extends Command {
|
|
6
|
-
logger: LoggerService;
|
|
7
7
|
protected args: Args<T>;
|
|
8
8
|
protected flags: Flags<T>;
|
|
9
|
+
contextDetails: Context;
|
|
9
10
|
/**
|
|
10
11
|
* The `init` function initializes the command by parsing arguments and flags, registering search
|
|
11
12
|
* plugins, registering the configuration, and initializing the logger.
|
|
@@ -30,4 +31,5 @@ export declare abstract class BaseCommand<T extends typeof Command> extends Comm
|
|
|
30
31
|
* superclass, which is a promise.
|
|
31
32
|
*/
|
|
32
33
|
protected finally(_: Error | undefined): Promise<any>;
|
|
34
|
+
protected createExportContext(apiKey?: string): Context;
|
|
33
35
|
}
|
package/lib/base-command.js
CHANGED
|
@@ -10,8 +10,7 @@ class BaseCommand extends cli_command_1.Command {
|
|
|
10
10
|
*/
|
|
11
11
|
async init() {
|
|
12
12
|
await super.init();
|
|
13
|
-
|
|
14
|
-
this.logger = new cli_utilities_1.LoggerService(process.cwd(), 'cli-log');
|
|
13
|
+
this.contextDetails = Object.assign({}, this.createExportContext());
|
|
15
14
|
}
|
|
16
15
|
/**
|
|
17
16
|
* The catch function is used to handle errors from a command, either by adding custom logic or
|
|
@@ -37,5 +36,18 @@ class BaseCommand extends cli_command_1.Command {
|
|
|
37
36
|
// called after run and catch regardless of whether or not the command errored
|
|
38
37
|
return super.finally(_);
|
|
39
38
|
}
|
|
39
|
+
// Create export context object
|
|
40
|
+
createExportContext(apiKey) {
|
|
41
|
+
var _a, _b, _c;
|
|
42
|
+
return {
|
|
43
|
+
command: ((_b = (_a = this.context) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.command) || 'auth',
|
|
44
|
+
module: '',
|
|
45
|
+
userId: cli_utilities_1.configHandler.get('userUid') || '',
|
|
46
|
+
email: cli_utilities_1.configHandler.get('email') || '',
|
|
47
|
+
sessionId: (_c = this.context) === null || _c === void 0 ? void 0 : _c.sessionId,
|
|
48
|
+
apiKey: apiKey || '',
|
|
49
|
+
orgId: cli_utilities_1.configHandler.get('oauthOrgUid') || '',
|
|
50
|
+
};
|
|
51
|
+
}
|
|
40
52
|
}
|
|
41
53
|
exports.BaseCommand = BaseCommand;
|
|
@@ -5,45 +5,56 @@ const utils_1 = require("../../utils");
|
|
|
5
5
|
const base_command_1 = require("../../base-command");
|
|
6
6
|
class LoginCommand extends base_command_1.BaseCommand {
|
|
7
7
|
async run() {
|
|
8
|
-
|
|
8
|
+
cli_utilities_1.log.debug('LoginCommand run method started', this.contextDetails);
|
|
9
9
|
try {
|
|
10
|
+
cli_utilities_1.log.debug('Initializing management API client', this.contextDetails);
|
|
10
11
|
const managementAPIClient = await (0, cli_utilities_1.managementSDKClient)({ host: this.cmaHost, skipTokenValidity: true });
|
|
12
|
+
cli_utilities_1.log.debug('Management API client initialized successfully', this.contextDetails);
|
|
11
13
|
const { flags: loginFlags } = await this.parse(LoginCommand);
|
|
14
|
+
cli_utilities_1.log.debug('Token add flags parsed', Object.assign(Object.assign({}, this.contextDetails), { flags: loginFlags }));
|
|
12
15
|
utils_1.authHandler.client = managementAPIClient;
|
|
16
|
+
cli_utilities_1.log.debug('Auth handler client set', this.contextDetails);
|
|
13
17
|
const oauth = loginFlags === null || loginFlags === void 0 ? void 0 : loginFlags.oauth;
|
|
18
|
+
cli_utilities_1.log.debug(`Authentication method: ${oauth ? 'OAuth' : 'Basic'}`, this.contextDetails);
|
|
14
19
|
if (oauth === true) {
|
|
20
|
+
cli_utilities_1.log.debug('Starting OAuth authentication flow', this.contextDetails);
|
|
15
21
|
cli_utilities_1.authHandler.host = this.cmaHost;
|
|
16
22
|
await cli_utilities_1.authHandler.oauth();
|
|
23
|
+
cli_utilities_1.log.debug('OAuth authentication completed', this.contextDetails);
|
|
17
24
|
}
|
|
18
25
|
else {
|
|
26
|
+
cli_utilities_1.log.debug('Starting basic authentication flow', this.contextDetails);
|
|
19
27
|
const username = (loginFlags === null || loginFlags === void 0 ? void 0 : loginFlags.username) || (await utils_1.interactive.askUsername());
|
|
20
28
|
const password = (loginFlags === null || loginFlags === void 0 ? void 0 : loginFlags.password) || (await utils_1.interactive.askPassword());
|
|
21
|
-
|
|
29
|
+
cli_utilities_1.log.debug('Credentials obtained', Object.assign(Object.assign({}, this.contextDetails), { hasUsername: !!username, hasPassword: !!password }));
|
|
22
30
|
await this.login(username, password);
|
|
23
31
|
}
|
|
24
32
|
}
|
|
25
33
|
catch (error) {
|
|
26
|
-
|
|
27
|
-
if (typeof errorMessage === 'object' && ((_a = Object.keys(errorMessage)) === null || _a === void 0 ? void 0 : _a.length) === 0) {
|
|
28
|
-
console.log(error);
|
|
29
|
-
}
|
|
30
|
-
this.logger.error('login failed', errorMessage);
|
|
34
|
+
cli_utilities_1.log.debug('Login command failed', Object.assign(Object.assign({}, this.contextDetails), { error }));
|
|
31
35
|
cli_utilities_1.cliux.error('CLI_AUTH_LOGIN_FAILED');
|
|
32
|
-
cli_utilities_1.
|
|
36
|
+
(0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.contextDetails));
|
|
33
37
|
process.exit();
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
async login(username, password) {
|
|
41
|
+
cli_utilities_1.log.debug('Starting login process', Object.assign(Object.assign({}, this.contextDetails), { username }));
|
|
37
42
|
try {
|
|
43
|
+
cli_utilities_1.log.debug('Calling auth handler login', this.contextDetails);
|
|
38
44
|
const user = await utils_1.authHandler.login(username, password);
|
|
45
|
+
cli_utilities_1.log.debug('Auth handler login completed', Object.assign(Object.assign({}, this.contextDetails), { hasUser: !!user, hasAuthToken: !!(user === null || user === void 0 ? void 0 : user.authtoken), userEmail: user === null || user === void 0 ? void 0 : user.email }));
|
|
39
46
|
if (typeof user !== 'object' || !user.authtoken || !user.email) {
|
|
47
|
+
cli_utilities_1.log.debug('Login failed - invalid user response', Object.assign(Object.assign({}, this.contextDetails), { user }));
|
|
40
48
|
throw new cli_utilities_1.CLIError('Failed to login - invalid response');
|
|
41
49
|
}
|
|
50
|
+
cli_utilities_1.log.debug('Setting config data for basic auth', this.contextDetails);
|
|
42
51
|
await cli_utilities_1.authHandler.setConfigData('basicAuth', user);
|
|
43
|
-
|
|
44
|
-
cli_utilities_1.
|
|
52
|
+
cli_utilities_1.log.debug('Config data set successfully', this.contextDetails);
|
|
53
|
+
cli_utilities_1.log.success(cli_utilities_1.messageHandler.parse('CLI_AUTH_LOGIN_SUCCESS'), this.contextDetails);
|
|
54
|
+
cli_utilities_1.log.debug('Login process completed successfully', this.contextDetails);
|
|
45
55
|
}
|
|
46
56
|
catch (error) {
|
|
57
|
+
cli_utilities_1.log.debug('Login process failed', Object.assign(Object.assign({}, this.contextDetails), { error }));
|
|
47
58
|
throw error;
|
|
48
59
|
}
|
|
49
60
|
}
|
|
@@ -5,43 +5,60 @@ const utils_1 = require("../../utils");
|
|
|
5
5
|
const base_command_1 = require("../../base-command");
|
|
6
6
|
class LogoutCommand extends base_command_1.BaseCommand {
|
|
7
7
|
async run() {
|
|
8
|
+
cli_utilities_1.log.debug('LogoutCommand run method started', this.contextDetails);
|
|
8
9
|
const { flags: logoutFlags } = await this.parse(LogoutCommand);
|
|
10
|
+
cli_utilities_1.log.debug('Token add flags parsed', Object.assign(Object.assign({}, this.contextDetails), { flags: logoutFlags }));
|
|
9
11
|
let confirm = logoutFlags.force === true || logoutFlags.yes === true;
|
|
12
|
+
cli_utilities_1.log.debug(`Initial confirmation status: ${confirm}`, Object.assign(Object.assign({}, this.contextDetails), { force: logoutFlags.force, yes: logoutFlags.yes }));
|
|
10
13
|
if (!confirm) {
|
|
14
|
+
cli_utilities_1.log.debug('Requesting user confirmation for logout', this.contextDetails);
|
|
11
15
|
confirm = await cli_utilities_1.cliux.inquire({
|
|
12
16
|
type: 'confirm',
|
|
13
17
|
message: 'CLI_AUTH_LOGOUT_CONFIRM',
|
|
14
18
|
name: 'confirmation',
|
|
15
19
|
});
|
|
20
|
+
cli_utilities_1.log.debug(`User confirmation received: ${confirm}`, this.contextDetails);
|
|
16
21
|
}
|
|
17
22
|
try {
|
|
23
|
+
cli_utilities_1.log.debug('Initializing management API client for logout', this.contextDetails);
|
|
18
24
|
const managementAPIClient = await (0, cli_utilities_1.managementSDKClient)({ host: this.cmaHost, skipTokenValidity: true });
|
|
25
|
+
cli_utilities_1.log.debug('Management API client initialized successfully', this.contextDetails);
|
|
19
26
|
utils_1.authHandler.client = managementAPIClient;
|
|
20
|
-
|
|
27
|
+
cli_utilities_1.log.debug('Auth handler client set for logout', this.contextDetails);
|
|
28
|
+
if (confirm === true && cli_utilities_1.authHandler.isAuthenticated()) {
|
|
29
|
+
cli_utilities_1.log.debug('User confirmed logout and is authenticated, proceeding with logout', this.contextDetails);
|
|
21
30
|
cli_utilities_1.cliux.loader('CLI_AUTH_LOGOUT_LOADER_START');
|
|
22
31
|
if (await cli_utilities_1.authHandler.isAuthorisationTypeBasic()) {
|
|
23
|
-
|
|
32
|
+
cli_utilities_1.log.debug('Using basic authentication for logout', this.contextDetails);
|
|
33
|
+
const authToken = cli_utilities_1.configHandler.get('authtoken');
|
|
34
|
+
cli_utilities_1.log.debug('Retrieved auth token for logout', Object.assign(Object.assign({}, this.contextDetails), { hasAuthToken: !!authToken }));
|
|
35
|
+
await utils_1.authHandler.logout(authToken);
|
|
36
|
+
cli_utilities_1.log.debug('Basic auth logout completed', this.contextDetails);
|
|
24
37
|
}
|
|
25
38
|
else if (await cli_utilities_1.authHandler.isAuthorisationTypeOAuth()) {
|
|
39
|
+
cli_utilities_1.log.debug('Using OAuth authentication for logout', this.contextDetails);
|
|
26
40
|
await cli_utilities_1.authHandler.oauthLogout();
|
|
41
|
+
cli_utilities_1.log.debug('OAuth logout completed', this.contextDetails);
|
|
27
42
|
}
|
|
28
43
|
cli_utilities_1.cliux.loader('');
|
|
29
|
-
|
|
30
|
-
cli_utilities_1.
|
|
44
|
+
cli_utilities_1.log.success(cli_utilities_1.messageHandler.parse('CLI_AUTH_LOGOUT_SUCCESS'), this.contextDetails);
|
|
45
|
+
cli_utilities_1.log.debug('Logout process completed successfully', this.contextDetails);
|
|
31
46
|
}
|
|
32
47
|
else {
|
|
33
|
-
cli_utilities_1.
|
|
48
|
+
cli_utilities_1.log.debug('User not confirmed or not authenticated, skipping logout', Object.assign(Object.assign({}, this.contextDetails), { confirm, isAuthenticated: cli_utilities_1.authHandler.isAuthenticated() }));
|
|
49
|
+
cli_utilities_1.log.success(cli_utilities_1.messageHandler.parse('CLI_AUTH_LOGOUT_ALREADY'), this.contextDetails);
|
|
34
50
|
}
|
|
35
51
|
}
|
|
36
52
|
catch (error) {
|
|
37
|
-
|
|
38
|
-
this.logger.error('Logout failed', errorMessage);
|
|
53
|
+
cli_utilities_1.log.debug('Logout command failed', Object.assign(Object.assign({}, this.contextDetails), { error: error.message }));
|
|
39
54
|
cli_utilities_1.cliux.print('CLI_AUTH_LOGOUT_FAILED', { color: 'yellow' });
|
|
40
|
-
cli_utilities_1.
|
|
55
|
+
(0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.contextDetails));
|
|
41
56
|
}
|
|
42
57
|
finally {
|
|
43
58
|
if (confirm === true) {
|
|
59
|
+
cli_utilities_1.log.debug('Setting config data for logout', this.contextDetails);
|
|
44
60
|
await cli_utilities_1.authHandler.setConfigData('logout');
|
|
61
|
+
cli_utilities_1.log.debug('Config data set for logout', this.contextDetails);
|
|
45
62
|
}
|
|
46
63
|
}
|
|
47
64
|
}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
4
|
-
const interactive_1 = require("../../../utils/interactive");
|
|
5
4
|
const base_command_1 = require("../../../base-command");
|
|
5
|
+
const interactive_1 = require("../../../utils/interactive");
|
|
6
6
|
class TokensAddCommand extends base_command_1.BaseCommand {
|
|
7
7
|
async run() {
|
|
8
|
+
cli_utilities_1.log.debug('TokensAddCommand run method started', this.contextDetails);
|
|
9
|
+
this.contextDetails.module = 'tokens-add';
|
|
8
10
|
const { flags: addTokenFlags } = await this.parse(TokensAddCommand);
|
|
11
|
+
cli_utilities_1.log.debug('Token add flags parsed', Object.assign(Object.assign({}, this.contextDetails), { flags: addTokenFlags }));
|
|
9
12
|
let isAliasExist = false;
|
|
10
13
|
const skipAliasReplaceConfirmation = addTokenFlags.force || addTokenFlags.yes;
|
|
11
14
|
let alias = addTokenFlags.alias;
|
|
@@ -15,72 +18,107 @@ class TokensAddCommand extends base_command_1.BaseCommand {
|
|
|
15
18
|
let isManagement = addTokenFlags.management;
|
|
16
19
|
let environment = addTokenFlags.environment;
|
|
17
20
|
const configKeyTokens = 'tokens';
|
|
21
|
+
cli_utilities_1.log.debug('Initial token parameters', Object.assign(Object.assign({}, this.contextDetails), { alias, hasApiKey: !!apiKey, hasToken: !!token, isDelivery,
|
|
22
|
+
isManagement,
|
|
23
|
+
environment,
|
|
24
|
+
skipAliasReplaceConfirmation }));
|
|
18
25
|
if (!isDelivery && !isManagement && !Boolean(environment)) {
|
|
26
|
+
cli_utilities_1.log.debug('No token type specified, requesting user input', this.contextDetails);
|
|
19
27
|
let tokenType = await (0, interactive_1.askTokenType)();
|
|
20
28
|
isDelivery = tokenType === 'delivery';
|
|
21
29
|
isManagement = tokenType === 'management';
|
|
30
|
+
cli_utilities_1.log.debug(`Token type selected: ${tokenType}`, this.contextDetails);
|
|
22
31
|
}
|
|
23
32
|
const type = isDelivery || Boolean(environment) ? 'delivery' : 'management';
|
|
24
|
-
|
|
33
|
+
cli_utilities_1.log.debug(`Final token type determined: ${type}`, this.contextDetails);
|
|
25
34
|
try {
|
|
26
35
|
if (!alias) {
|
|
36
|
+
cli_utilities_1.log.debug('No alias provided, requesting user input', this.contextDetails);
|
|
27
37
|
alias = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ASK_TOKEN_ALIAS', name: 'alias' });
|
|
38
|
+
cli_utilities_1.log.debug(`Alias obtained: ${alias}`, this.contextDetails);
|
|
28
39
|
}
|
|
29
40
|
isAliasExist = Boolean(cli_utilities_1.configHandler.get(`${configKeyTokens}.${alias}`)); // get to Check if alias already present
|
|
41
|
+
cli_utilities_1.log.debug(`Checking if alias exists: ${isAliasExist}`, Object.assign(Object.assign({}, this.contextDetails), { alias }));
|
|
30
42
|
if (isAliasExist && !skipAliasReplaceConfirmation) {
|
|
43
|
+
cli_utilities_1.log.debug('Alias exists and confirmation required, requesting user input', this.contextDetails);
|
|
31
44
|
const shouldAliasReplace = await cli_utilities_1.cliux.inquire({
|
|
32
45
|
type: 'confirm',
|
|
33
46
|
message: `CLI_AUTH_TOKENS_ADD_CONFIRM_ALIAS_REPLACE`,
|
|
34
47
|
name: 'confirm',
|
|
35
48
|
});
|
|
49
|
+
cli_utilities_1.log.debug(`Alias replace confirmation: ${shouldAliasReplace}`, this.contextDetails);
|
|
36
50
|
if (!shouldAliasReplace) {
|
|
37
|
-
|
|
51
|
+
cli_utilities_1.log.debug('User declined alias replacement, exiting', this.contextDetails);
|
|
52
|
+
cli_utilities_1.log.info('Exiting from the process of replacing the token', this.contextDetails);
|
|
38
53
|
cli_utilities_1.cliux.print('CLI_AUTH_EXIT_PROCESS');
|
|
39
54
|
return;
|
|
40
55
|
}
|
|
41
56
|
}
|
|
42
57
|
if (!apiKey) {
|
|
58
|
+
cli_utilities_1.log.debug('No API key provided, requesting user input', this.contextDetails);
|
|
43
59
|
apiKey = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_API_KEY', name: 'apiKey' });
|
|
60
|
+
cli_utilities_1.log.debug('API key obtained', Object.assign(Object.assign({}, this.contextDetails), { hasApiKey: !!apiKey }));
|
|
44
61
|
}
|
|
45
62
|
if (!token) {
|
|
63
|
+
cli_utilities_1.log.debug('No token provided, requesting user input', this.contextDetails);
|
|
46
64
|
token = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_TOKEN', name: 'token' });
|
|
65
|
+
cli_utilities_1.log.debug('Token obtained', Object.assign(Object.assign({}, this.contextDetails), { hasToken: !!token }));
|
|
47
66
|
}
|
|
48
67
|
if (isDelivery && !environment) {
|
|
68
|
+
cli_utilities_1.log.debug('Delivery token requires environment, requesting user input', this.contextDetails);
|
|
49
69
|
environment = await cli_utilities_1.cliux.inquire({
|
|
50
70
|
type: 'input',
|
|
51
71
|
message: 'CLI_AUTH_TOKENS_ADD_ENTER_ENVIRONMENT',
|
|
52
72
|
name: 'env',
|
|
53
73
|
});
|
|
74
|
+
cli_utilities_1.log.debug(`Environment obtained: ${environment}`, this.contextDetails);
|
|
75
|
+
}
|
|
76
|
+
let msg = `Adding ${type} token with alias: ${alias}, apiKey: ${apiKey}`;
|
|
77
|
+
if (environment) {
|
|
78
|
+
msg += `, environment: ${environment}`;
|
|
54
79
|
}
|
|
80
|
+
cli_utilities_1.log.info(msg, this.contextDetails);
|
|
55
81
|
if (type === 'management') {
|
|
82
|
+
cli_utilities_1.log.debug('Validating management token', Object.assign(Object.assign({}, this.contextDetails), { apiKeyStatus: apiKey ? 'provided' : 'not-provided' }));
|
|
56
83
|
// FIXME - Once the SDK refresh token issue is resolved, need to revert this back to SDK call
|
|
57
84
|
const httpClient = new cli_utilities_1.HttpClient({ headers: { api_key: apiKey, authorization: token } });
|
|
85
|
+
cli_utilities_1.log.debug('Making management token validation API call', this.contextDetails);
|
|
58
86
|
const response = (await httpClient.get(`https://${this.cmaHost}/v3/environments?limit=1`)).data;
|
|
87
|
+
cli_utilities_1.log.debug('Management token validation response received', Object.assign(Object.assign({}, this.contextDetails), { response }));
|
|
59
88
|
if ((response === null || response === void 0 ? void 0 : response.error_code) === 105) {
|
|
89
|
+
cli_utilities_1.log.debug('Management token validation failed - invalid token', this.contextDetails);
|
|
60
90
|
throw new Error(cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN'));
|
|
61
91
|
}
|
|
62
92
|
else if (response === null || response === void 0 ? void 0 : response.error_message) {
|
|
93
|
+
cli_utilities_1.log.debug('Management token validation failed with error message', Object.assign(Object.assign({}, this.contextDetails), { errorMessage: response.error_message }));
|
|
63
94
|
throw new Error(response.error_message);
|
|
64
95
|
}
|
|
96
|
+
cli_utilities_1.log.debug('Management token validation successful', this.contextDetails);
|
|
65
97
|
}
|
|
98
|
+
cli_utilities_1.log.debug('Saving token to configuration', Object.assign(Object.assign({}, this.contextDetails), { alias,
|
|
99
|
+
type, hasEnvironment: !!environment }));
|
|
66
100
|
if (isManagement) {
|
|
67
101
|
cli_utilities_1.configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, type });
|
|
102
|
+
cli_utilities_1.log.debug('Management token saved to configuration', this.contextDetails);
|
|
68
103
|
}
|
|
69
104
|
else {
|
|
70
105
|
cli_utilities_1.configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, environment, type });
|
|
106
|
+
cli_utilities_1.log.debug('Delivery token saved to configuration', this.contextDetails);
|
|
71
107
|
}
|
|
72
108
|
if (isAliasExist) {
|
|
109
|
+
cli_utilities_1.log.debug('Token replaced successfully', this.contextDetails);
|
|
73
110
|
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_ADD_REPLACE_SUCCESS');
|
|
74
111
|
}
|
|
75
112
|
else {
|
|
113
|
+
cli_utilities_1.log.debug('Token added successfully', this.contextDetails);
|
|
76
114
|
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_ADD_SUCCESS');
|
|
77
115
|
}
|
|
116
|
+
cli_utilities_1.log.debug('Token add process completed successfully', this.contextDetails);
|
|
78
117
|
}
|
|
79
118
|
catch (error) {
|
|
80
|
-
|
|
81
|
-
this.logger.error('token add error', errorMessage);
|
|
119
|
+
cli_utilities_1.log.debug('Token add process failed', Object.assign(Object.assign({}, this.contextDetails), { error }));
|
|
82
120
|
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_ADD_FAILED', { color: 'yellow' });
|
|
83
|
-
cli_utilities_1.
|
|
121
|
+
(0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.contextDetails));
|
|
84
122
|
}
|
|
85
123
|
}
|
|
86
124
|
}
|
|
@@ -4,10 +4,15 @@ const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
|
4
4
|
const base_command_1 = require("../../../base-command");
|
|
5
5
|
class TokensListCommand extends base_command_1.BaseCommand {
|
|
6
6
|
async run() {
|
|
7
|
+
cli_utilities_1.log.debug('TokensListCommand run method started', this.contextDetails);
|
|
8
|
+
this.contextDetails.module = 'tokens-list';
|
|
7
9
|
try {
|
|
10
|
+
cli_utilities_1.log.debug('Retrieving tokens from configuration', this.contextDetails);
|
|
8
11
|
const managementTokens = cli_utilities_1.configHandler.get('tokens');
|
|
12
|
+
cli_utilities_1.log.debug('Tokens retrieved from configuration', Object.assign(Object.assign({}, this.contextDetails), { tokenCount: managementTokens ? Object.keys(managementTokens).length : 0 }));
|
|
9
13
|
const tokens = [];
|
|
10
14
|
if (managementTokens && Object.keys(managementTokens).length > 0) {
|
|
15
|
+
cli_utilities_1.log.debug('Processing tokens for display', this.contextDetails);
|
|
11
16
|
Object.keys(managementTokens).forEach(function (item) {
|
|
12
17
|
tokens.push({
|
|
13
18
|
alias: item,
|
|
@@ -16,8 +21,10 @@ class TokensListCommand extends base_command_1.BaseCommand {
|
|
|
16
21
|
environment: managementTokens[item].environment ? managementTokens[item].environment : '-',
|
|
17
22
|
type: managementTokens[item].type,
|
|
18
23
|
});
|
|
24
|
+
cli_utilities_1.log.debug(`Token processed: ${item}`, { tokenType: managementTokens[item].type });
|
|
19
25
|
});
|
|
20
26
|
const { flags } = await this.parse(TokensListCommand);
|
|
27
|
+
cli_utilities_1.log.debug('Tokens list flags parsed', Object.assign(Object.assign({}, this.contextDetails), { flags }));
|
|
21
28
|
const headers = [
|
|
22
29
|
{
|
|
23
30
|
value: 'alias',
|
|
@@ -35,17 +42,20 @@ class TokensListCommand extends base_command_1.BaseCommand {
|
|
|
35
42
|
value: 'type',
|
|
36
43
|
},
|
|
37
44
|
];
|
|
45
|
+
cli_utilities_1.log.debug('Displaying tokens table', Object.assign(Object.assign({}, this.contextDetails), { tokenCount: tokens.length }));
|
|
38
46
|
cli_utilities_1.cliux.table(headers, tokens, flags);
|
|
47
|
+
cli_utilities_1.log.debug('Tokens table displayed successfully', this.contextDetails);
|
|
39
48
|
}
|
|
40
49
|
else {
|
|
50
|
+
cli_utilities_1.log.debug('No tokens found in configuration', this.contextDetails);
|
|
41
51
|
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS');
|
|
42
52
|
}
|
|
53
|
+
cli_utilities_1.log.debug('Tokens list command completed successfully', this.contextDetails);
|
|
43
54
|
}
|
|
44
55
|
catch (error) {
|
|
45
|
-
|
|
46
|
-
this.logger.error('Token list error', errorMessage);
|
|
56
|
+
cli_utilities_1.log.debug('Tokens list command failed', Object.assign(Object.assign({}, this.contextDetails), { error }));
|
|
47
57
|
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' });
|
|
48
|
-
cli_utilities_1.
|
|
58
|
+
(0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.contextDetails));
|
|
49
59
|
}
|
|
50
60
|
}
|
|
51
61
|
}
|
|
@@ -4,49 +4,68 @@ const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
|
4
4
|
const base_command_1 = require("../../../base-command");
|
|
5
5
|
class TokensRemoveCommand extends base_command_1.BaseCommand {
|
|
6
6
|
async run() {
|
|
7
|
+
cli_utilities_1.log.debug('TokensRemoveCommand run method started', this.contextDetails);
|
|
8
|
+
this.contextDetails.module = 'tokens-remove';
|
|
7
9
|
const { flags: removeTokenFlags } = await this.parse(TokensRemoveCommand);
|
|
10
|
+
cli_utilities_1.log.debug('Token remove flags parsed', Object.assign(Object.assign({}, this.contextDetails), { flags: removeTokenFlags }));
|
|
8
11
|
const alias = removeTokenFlags.alias;
|
|
9
12
|
const ignore = removeTokenFlags.ignore;
|
|
13
|
+
cli_utilities_1.log.debug('Token removal parameters', Object.assign(Object.assign({}, this.contextDetails), { alias, ignore }));
|
|
10
14
|
try {
|
|
15
|
+
cli_utilities_1.log.debug('Retrieving token from configuration', Object.assign(Object.assign({}, this.contextDetails), { alias }));
|
|
11
16
|
const token = cli_utilities_1.configHandler.get(`tokens.${alias}`);
|
|
17
|
+
cli_utilities_1.log.debug('Token retrieved from configuration', Object.assign(Object.assign({}, this.contextDetails), { hasToken: !!token, tokenType: token === null || token === void 0 ? void 0 : token.type }));
|
|
12
18
|
const tokens = cli_utilities_1.configHandler.get('tokens');
|
|
19
|
+
cli_utilities_1.log.debug('All tokens retrieved from configuration', Object.assign(Object.assign({}, this.contextDetails), { tokenCount: tokens ? Object.keys(tokens).length : 0 }));
|
|
13
20
|
const tokenOptions = [];
|
|
14
21
|
if (token || ignore) {
|
|
22
|
+
cli_utilities_1.log.debug('Token found or ignore flag set, proceeding with removal', Object.assign(Object.assign({}, this.contextDetails), { hasToken: !!token, ignore }));
|
|
15
23
|
cli_utilities_1.configHandler.delete(`tokens.${alias}`);
|
|
24
|
+
cli_utilities_1.log.debug('Token removed from configuration', Object.assign(Object.assign({}, this.contextDetails), { alias }));
|
|
16
25
|
return cli_utilities_1.cliux.success(`CLI_AUTH_TOKENS_REMOVE_SUCCESS`);
|
|
17
26
|
}
|
|
18
27
|
if (tokens && Object.keys(tokens).length > 0) {
|
|
28
|
+
cli_utilities_1.log.debug('Building token options for user selection', this.contextDetails);
|
|
19
29
|
Object.keys(tokens).forEach(function (item) {
|
|
20
|
-
|
|
30
|
+
const tokenOption = `${item}: ${tokens[item].token} : ${tokens[item].apiKey}${tokens[item].environment ? ' : ' + tokens[item].environment + ' ' : ''}: ${tokens[item].type}`;
|
|
31
|
+
tokenOptions.push(tokenOption);
|
|
32
|
+
cli_utilities_1.log.debug(`Token option added: ${item}`, { tokenType: tokens[item].type });
|
|
21
33
|
});
|
|
34
|
+
cli_utilities_1.log.debug(`Token options built: ${tokenOptions.length} options`, this.contextDetails);
|
|
22
35
|
}
|
|
23
36
|
else {
|
|
37
|
+
cli_utilities_1.log.debug('No tokens found in configuration', this.contextDetails);
|
|
24
38
|
return cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_NOT_FOUND');
|
|
25
39
|
}
|
|
40
|
+
cli_utilities_1.log.debug('Requesting user to select tokens for removal', this.contextDetails);
|
|
26
41
|
const selectedTokens = await cli_utilities_1.cliux.inquire({
|
|
27
42
|
name: 'selectedTokens',
|
|
28
43
|
message: 'CLI_AUTH_TOKENS_REMOVE_SELECT_TOKEN',
|
|
29
44
|
type: 'checkbox',
|
|
30
45
|
choices: tokenOptions,
|
|
31
46
|
});
|
|
47
|
+
cli_utilities_1.log.debug(`User selected ${selectedTokens.length} tokens for removal`, Object.assign(Object.assign({}, this.contextDetails), { selectedTokens }));
|
|
32
48
|
if (selectedTokens.length === 0) {
|
|
49
|
+
cli_utilities_1.log.debug('No tokens selected for removal, exiting', this.contextDetails);
|
|
33
50
|
return;
|
|
34
51
|
}
|
|
35
52
|
selectedTokens.forEach((ele) => {
|
|
36
|
-
|
|
53
|
+
cli_utilities_1.log.info(`Selected token: ${ele}`, this.contextDetails);
|
|
37
54
|
});
|
|
55
|
+
cli_utilities_1.log.debug('Removing selected tokens from configuration', this.contextDetails);
|
|
38
56
|
selectedTokens.forEach((element) => {
|
|
39
57
|
const selectedToken = element.split(':')[0];
|
|
58
|
+
cli_utilities_1.log.debug(`Removing token: ${selectedToken}`, this.contextDetails);
|
|
40
59
|
cli_utilities_1.configHandler.delete(`tokens.${selectedToken}`);
|
|
41
60
|
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_REMOVE_SUCCESS');
|
|
42
|
-
|
|
61
|
+
cli_utilities_1.log.info(`Token removed: ${selectedToken}`, this.contextDetails);
|
|
43
62
|
});
|
|
63
|
+
cli_utilities_1.log.debug('Token removal process completed successfully', this.contextDetails);
|
|
44
64
|
}
|
|
45
65
|
catch (error) {
|
|
46
|
-
|
|
47
|
-
this.logger.error('Token remove error', errorMessage);
|
|
66
|
+
cli_utilities_1.log.debug('Token removal process failed', Object.assign(Object.assign({}, this.contextDetails), { error }));
|
|
48
67
|
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_REMOVE_FAILED', { color: 'yellow' });
|
|
49
|
-
cli_utilities_1.
|
|
68
|
+
(0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.contextDetails));
|
|
50
69
|
}
|
|
51
70
|
}
|
|
52
71
|
}
|
|
@@ -4,21 +4,24 @@ const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
|
4
4
|
const base_command_1 = require("../../base-command");
|
|
5
5
|
class WhoamiCommand extends base_command_1.BaseCommand {
|
|
6
6
|
async run() {
|
|
7
|
+
cli_utilities_1.log.debug('WhoamiCommand run method started', this.contextDetails);
|
|
7
8
|
try {
|
|
9
|
+
cli_utilities_1.log.debug('Checking user email from context', Object.assign(Object.assign({}, this.contextDetails), { hasEmail: !!this.email }));
|
|
8
10
|
if (this.email) {
|
|
11
|
+
cli_utilities_1.log.debug('User email found, displaying user information', Object.assign(Object.assign({}, this.contextDetails), { email: this.email }));
|
|
9
12
|
cli_utilities_1.cliux.print('CLI_AUTH_WHOAMI_LOGGED_IN_AS', { color: 'white' });
|
|
10
13
|
cli_utilities_1.cliux.print(this.email, { color: 'green' });
|
|
11
|
-
|
|
14
|
+
cli_utilities_1.log.debug('Whoami command completed successfully', this.contextDetails);
|
|
12
15
|
}
|
|
13
16
|
else {
|
|
14
|
-
cli_utilities_1.
|
|
17
|
+
cli_utilities_1.log.debug('No user email found in context', this.contextDetails);
|
|
18
|
+
cli_utilities_1.log.error(cli_utilities_1.messageHandler.parse('CLI_AUTH_WHOAMI_FAILED'), this.contextDetails);
|
|
15
19
|
}
|
|
16
20
|
}
|
|
17
21
|
catch (error) {
|
|
18
|
-
|
|
19
|
-
this.logger.error('whoami error', errorMessage);
|
|
22
|
+
cli_utilities_1.log.debug('Whoami command failed', Object.assign(Object.assign({}, this.contextDetails), { error }));
|
|
20
23
|
cli_utilities_1.cliux.print('CLI_AUTH_WHOAMI_FAILED', { color: 'yellow' });
|
|
21
|
-
cli_utilities_1.
|
|
24
|
+
(0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.contextDetails));
|
|
22
25
|
}
|
|
23
26
|
}
|
|
24
27
|
}
|
|
@@ -18,3 +18,13 @@ export interface User {
|
|
|
18
18
|
email: string;
|
|
19
19
|
authtoken: string;
|
|
20
20
|
}
|
|
21
|
+
export interface Context {
|
|
22
|
+
command: string;
|
|
23
|
+
module: string;
|
|
24
|
+
userId: string | undefined;
|
|
25
|
+
email: string | undefined;
|
|
26
|
+
sessionId: string | undefined;
|
|
27
|
+
clientId?: string | undefined;
|
|
28
|
+
apiKey: string;
|
|
29
|
+
orgId: string;
|
|
30
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { User } from '../interfaces';
|
|
2
|
-
import { LoggerService } from '@contentstack/cli-utilities';
|
|
3
2
|
/**
|
|
4
3
|
* @class
|
|
5
4
|
* Auth handler
|
|
@@ -7,10 +6,8 @@ import { LoggerService } from '@contentstack/cli-utilities';
|
|
|
7
6
|
declare class AuthHandler {
|
|
8
7
|
private _client;
|
|
9
8
|
private _host;
|
|
10
|
-
logger: LoggerService;
|
|
11
9
|
set client(contentStackClient: any);
|
|
12
10
|
set host(contentStackHost: any);
|
|
13
|
-
initLog(): void;
|
|
14
11
|
/**
|
|
15
12
|
*
|
|
16
13
|
*
|
|
@@ -2,21 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
4
4
|
const interactive_1 = require("./interactive");
|
|
5
|
-
const cli_utilities_2 = require("@contentstack/cli-utilities");
|
|
6
5
|
/**
|
|
7
6
|
* @class
|
|
8
7
|
* Auth handler
|
|
9
8
|
*/
|
|
10
9
|
class AuthHandler {
|
|
11
10
|
set client(contentStackClient) {
|
|
11
|
+
cli_utilities_1.log.debug('Setting ContentStack client', { module: 'auth-handler' });
|
|
12
12
|
this._client = contentStackClient;
|
|
13
13
|
}
|
|
14
14
|
set host(contentStackHost) {
|
|
15
|
+
cli_utilities_1.log.debug(`Setting ContentStack host: ${contentStackHost}`, { module: 'auth-handler' });
|
|
15
16
|
this._host = contentStackHost;
|
|
16
17
|
}
|
|
17
|
-
initLog() {
|
|
18
|
-
this.logger = new cli_utilities_2.LoggerService(process.cwd(), 'cli-log');
|
|
19
|
-
}
|
|
20
18
|
/**
|
|
21
19
|
*
|
|
22
20
|
*
|
|
@@ -27,53 +25,90 @@ class AuthHandler {
|
|
|
27
25
|
* TBD: take out the otp implementation from login and create a new method/function to handle otp
|
|
28
26
|
*/
|
|
29
27
|
async login(email, password, tfaToken) {
|
|
30
|
-
|
|
28
|
+
const hasCredentials = !!password;
|
|
29
|
+
const hasTfaToken = !!tfaToken;
|
|
30
|
+
cli_utilities_1.log.debug('Starting login process', {
|
|
31
|
+
module: 'auth-handler',
|
|
32
|
+
email,
|
|
33
|
+
hasCredentials,
|
|
34
|
+
hasTfaToken,
|
|
35
|
+
});
|
|
31
36
|
return new Promise((resolve, reject) => {
|
|
32
37
|
if (email && password) {
|
|
33
38
|
const loginPayload = { email, password };
|
|
34
39
|
if (tfaToken) {
|
|
35
40
|
loginPayload.tfa_token = tfaToken;
|
|
41
|
+
cli_utilities_1.log.debug('Adding TFA token to login payload', { module: 'auth-handler' });
|
|
36
42
|
}
|
|
43
|
+
const hasCredentials = !!password;
|
|
44
|
+
const hasTfaTokenPresent = !!tfaToken;
|
|
45
|
+
cli_utilities_1.log.debug('Making login API call', {
|
|
46
|
+
module: 'auth-handler',
|
|
47
|
+
payload: { email, hasCredentials, hasTfaTokenPresent },
|
|
48
|
+
});
|
|
37
49
|
this._client
|
|
38
50
|
.login(loginPayload)
|
|
39
51
|
.then(async (result) => {
|
|
40
|
-
|
|
52
|
+
cli_utilities_1.log.debug('Login API response received', {
|
|
53
|
+
module: 'auth-handler',
|
|
54
|
+
hasUser: !!result.user,
|
|
55
|
+
errorCode: result.error_code,
|
|
56
|
+
});
|
|
41
57
|
if (result.user) {
|
|
58
|
+
cli_utilities_1.log.debug('Login successful, user found', { module: 'auth-handler', userEmail: result.user.email });
|
|
42
59
|
resolve(result.user);
|
|
43
60
|
}
|
|
44
61
|
else if (result.error_code === 294) {
|
|
62
|
+
cli_utilities_1.log.debug('TFA required, requesting OTP channel', { module: 'auth-handler' });
|
|
45
63
|
const otpChannel = await (0, interactive_1.askOTPChannel)();
|
|
64
|
+
cli_utilities_1.log.debug(`OTP channel selected: ${otpChannel}`, { module: 'auth-handler' });
|
|
46
65
|
// need to send sms to the mobile
|
|
47
66
|
if (otpChannel === 'sms') {
|
|
67
|
+
cli_utilities_1.log.debug('Sending SMS OTP request', { module: 'auth-handler' });
|
|
48
68
|
try {
|
|
49
69
|
await this._client.axiosInstance.post('/user/request_token_sms', { user: loginPayload });
|
|
70
|
+
cli_utilities_1.log.debug('SMS OTP request successful', { module: 'auth-handler' });
|
|
50
71
|
cli_utilities_1.cliux.print('CLI_AUTH_LOGIN_SECURITY_CODE_SEND_SUCCESS');
|
|
51
72
|
}
|
|
52
73
|
catch (error) {
|
|
53
|
-
|
|
54
|
-
|
|
74
|
+
cli_utilities_1.log.debug('SMS OTP request failed', { module: 'auth-handler', error });
|
|
75
|
+
const err = cli_utilities_1.cliErrorHandler.classifyError(error);
|
|
76
|
+
reject(err);
|
|
55
77
|
return;
|
|
56
78
|
}
|
|
57
79
|
}
|
|
80
|
+
cli_utilities_1.log.debug('Requesting OTP input from user', { module: 'auth-handler' });
|
|
58
81
|
const tfToken = await (0, interactive_1.askOTP)();
|
|
82
|
+
cli_utilities_1.log.debug('OTP received, retrying login', { module: 'auth-handler' });
|
|
59
83
|
try {
|
|
60
84
|
resolve(await this.login(email, password, tfToken));
|
|
61
85
|
}
|
|
62
86
|
catch (error) {
|
|
63
|
-
|
|
64
|
-
|
|
87
|
+
cli_utilities_1.log.debug('Login with TFA token failed', { module: 'auth-handler', error });
|
|
88
|
+
const err = cli_utilities_1.cliErrorHandler.classifyError(error);
|
|
89
|
+
reject(err);
|
|
90
|
+
return;
|
|
65
91
|
}
|
|
66
92
|
}
|
|
67
93
|
else {
|
|
94
|
+
cli_utilities_1.log.debug('Login failed - no user found', { module: 'auth-handler', result });
|
|
68
95
|
reject(new cli_utilities_1.CLIError({ message: 'No user found with the credentials' }));
|
|
69
96
|
}
|
|
70
97
|
})
|
|
71
98
|
.catch((error) => {
|
|
72
|
-
|
|
73
|
-
|
|
99
|
+
cli_utilities_1.log.debug('Login API call failed', { module: 'auth-handler', error: error.message || error });
|
|
100
|
+
const err = cli_utilities_1.cliErrorHandler.classifyError(error);
|
|
101
|
+
reject(err);
|
|
74
102
|
});
|
|
75
103
|
}
|
|
76
104
|
else {
|
|
105
|
+
const hasEmail = !!email;
|
|
106
|
+
const hasCredentials = !!password;
|
|
107
|
+
cli_utilities_1.log.debug('Login failed - missing credentials', {
|
|
108
|
+
module: 'auth-handler',
|
|
109
|
+
hasEmail,
|
|
110
|
+
hasCredentials,
|
|
111
|
+
});
|
|
77
112
|
reject(new cli_utilities_1.CLIError({ message: 'No credential found to login' }));
|
|
78
113
|
}
|
|
79
114
|
});
|
|
@@ -84,20 +119,24 @@ class AuthHandler {
|
|
|
84
119
|
* @returns {Promise} Promise object returns response object from Contentstack
|
|
85
120
|
*/
|
|
86
121
|
async logout(authtoken) {
|
|
87
|
-
|
|
122
|
+
cli_utilities_1.log.debug('Starting logout process', { module: 'auth-handler', hasAuthToken: !!authtoken });
|
|
88
123
|
return new Promise((resolve, reject) => {
|
|
89
124
|
if (authtoken) {
|
|
125
|
+
cli_utilities_1.log.debug('Making logout API call', { module: 'auth-handler' });
|
|
90
126
|
this._client
|
|
91
127
|
.logout(authtoken)
|
|
92
128
|
.then(function (response) {
|
|
129
|
+
cli_utilities_1.log.debug('Logout API call successful', { module: 'auth-handler', response });
|
|
93
130
|
return resolve(response);
|
|
94
131
|
})
|
|
95
132
|
.catch((error) => {
|
|
96
|
-
|
|
97
|
-
|
|
133
|
+
cli_utilities_1.log.debug('Logout API call failed', { module: 'auth-handler', error: error.message });
|
|
134
|
+
const err = cli_utilities_1.cliErrorHandler.classifyError(error);
|
|
135
|
+
reject(err);
|
|
98
136
|
});
|
|
99
137
|
}
|
|
100
138
|
else {
|
|
139
|
+
cli_utilities_1.log.debug('Logout failed - no auth token provided', { module: 'auth-handler' });
|
|
101
140
|
reject(new cli_utilities_1.CLIError({ message: 'No auth token found to logout' }));
|
|
102
141
|
}
|
|
103
142
|
});
|
|
@@ -108,18 +147,24 @@ class AuthHandler {
|
|
|
108
147
|
* @returns {Promise} Promise object returns response object from Contentstack
|
|
109
148
|
*/
|
|
110
149
|
async validateAuthtoken(authtoken) {
|
|
111
|
-
|
|
150
|
+
cli_utilities_1.log.debug('Starting token validation', { module: 'auth-handler', hasAuthToken: !!authtoken });
|
|
112
151
|
return new Promise((resolve, reject) => {
|
|
113
152
|
if (authtoken) {
|
|
153
|
+
cli_utilities_1.log.debug('Making token validation API call', { module: 'auth-handler' });
|
|
114
154
|
this._client
|
|
115
155
|
.getUser()
|
|
116
|
-
.then((user) =>
|
|
156
|
+
.then((user) => {
|
|
157
|
+
cli_utilities_1.log.debug('Token validation successful', { module: 'auth-handler', user });
|
|
158
|
+
resolve(user);
|
|
159
|
+
})
|
|
117
160
|
.catch((error) => {
|
|
118
|
-
|
|
119
|
-
|
|
161
|
+
cli_utilities_1.log.debug('Token validation failed', { module: 'auth-handler', error: error.message });
|
|
162
|
+
const err = cli_utilities_1.cliErrorHandler.classifyError(error);
|
|
163
|
+
reject(err);
|
|
120
164
|
});
|
|
121
165
|
}
|
|
122
166
|
else {
|
|
167
|
+
cli_utilities_1.log.debug('Token validation failed - no auth token provided', { module: 'auth-handler' });
|
|
123
168
|
reject(new cli_utilities_1.CLIError({ message: 'No auth token found to validate' }));
|
|
124
169
|
}
|
|
125
170
|
});
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateAPIKey = exports.validateEnvironment = void 0;
|
|
4
4
|
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
-
const cli_utilities_2 = require("@contentstack/cli-utilities");
|
|
6
5
|
/**
|
|
7
6
|
* Validate environment
|
|
8
7
|
* @param contentStackClient
|
|
@@ -11,22 +10,27 @@ const cli_utilities_2 = require("@contentstack/cli-utilities");
|
|
|
11
10
|
* @returns
|
|
12
11
|
*/
|
|
13
12
|
const validateEnvironment = async (contentStackClient, apiKey, environment) => {
|
|
14
|
-
|
|
13
|
+
cli_utilities_1.log.debug('Starting environment validation', { module: 'tokens-validation', apiKeyStatus: apiKey ? 'provided' : 'not-provided', environment });
|
|
15
14
|
let result;
|
|
16
15
|
try {
|
|
16
|
+
cli_utilities_1.log.debug('Making environment validation API call', { module: 'tokens-validation', environment });
|
|
17
17
|
const validationResult = await contentStackClient.Stack({ api_key: apiKey }).environment(environment).fetch();
|
|
18
|
-
|
|
18
|
+
cli_utilities_1.log.debug('Environment validation API response received', { module: 'tokens-validation', validationResult });
|
|
19
19
|
if (validationResult.name === environment) {
|
|
20
|
+
cli_utilities_1.log.debug('Environment validation successful', { module: 'tokens-validation', environment, validationResult });
|
|
20
21
|
result = { valid: true, message: validationResult };
|
|
21
22
|
}
|
|
22
23
|
else {
|
|
24
|
+
cli_utilities_1.log.debug('Environment validation failed - name mismatch', { module: 'tokens-validation', expected: environment, actual: validationResult.name });
|
|
23
25
|
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME') };
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
catch (error) {
|
|
27
|
-
|
|
29
|
+
cli_utilities_1.log.debug('Environment validation API call failed', { module: 'tokens-validation', error: error.message, environment });
|
|
30
|
+
(0, cli_utilities_1.handleAndLogError)(error, { apiKey, environment });
|
|
28
31
|
result = { valid: false, message: 'CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME' };
|
|
29
32
|
}
|
|
33
|
+
cli_utilities_1.log.debug('Environment validation completed', { module: 'tokens-validation', result });
|
|
30
34
|
return result;
|
|
31
35
|
};
|
|
32
36
|
exports.validateEnvironment = validateEnvironment;
|
|
@@ -37,22 +41,27 @@ exports.validateEnvironment = validateEnvironment;
|
|
|
37
41
|
* @returns
|
|
38
42
|
*/
|
|
39
43
|
const validateAPIKey = async (contentStackClient, apiKey) => {
|
|
40
|
-
|
|
44
|
+
cli_utilities_1.log.debug('Starting API key validation', { module: 'tokens-validation', apiKeyStatus: apiKey ? 'provided' : 'not-provided' });
|
|
41
45
|
let result;
|
|
42
46
|
try {
|
|
47
|
+
cli_utilities_1.log.debug('Making API key validation API call', { module: 'tokens-validation' });
|
|
43
48
|
const validateAPIKeyResult = await contentStackClient.stack({ api_key: apiKey }).fetch();
|
|
44
|
-
|
|
49
|
+
cli_utilities_1.log.debug('API key validation API response received', { module: 'tokens-validation', validateAPIKeyResult });
|
|
45
50
|
if (validateAPIKeyResult.api_key === apiKey) {
|
|
51
|
+
cli_utilities_1.log.debug('API key validation successful', { module: 'tokens-validation', apiKey: validateAPIKeyResult.api_key });
|
|
46
52
|
result = { valid: true, message: validateAPIKeyResult };
|
|
47
53
|
}
|
|
48
54
|
else {
|
|
55
|
+
cli_utilities_1.log.debug('API key validation failed - key mismatch', { module: 'tokens-validation', expected: apiKey, actual: validateAPIKeyResult.api_key });
|
|
49
56
|
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
|
|
50
57
|
}
|
|
51
58
|
}
|
|
52
59
|
catch (error) {
|
|
53
|
-
|
|
60
|
+
cli_utilities_1.log.debug('API key validation API call failed', { module: 'tokens-validation', error: error.message });
|
|
61
|
+
(0, cli_utilities_1.handleAndLogError)(error, { apiKey });
|
|
54
62
|
result = { valid: false, message: cli_utilities_1.messageHandler.parse('CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY') };
|
|
55
63
|
}
|
|
64
|
+
cli_utilities_1.log.debug('API key validation completed', { module: 'tokens-validation', result });
|
|
56
65
|
return result;
|
|
57
66
|
};
|
|
58
67
|
exports.validateAPIKey = validateAPIKey;
|
package/messages/index.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"CLI_AUTH_LOGOUT_ALREADY": "You're already logged out",
|
|
21
21
|
"CLI_AUTH_LOGOUT_NO_AUTHORIZATIONS": "No authorizations found",
|
|
22
22
|
"CLI_AUTH_LOGOUT_NO_AUTHORIZATIONS_USER": "No authorizations found for current user",
|
|
23
|
-
"CLI_AUTH_WHOAMI_LOGGED_IN_AS": "You are currently logged in with email",
|
|
23
|
+
"CLI_AUTH_WHOAMI_LOGGED_IN_AS": "You are currently logged in with email:",
|
|
24
24
|
"CLI_AUTH_WHOAMI_FAILED": "Failed to get the current user details",
|
|
25
25
|
"CLI_AUTH_WHOAMI_DESCRIPTION": "Display current users email address",
|
|
26
26
|
"CLI_AUTH_TOKENS_ADD_ASK_TOKEN_ALIAS": "Provide alias to store token",
|
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.
|
|
4
|
+
"version": "1.5.1",
|
|
5
5
|
"author": "Contentstack",
|
|
6
6
|
"bugs": "https://github.com/contentstack/cli/issues",
|
|
7
7
|
"scripts": {
|
|
@@ -22,8 +22,10 @@
|
|
|
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.
|
|
26
|
-
"@contentstack/cli-utilities": "~1.
|
|
25
|
+
"@contentstack/cli-command": "~1.6.0",
|
|
26
|
+
"@contentstack/cli-utilities": "~1.13.1",
|
|
27
|
+
"@oclif/core": "^4.3.0",
|
|
28
|
+
"@oclif/plugin-help": "^6.2.28"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
31
|
"@fancy-test/nock": "^0.1.1",
|