@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Contentstack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @contentstack/cli-auth
|
|
2
2
|
|
|
3
|
-
It is Contentstack’s CLI plugin to perform authentication-related activities. To get started with
|
|
3
|
+
It is Contentstack’s CLI plugin to perform authentication-related activities. To get started with authentication, refer to the [CLI’s Authentication documentation](https://www.contentstack.com/docs/developers/cli/authentication)
|
|
4
4
|
|
|
5
5
|
[](https://github.com/contentstack/cli/blob/main/LICENSE)
|
|
6
6
|
|
|
@@ -18,7 +18,7 @@ $ npm install -g @contentstack/cli-auth
|
|
|
18
18
|
$ csdx COMMAND
|
|
19
19
|
running command...
|
|
20
20
|
$ csdx (-v|--version|version)
|
|
21
|
-
@contentstack/cli-auth/0.1
|
|
21
|
+
@contentstack/cli-auth/1.0.1 darwin-arm64 node-v16.17.0
|
|
22
22
|
$ csdx --help [COMMAND]
|
|
23
23
|
USAGE
|
|
24
24
|
$ csdx COMMAND
|
|
@@ -32,56 +32,62 @@ USAGE
|
|
|
32
32
|
* [`csdx auth:login`](#csdx-authlogin)
|
|
33
33
|
* [`csdx auth:logout`](#csdx-authlogout)
|
|
34
34
|
* [`csdx auth:tokens`](#csdx-authtokens)
|
|
35
|
-
* [`csdx auth:tokens:add`](#csdx-authtokensadd)
|
|
35
|
+
* [`csdx auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]`](#csdx-authtokensadd--a-value---delivery---management--e-value--k-value--y---token-value)
|
|
36
36
|
* [`csdx auth:tokens:remove`](#csdx-authtokensremove)
|
|
37
37
|
* [`csdx auth:whoami`](#csdx-authwhoami)
|
|
38
38
|
|
|
39
39
|
## `csdx auth:login`
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
User sessions login
|
|
42
42
|
|
|
43
43
|
```
|
|
44
|
-
Login to Contentstack and save the session for further use
|
|
45
|
-
|
|
46
44
|
USAGE
|
|
47
45
|
$ csdx auth:login
|
|
48
46
|
|
|
49
47
|
OPTIONS
|
|
50
|
-
-
|
|
48
|
+
-p, --password=password Password
|
|
49
|
+
-u, --username=username User name
|
|
51
50
|
|
|
52
51
|
ALIASES
|
|
53
52
|
$ csdx login
|
|
53
|
+
|
|
54
|
+
EXAMPLES
|
|
55
|
+
$ csdx auth:login
|
|
56
|
+
$ csdx auth:login -u <username>
|
|
57
|
+
$ csdx auth:login -u <username> -p <password>
|
|
58
|
+
$ csdx auth:login --username <username>
|
|
59
|
+
$ csdx auth:login --username <username> --password <password>
|
|
54
60
|
```
|
|
55
61
|
|
|
56
|
-
_See code: [src/commands/auth/login.
|
|
62
|
+
_See code: [src/commands/auth/login.ts](https://github.com/contentstack/cli/blob/v1.0.1/src/commands/auth/login.ts)_
|
|
57
63
|
|
|
58
64
|
## `csdx auth:logout`
|
|
59
65
|
|
|
60
|
-
|
|
66
|
+
User session logout
|
|
61
67
|
|
|
62
68
|
```
|
|
63
|
-
Log out from Contentstack and clear the session
|
|
64
|
-
|
|
65
69
|
USAGE
|
|
66
70
|
$ csdx auth:logout
|
|
67
71
|
|
|
68
72
|
OPTIONS
|
|
69
|
-
-
|
|
73
|
+
-y, --yes Force log out by skipping the confirmation
|
|
70
74
|
|
|
71
75
|
ALIASES
|
|
72
76
|
$ csdx logout
|
|
77
|
+
|
|
78
|
+
EXAMPLES
|
|
79
|
+
$ csdx auth:logout
|
|
80
|
+
$ csdx auth:logout -y
|
|
81
|
+
$ csdx auth:logout --yes
|
|
73
82
|
```
|
|
74
83
|
|
|
75
|
-
_See code: [src/commands/auth/logout.
|
|
84
|
+
_See code: [src/commands/auth/logout.ts](https://github.com/contentstack/cli/blob/v1.0.1/src/commands/auth/logout.ts)_
|
|
76
85
|
|
|
77
86
|
## `csdx auth:tokens`
|
|
78
87
|
|
|
79
88
|
Lists all existing tokens added to the session
|
|
80
89
|
|
|
81
90
|
```
|
|
82
|
-
Lists all existing tokens added to the session
|
|
83
|
-
|
|
84
|
-
|
|
85
91
|
USAGE
|
|
86
92
|
$ csdx auth:tokens
|
|
87
93
|
|
|
@@ -97,77 +103,80 @@ OPTIONS
|
|
|
97
103
|
|
|
98
104
|
ALIASES
|
|
99
105
|
$ csdx tokens
|
|
106
|
+
|
|
107
|
+
EXAMPLE
|
|
108
|
+
$ csdx auth:tokens
|
|
100
109
|
```
|
|
101
110
|
|
|
102
|
-
_See code: [src/commands/auth/tokens/index.
|
|
111
|
+
_See code: [src/commands/auth/tokens/index.ts](https://github.com/contentstack/cli/blob/v1.0.1/src/commands/auth/tokens/index.ts)_
|
|
103
112
|
|
|
104
|
-
## `csdx auth:tokens:add`
|
|
113
|
+
## `csdx auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]`
|
|
105
114
|
|
|
106
|
-
Adds management/delivery tokens to your session to use it with
|
|
115
|
+
Adds management/delivery tokens to your session to use it with other CLI commands
|
|
107
116
|
|
|
108
117
|
```
|
|
109
|
-
Adds management/delivery tokens to your session to use it with further CLI command
|
|
110
|
-
by default it adds management token if either of management or delivery flags are not set
|
|
111
|
-
|
|
112
118
|
USAGE
|
|
113
|
-
$ csdx auth:tokens:add
|
|
119
|
+
$ csdx auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]
|
|
114
120
|
|
|
115
121
|
OPTIONS
|
|
116
|
-
-a, --alias=alias
|
|
117
|
-
-d, --delivery
|
|
118
|
-
-e, --environment=environment
|
|
119
|
-
-
|
|
120
|
-
-
|
|
121
|
-
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
$ csdx tokens:add
|
|
122
|
+
-a, --alias=alias Name of the token alias
|
|
123
|
+
-d, --delivery Set this flag to save delivery token
|
|
124
|
+
-e, --environment=environment Environment name for delivery token
|
|
125
|
+
-k, --stack-api-key=stack-api-key Stack API Key
|
|
126
|
+
-m, --management Set this flag to save management token
|
|
127
|
+
-t, --token=token Add the token name
|
|
128
|
+
-y, --yes Use this flag to skip confirmation
|
|
129
|
+
|
|
130
|
+
EXAMPLES
|
|
131
|
+
$ csdx auth:tokens:add
|
|
132
|
+
$ csdx auth:tokens:add -a <alias>
|
|
133
|
+
$ csdx auth:tokens:add -k <stack api key>
|
|
134
|
+
$ csdx auth:tokens:add --delivery
|
|
135
|
+
$ csdx auth:tokens:add --management
|
|
136
|
+
$ csdx auth:tokens:add -e <environment>
|
|
137
|
+
$ csdx auth:tokens:add --token <token>
|
|
138
|
+
$ csdx auth:tokens:add -a <alias> -k <stack api key> --management --token <management token>
|
|
139
|
+
$ csdx auth:tokens:add -a <alias> -k <stack api key> --delivery -e <environment> --token <delivery token>
|
|
140
|
+
$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --management --token <management token>
|
|
141
|
+
$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --delivery -e <environment> --token <delivery
|
|
142
|
+
token>
|
|
131
143
|
```
|
|
132
144
|
|
|
133
|
-
_See code: [src/commands/auth/tokens/add.
|
|
145
|
+
_See code: [src/commands/auth/tokens/add.ts](https://github.com/contentstack/cli/blob/v1.0.1/src/commands/auth/tokens/add.ts)_
|
|
134
146
|
|
|
135
147
|
## `csdx auth:tokens:remove`
|
|
136
148
|
|
|
137
|
-
Removes
|
|
149
|
+
Removes selected tokens
|
|
138
150
|
|
|
139
151
|
```
|
|
140
|
-
Removes stored tokens
|
|
141
|
-
|
|
142
152
|
USAGE
|
|
143
153
|
$ csdx auth:tokens:remove
|
|
144
154
|
|
|
145
155
|
OPTIONS
|
|
146
|
-
-a, --alias=alias
|
|
147
|
-
|
|
148
|
-
-i, --ignore Ignores if token not present. Command shows show list of available aliases with multi select option
|
|
149
|
-
to delete tokens from that list.
|
|
156
|
+
-a, --alias=alias Token alias
|
|
157
|
+
-i, --ignore Ignore
|
|
150
158
|
|
|
151
|
-
|
|
152
|
-
$ csdx tokens:remove
|
|
159
|
+
EXAMPLES
|
|
160
|
+
$ csdx auth:tokens:remove
|
|
161
|
+
$ csdx auth:tokens:remove -a <alias>
|
|
153
162
|
```
|
|
154
163
|
|
|
155
|
-
_See code: [src/commands/auth/tokens/remove.
|
|
164
|
+
_See code: [src/commands/auth/tokens/remove.ts](https://github.com/contentstack/cli/blob/v1.0.1/src/commands/auth/tokens/remove.ts)_
|
|
156
165
|
|
|
157
166
|
## `csdx auth:whoami`
|
|
158
167
|
|
|
159
168
|
Display current users email address
|
|
160
169
|
|
|
161
170
|
```
|
|
162
|
-
Display current users email address
|
|
163
|
-
|
|
164
|
-
|
|
165
171
|
USAGE
|
|
166
172
|
$ csdx auth:whoami
|
|
167
173
|
|
|
168
174
|
ALIASES
|
|
169
175
|
$ csdx whoami
|
|
176
|
+
|
|
177
|
+
EXAMPLE
|
|
178
|
+
$ csdx auth:whoami
|
|
170
179
|
```
|
|
171
180
|
|
|
172
|
-
_See code: [src/commands/auth/whoami.
|
|
181
|
+
_See code: [src/commands/auth/whoami.ts](https://github.com/contentstack/cli/blob/v1.0.1/src/commands/auth/whoami.ts)_
|
|
173
182
|
<!-- commandsstop -->
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cli_command_1 = require("@contentstack/cli-command");
|
|
4
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
class LoginCommand extends cli_command_1.Command {
|
|
7
|
+
async run() {
|
|
8
|
+
const { flags: loginFlags } = this.parse(LoginCommand);
|
|
9
|
+
utils_1.authHandler.client = this.managementAPIClient;
|
|
10
|
+
try {
|
|
11
|
+
const username = (loginFlags === null || loginFlags === void 0 ? void 0 : loginFlags.username) || await utils_1.interactive.askUsername();
|
|
12
|
+
const password = (loginFlags === null || loginFlags === void 0 ? void 0 : loginFlags.password) || await utils_1.interactive.askPassword();
|
|
13
|
+
cli_utilities_1.logger.debug('username', username);
|
|
14
|
+
await this.login(username, password);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
cli_utilities_1.logger.error('login failed', error.message);
|
|
18
|
+
cli_utilities_1.cliux.print('CLI_AUTH_LOGIN_FAILED', { color: 'yellow' });
|
|
19
|
+
cli_utilities_1.cliux.print(error.message.message ? error.message.message : error.message, { color: 'red' });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async login(username, password) {
|
|
23
|
+
const user = await utils_1.authHandler.login(username, password);
|
|
24
|
+
if (typeof user !== 'object' || !user.authtoken || !user.email) {
|
|
25
|
+
throw new cli_utilities_1.CLIError('Failed to login - invalid response');
|
|
26
|
+
}
|
|
27
|
+
cli_utilities_1.configHandler.set('authtoken', user.authtoken);
|
|
28
|
+
cli_utilities_1.configHandler.set('email', user.email);
|
|
29
|
+
cli_utilities_1.logger.info('successfully logged in');
|
|
30
|
+
cli_utilities_1.cliux.success('CLI_AUTH_LOGIN_SUCCESS');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.default = LoginCommand;
|
|
34
|
+
LoginCommand.description = 'User sessions login';
|
|
35
|
+
LoginCommand.examples = [
|
|
36
|
+
'$ csdx auth:login',
|
|
37
|
+
'$ csdx auth:login -u <username>',
|
|
38
|
+
'$ csdx auth:login -u <username> -p <password>',
|
|
39
|
+
'$ csdx auth:login --username <username>',
|
|
40
|
+
'$ csdx auth:login --username <username> --password <password>',
|
|
41
|
+
];
|
|
42
|
+
LoginCommand.flags = {
|
|
43
|
+
username: cli_command_1.flags.string({
|
|
44
|
+
char: 'u',
|
|
45
|
+
description: 'User name',
|
|
46
|
+
multiple: false,
|
|
47
|
+
required: false,
|
|
48
|
+
}),
|
|
49
|
+
password: cli_command_1.flags.string({
|
|
50
|
+
char: 'p',
|
|
51
|
+
description: 'Password',
|
|
52
|
+
multiple: false,
|
|
53
|
+
required: false,
|
|
54
|
+
}),
|
|
55
|
+
};
|
|
56
|
+
LoginCommand.aliases = ['login'];
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cli_command_1 = require("@contentstack/cli-command");
|
|
4
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
class LogoutCommand extends cli_command_1.Command {
|
|
7
|
+
async run() {
|
|
8
|
+
const { flags: logoutFlags } = this.parse(LogoutCommand);
|
|
9
|
+
utils_1.authHandler.client = this.managementAPIClient;
|
|
10
|
+
let confirm = false;
|
|
11
|
+
confirm =
|
|
12
|
+
logoutFlags.force || logoutFlags.yes
|
|
13
|
+
? true
|
|
14
|
+
: await cli_utilities_1.cliux.inquire({
|
|
15
|
+
type: 'confirm',
|
|
16
|
+
message: 'CLI_AUTH_LOGOUT_CONFIRM',
|
|
17
|
+
name: 'confirmation',
|
|
18
|
+
});
|
|
19
|
+
try {
|
|
20
|
+
if (this.authToken) {
|
|
21
|
+
if (confirm) {
|
|
22
|
+
cli_utilities_1.cliux.loader('CLI_AUTH_LOGOUT_LOADER_START');
|
|
23
|
+
const authtoken = this.authToken;
|
|
24
|
+
await utils_1.authHandler.logout(authtoken);
|
|
25
|
+
cli_utilities_1.cliux.loader(''); //stops loading
|
|
26
|
+
cli_utilities_1.logger.info('successfully logged out');
|
|
27
|
+
cli_utilities_1.cliux.success('CLI_AUTH_LOGOUT_SUCCESS');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
cli_utilities_1.logger.error('Logout failed', error.message);
|
|
33
|
+
cli_utilities_1.cliux.print('CLI_AUTH_LOGOUT_FAILED', { color: 'yellow' });
|
|
34
|
+
cli_utilities_1.cliux.print(error.message, { color: 'red' });
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
cli_utilities_1.configHandler.delete('authtoken');
|
|
38
|
+
cli_utilities_1.configHandler.delete('email');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.default = LogoutCommand;
|
|
43
|
+
LogoutCommand.description = 'User session logout';
|
|
44
|
+
LogoutCommand.examples = ['$ csdx auth:logout', '$ csdx auth:logout -y', '$ csdx auth:logout --yes'];
|
|
45
|
+
LogoutCommand.flags = {
|
|
46
|
+
yes: cli_command_1.flags.boolean({
|
|
47
|
+
char: 'y',
|
|
48
|
+
description: 'Force log out by skipping the confirmation',
|
|
49
|
+
// multiple: false,
|
|
50
|
+
required: false,
|
|
51
|
+
}),
|
|
52
|
+
force: cli_command_1.flags.boolean({
|
|
53
|
+
char: 'f',
|
|
54
|
+
description: 'Force log out by skipping the confirmation',
|
|
55
|
+
// multiple: false,
|
|
56
|
+
required: false,
|
|
57
|
+
hidden: true,
|
|
58
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-f', '--force'], ['-y', '--yes']),
|
|
59
|
+
}),
|
|
60
|
+
};
|
|
61
|
+
LogoutCommand.aliases = ['logout'];
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cli_command_1 = require("@contentstack/cli-command");
|
|
4
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
+
const interactive_1 = require("../../../utils/interactive");
|
|
6
|
+
const utils_1 = require("../../../utils");
|
|
7
|
+
class TokensAddCommand extends cli_command_1.Command {
|
|
8
|
+
async run() {
|
|
9
|
+
const { flags: addTokenFlags } = this.parse(TokensAddCommand);
|
|
10
|
+
let isAliasExist = false;
|
|
11
|
+
const skipAliasReplaceConfirmation = addTokenFlags.force || addTokenFlags.yes;
|
|
12
|
+
let alias = addTokenFlags.alias;
|
|
13
|
+
let apiKey = addTokenFlags['api-key'] || addTokenFlags['stack-api-key'];
|
|
14
|
+
let token = addTokenFlags.token;
|
|
15
|
+
let isDelivery = addTokenFlags.delivery;
|
|
16
|
+
let isManagement = addTokenFlags.management;
|
|
17
|
+
let environment = addTokenFlags.environment;
|
|
18
|
+
const configKeyTokens = 'tokens';
|
|
19
|
+
if (!isDelivery && !isManagement && !Boolean(environment)) {
|
|
20
|
+
let tokenType = await (0, interactive_1.askTokenType)();
|
|
21
|
+
isDelivery = tokenType === 'delivery';
|
|
22
|
+
isManagement = tokenType === 'management';
|
|
23
|
+
}
|
|
24
|
+
const type = isDelivery || Boolean(environment) ? 'delivery' : 'management';
|
|
25
|
+
cli_utilities_1.logger.info(`adding ${type} token`);
|
|
26
|
+
try {
|
|
27
|
+
if (!alias) {
|
|
28
|
+
alias = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ASK_TOKEN_ALIAS', name: 'alias' });
|
|
29
|
+
}
|
|
30
|
+
isAliasExist = Boolean(cli_utilities_1.configHandler.get(`${configKeyTokens}.${alias}`)); // get to Check if alias already present
|
|
31
|
+
if (isAliasExist && !skipAliasReplaceConfirmation) {
|
|
32
|
+
const shouldAliasReplace = await cli_utilities_1.cliux.inquire({
|
|
33
|
+
type: 'confirm',
|
|
34
|
+
message: `CLI_AUTH_TOKENS_ADD_CONFIRM_ALIAS_REPLACE`,
|
|
35
|
+
name: 'confirm',
|
|
36
|
+
});
|
|
37
|
+
if (!shouldAliasReplace) {
|
|
38
|
+
cli_utilities_1.logger.info('Exiting from the process of replacing the token');
|
|
39
|
+
cli_utilities_1.cliux.print('CLI_AUTH_EXIT_PROCESS');
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (!apiKey) {
|
|
44
|
+
apiKey = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_API_KEY', name: 'apiKey' });
|
|
45
|
+
}
|
|
46
|
+
if (!token) {
|
|
47
|
+
token = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_TOKEN', name: 'token' });
|
|
48
|
+
}
|
|
49
|
+
if (isDelivery && !environment) {
|
|
50
|
+
environment = await cli_utilities_1.cliux.inquire({
|
|
51
|
+
type: 'input',
|
|
52
|
+
message: 'CLI_AUTH_TOKENS_ADD_ENTER_ENVIRONMENT',
|
|
53
|
+
name: 'env',
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
let tokenValidationResult;
|
|
57
|
+
if (type === 'delivery') {
|
|
58
|
+
tokenValidationResult = await utils_1.tokenValidation.validateDeliveryToken(this.deliveryAPIClient, apiKey, token, environment, this.region.name, this.cdaHost);
|
|
59
|
+
}
|
|
60
|
+
else if (type === 'management') {
|
|
61
|
+
this.managementAPIClient = { host: this.cmaHost, authorization: token, api_key: apiKey };
|
|
62
|
+
tokenValidationResult = await utils_1.tokenValidation.validateManagementToken(this.managementAPIClient, apiKey, token);
|
|
63
|
+
}
|
|
64
|
+
if (!tokenValidationResult.valid) {
|
|
65
|
+
throw new cli_utilities_1.CLIError(tokenValidationResult.message);
|
|
66
|
+
}
|
|
67
|
+
if (isManagement) {
|
|
68
|
+
cli_utilities_1.configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, type });
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
cli_utilities_1.configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, environment, type });
|
|
72
|
+
}
|
|
73
|
+
if (isAliasExist) {
|
|
74
|
+
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_ADD_REPLACE_SUCCESS');
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_ADD_SUCCESS');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
cli_utilities_1.logger.error('token add error', error.message);
|
|
82
|
+
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_ADD_FAILED', { color: 'yellow' });
|
|
83
|
+
cli_utilities_1.cliux.print(error.message.message ? error.message.message : error.message, { color: 'red' });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.default = TokensAddCommand;
|
|
88
|
+
TokensAddCommand.description = 'Adds management/delivery tokens to your session to use it with other CLI commands';
|
|
89
|
+
TokensAddCommand.examples = [
|
|
90
|
+
'$ csdx auth:tokens:add',
|
|
91
|
+
'$ csdx auth:tokens:add -a <alias>',
|
|
92
|
+
'$ csdx auth:tokens:add -k <stack api key>',
|
|
93
|
+
'$ csdx auth:tokens:add --delivery',
|
|
94
|
+
'$ csdx auth:tokens:add --management',
|
|
95
|
+
'$ csdx auth:tokens:add -e <environment>',
|
|
96
|
+
'$ csdx auth:tokens:add --token <token>',
|
|
97
|
+
'$ csdx auth:tokens:add -a <alias> -k <stack api key> --management --token <management token>',
|
|
98
|
+
'$ csdx auth:tokens:add -a <alias> -k <stack api key> --delivery -e <environment> --token <delivery token>',
|
|
99
|
+
'$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --management --token <management token>',
|
|
100
|
+
'$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --delivery -e <environment> --token <delivery token>',
|
|
101
|
+
];
|
|
102
|
+
TokensAddCommand.flags = {
|
|
103
|
+
alias: cli_command_1.flags.string({ char: 'a', description: 'Name of the token alias' }),
|
|
104
|
+
delivery: cli_command_1.flags.boolean({
|
|
105
|
+
char: 'd',
|
|
106
|
+
description: 'Set this flag to save delivery token',
|
|
107
|
+
exclusive: ['management'],
|
|
108
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-d'], ['--delivery']),
|
|
109
|
+
}),
|
|
110
|
+
management: cli_command_1.flags.boolean({
|
|
111
|
+
char: 'm',
|
|
112
|
+
description: 'Set this flag to save management token',
|
|
113
|
+
exclusive: ['delivery', 'environment'],
|
|
114
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-m'], ['--management']),
|
|
115
|
+
}),
|
|
116
|
+
environment: cli_command_1.flags.string({
|
|
117
|
+
char: 'e',
|
|
118
|
+
description: 'Environment name for delivery token',
|
|
119
|
+
exclusive: ['management'],
|
|
120
|
+
}),
|
|
121
|
+
'stack-api-key': cli_command_1.flags.string({ char: 'k', description: 'Stack API Key' }),
|
|
122
|
+
yes: cli_command_1.flags.boolean({ char: 'y', description: 'Use this flag to skip confirmation' }),
|
|
123
|
+
token: cli_command_1.flags.string({
|
|
124
|
+
char: 't',
|
|
125
|
+
description: 'Add the token name',
|
|
126
|
+
env: 'TOKEN',
|
|
127
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-t'], ['--token']),
|
|
128
|
+
}),
|
|
129
|
+
//To be deprecated
|
|
130
|
+
'api-key': cli_command_1.flags.string({
|
|
131
|
+
description: 'API Key',
|
|
132
|
+
hidden: true,
|
|
133
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['api-key'], ['-k', 'stack-api-key']),
|
|
134
|
+
}),
|
|
135
|
+
force: cli_command_1.flags.boolean({
|
|
136
|
+
char: 'f',
|
|
137
|
+
hidden: true,
|
|
138
|
+
description: 'Force adding',
|
|
139
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-f', '--force'], ['-y', '--yes']),
|
|
140
|
+
}),
|
|
141
|
+
};
|
|
142
|
+
TokensAddCommand.usage = 'auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cli_command_1 = require("@contentstack/cli-command");
|
|
4
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
+
class TokensListCommand extends cli_command_1.Command {
|
|
6
|
+
async run() {
|
|
7
|
+
try {
|
|
8
|
+
const managementTokens = cli_utilities_1.configHandler.get('tokens');
|
|
9
|
+
const tokenOptions = [];
|
|
10
|
+
if (managementTokens && Object.keys(managementTokens).length > 0) {
|
|
11
|
+
Object.keys(managementTokens).forEach(function (item) {
|
|
12
|
+
tokenOptions.push({
|
|
13
|
+
alias: item,
|
|
14
|
+
token: managementTokens[item].token,
|
|
15
|
+
apiKey: managementTokens[item].apiKey,
|
|
16
|
+
environment: managementTokens[item].environment ? managementTokens[item].environment : '-',
|
|
17
|
+
type: managementTokens[item].type,
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
const { flags } = this.parse(TokensListCommand);
|
|
21
|
+
cli_utilities_1.cliux.table(tokenOptions, {
|
|
22
|
+
alias: {
|
|
23
|
+
minWidth: 7,
|
|
24
|
+
},
|
|
25
|
+
token: {
|
|
26
|
+
minWidth: 7,
|
|
27
|
+
},
|
|
28
|
+
apiKey: {
|
|
29
|
+
minWidth: 7,
|
|
30
|
+
},
|
|
31
|
+
environment: {
|
|
32
|
+
minWidth: 7,
|
|
33
|
+
},
|
|
34
|
+
type: {
|
|
35
|
+
minWidth: 7,
|
|
36
|
+
},
|
|
37
|
+
}, Object.assign({ printLine: cli_utilities_1.cliux.print }, flags));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
cli_utilities_1.logger.error('Token list error', error.message);
|
|
45
|
+
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' });
|
|
46
|
+
cli_utilities_1.cliux.print(error.message, { color: 'red' });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.default = TokensListCommand;
|
|
51
|
+
TokensListCommand.aliases = ['tokens'];
|
|
52
|
+
TokensListCommand.examples = ['$ csdx auth:tokens'];
|
|
53
|
+
TokensListCommand.description = 'Lists all existing tokens added to the session';
|
|
54
|
+
TokensListCommand.flags = cli_utilities_1.cliux.uxTable.flags(); // use the cli table flags as it displays tokens in table
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cli_command_1 = require("@contentstack/cli-command");
|
|
4
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
+
class TokensRemoveCommand extends cli_command_1.Command {
|
|
6
|
+
async run() {
|
|
7
|
+
const { flags: removeTokenFlags } = this.parse(TokensRemoveCommand);
|
|
8
|
+
const alias = removeTokenFlags.alias;
|
|
9
|
+
const ignore = removeTokenFlags.ignore;
|
|
10
|
+
try {
|
|
11
|
+
const token = cli_utilities_1.configHandler.get(`tokens.${alias}`);
|
|
12
|
+
const tokens = cli_utilities_1.configHandler.get('tokens');
|
|
13
|
+
const tokenOptions = [];
|
|
14
|
+
if (token || ignore) {
|
|
15
|
+
cli_utilities_1.configHandler.delete(`tokens.${alias}`);
|
|
16
|
+
return cli_utilities_1.cliux.success(`CLI_AUTH_TOKENS_REMOVE_SUCCESS`);
|
|
17
|
+
}
|
|
18
|
+
if (tokens && Object.keys(tokens).length > 0) {
|
|
19
|
+
Object.keys(tokens).forEach(function (item) {
|
|
20
|
+
tokenOptions.push(`${item}: ${tokens[item].token} : ${tokens[item].apiKey}${tokens[item].environment ? ' : ' + tokens[item].environment + ' ' : ''}: ${tokens[item].type}`);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_NOT_FOUND');
|
|
25
|
+
}
|
|
26
|
+
const selectedTokens = await cli_utilities_1.cliux.inquire({
|
|
27
|
+
name: 'selectedTokens',
|
|
28
|
+
message: 'CLI_AUTH_TOKENS_REMOVE_SELECT_TOKEN',
|
|
29
|
+
type: 'checkbox',
|
|
30
|
+
choices: tokenOptions,
|
|
31
|
+
});
|
|
32
|
+
cli_utilities_1.logger.debug('selected tokens', selectedTokens);
|
|
33
|
+
if (selectedTokens.length === 0) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
selectedTokens.forEach((element) => {
|
|
37
|
+
const selectedToken = element.split(':')[0];
|
|
38
|
+
cli_utilities_1.configHandler.delete(`tokens.${selectedToken}`);
|
|
39
|
+
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_REMOVE_SUCCESS');
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
cli_utilities_1.logger.error('Token remove error', error.message);
|
|
44
|
+
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_REMOVE_FAILED', { color: 'yellow' });
|
|
45
|
+
cli_utilities_1.cliux.print(error.message, { color: 'red' });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.default = TokensRemoveCommand;
|
|
50
|
+
TokensRemoveCommand.description = 'Removes selected tokens';
|
|
51
|
+
TokensRemoveCommand.examples = ['$ csdx auth:tokens:remove', '$ csdx auth:tokens:remove -a <alias>'];
|
|
52
|
+
TokensRemoveCommand.flags = {
|
|
53
|
+
alias: cli_command_1.flags.string({ char: 'a', description: 'Token alias' }),
|
|
54
|
+
ignore: cli_command_1.flags.boolean({ char: 'i', description: 'Ignore' }),
|
|
55
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cli_command_1 = require("@contentstack/cli-command");
|
|
4
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
5
|
+
class WhoamiCommand extends cli_command_1.Command {
|
|
6
|
+
async run() {
|
|
7
|
+
try {
|
|
8
|
+
if (this.email) {
|
|
9
|
+
cli_utilities_1.cliux.print('CLI_AUTH_WHOAMI_LOGGED_IN_AS', { color: 'white' });
|
|
10
|
+
cli_utilities_1.cliux.print(this.email, { color: 'green' });
|
|
11
|
+
cli_utilities_1.logger.info('Currently logged in user', this.email);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
cli_utilities_1.cliux.error('CLI_AUTH_WHOAMI_FAILED');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
cli_utilities_1.logger.error('whoami error', error.message);
|
|
19
|
+
cli_utilities_1.cliux.print('CLI_AUTH_WHOAMI_FAILED', { color: 'yellow' });
|
|
20
|
+
cli_utilities_1.cliux.print(error.message, { color: 'red' });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.default = WhoamiCommand;
|
|
25
|
+
WhoamiCommand.description = 'Display current users email address';
|
|
26
|
+
WhoamiCommand.examples = ['$ csdx auth:whoami'];
|
|
27
|
+
WhoamiCommand.aliases = ['whoami'];
|
|
File without changes
|