@contentstack/cli-auth 0.1.1-beta.1 → 1.0.0
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 +68 -40
- package/lib/commands/auth/login.js +56 -0
- package/lib/commands/auth/logout.js +61 -0
- package/lib/commands/auth/tokens/add.js +152 -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 +114 -0
- package/oclif.manifest.json +1 -1
- package/package.json +55 -32
- package/messages/index.json +0 -43
- 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 -63
- 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,70 +1,87 @@
|
|
|
1
|
-
@contentstack/cli-auth
|
|
2
|
-
===
|
|
1
|
+
# @contentstack/cli-auth
|
|
3
2
|
|
|
4
|
-
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)
|
|
5
4
|
|
|
6
5
|
[](https://github.com/contentstack/cli/blob/main/LICENSE)
|
|
7
6
|
|
|
8
7
|
<!-- toc -->
|
|
8
|
+
* [@contentstack/cli-auth](#contentstackcli-auth)
|
|
9
9
|
* [Usage](#usage)
|
|
10
10
|
* [Commands](#commands)
|
|
11
11
|
<!-- tocstop -->
|
|
12
|
+
|
|
12
13
|
# Usage
|
|
14
|
+
|
|
13
15
|
<!-- usage -->
|
|
14
16
|
```sh-session
|
|
15
17
|
$ npm install -g @contentstack/cli-auth
|
|
16
18
|
$ csdx COMMAND
|
|
17
19
|
running command...
|
|
18
20
|
$ csdx (-v|--version|version)
|
|
19
|
-
@contentstack/cli-auth/
|
|
21
|
+
@contentstack/cli-auth/1.0.0 linux-x64 node-v16.14.2
|
|
20
22
|
$ csdx --help [COMMAND]
|
|
21
23
|
USAGE
|
|
22
24
|
$ csdx COMMAND
|
|
23
25
|
...
|
|
24
26
|
```
|
|
25
27
|
<!-- usagestop -->
|
|
28
|
+
|
|
26
29
|
# Commands
|
|
30
|
+
|
|
27
31
|
<!-- commands -->
|
|
28
32
|
* [`csdx auth:login`](#csdx-authlogin)
|
|
29
33
|
* [`csdx auth:logout`](#csdx-authlogout)
|
|
30
34
|
* [`csdx auth:tokens`](#csdx-authtokens)
|
|
31
|
-
* [`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)
|
|
32
36
|
* [`csdx auth:tokens:remove`](#csdx-authtokensremove)
|
|
33
37
|
* [`csdx auth:whoami`](#csdx-authwhoami)
|
|
34
38
|
|
|
35
39
|
## `csdx auth:login`
|
|
36
40
|
|
|
37
|
-
|
|
41
|
+
User sessions login
|
|
38
42
|
|
|
39
43
|
```
|
|
40
44
|
USAGE
|
|
41
45
|
$ csdx auth:login
|
|
42
46
|
|
|
43
47
|
OPTIONS
|
|
44
|
-
-
|
|
48
|
+
-p, --password=password Password
|
|
49
|
+
-u, --username=username User name
|
|
45
50
|
|
|
46
51
|
ALIASES
|
|
47
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>
|
|
48
60
|
```
|
|
49
61
|
|
|
50
|
-
_See code: [src/commands/auth/login.
|
|
62
|
+
_See code: [src/commands/auth/login.ts](https://github.com/contentstack/cli/blob/v1.0.0/src/commands/auth/login.ts)_
|
|
51
63
|
|
|
52
64
|
## `csdx auth:logout`
|
|
53
65
|
|
|
54
|
-
|
|
66
|
+
User session logout
|
|
55
67
|
|
|
56
68
|
```
|
|
57
69
|
USAGE
|
|
58
70
|
$ csdx auth:logout
|
|
59
71
|
|
|
60
72
|
OPTIONS
|
|
61
|
-
-
|
|
73
|
+
-y, --yes Force log out by skipping the confirmation
|
|
62
74
|
|
|
63
75
|
ALIASES
|
|
64
76
|
$ csdx logout
|
|
77
|
+
|
|
78
|
+
EXAMPLES
|
|
79
|
+
$ csdx auth:logout
|
|
80
|
+
$ csdx auth:logout -y
|
|
81
|
+
$ csdx auth:logout --yes
|
|
65
82
|
```
|
|
66
83
|
|
|
67
|
-
_See code: [src/commands/auth/logout.
|
|
84
|
+
_See code: [src/commands/auth/logout.ts](https://github.com/contentstack/cli/blob/v1.0.0/src/commands/auth/logout.ts)_
|
|
68
85
|
|
|
69
86
|
## `csdx auth:tokens`
|
|
70
87
|
|
|
@@ -86,57 +103,65 @@ OPTIONS
|
|
|
86
103
|
|
|
87
104
|
ALIASES
|
|
88
105
|
$ csdx tokens
|
|
106
|
+
|
|
107
|
+
EXAMPLE
|
|
108
|
+
$ csdx auth:tokens
|
|
89
109
|
```
|
|
90
110
|
|
|
91
|
-
_See code: [src/commands/auth/tokens/index.
|
|
111
|
+
_See code: [src/commands/auth/tokens/index.ts](https://github.com/contentstack/cli/blob/v1.0.0/src/commands/auth/tokens/index.ts)_
|
|
92
112
|
|
|
93
|
-
## `csdx auth:tokens:add`
|
|
113
|
+
## `csdx auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]`
|
|
94
114
|
|
|
95
|
-
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
|
|
96
116
|
|
|
97
117
|
```
|
|
98
118
|
USAGE
|
|
99
|
-
$ csdx auth:tokens:add
|
|
119
|
+
$ csdx auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]
|
|
100
120
|
|
|
101
121
|
OPTIONS
|
|
102
|
-
-a, --alias=alias
|
|
103
|
-
-d, --delivery
|
|
104
|
-
-e, --environment=environment
|
|
105
|
-
-
|
|
106
|
-
-
|
|
107
|
-
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
$ 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>
|
|
117
143
|
```
|
|
118
144
|
|
|
119
|
-
_See code: [src/commands/auth/tokens/add.
|
|
145
|
+
_See code: [src/commands/auth/tokens/add.ts](https://github.com/contentstack/cli/blob/v1.0.0/src/commands/auth/tokens/add.ts)_
|
|
120
146
|
|
|
121
147
|
## `csdx auth:tokens:remove`
|
|
122
148
|
|
|
123
|
-
Removes
|
|
149
|
+
Removes selected tokens
|
|
124
150
|
|
|
125
151
|
```
|
|
126
152
|
USAGE
|
|
127
153
|
$ csdx auth:tokens:remove
|
|
128
154
|
|
|
129
155
|
OPTIONS
|
|
130
|
-
-a, --alias=alias
|
|
156
|
+
-a, --alias=alias Token alias
|
|
157
|
+
-i, --ignore Ignore
|
|
131
158
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
ALIASES
|
|
136
|
-
$ csdx tokens:remove
|
|
159
|
+
EXAMPLES
|
|
160
|
+
$ csdx auth:tokens:remove
|
|
161
|
+
$ csdx auth:tokens:remove -a <alias>
|
|
137
162
|
```
|
|
138
163
|
|
|
139
|
-
_See code: [src/commands/auth/tokens/remove.
|
|
164
|
+
_See code: [src/commands/auth/tokens/remove.ts](https://github.com/contentstack/cli/blob/v1.0.0/src/commands/auth/tokens/remove.ts)_
|
|
140
165
|
|
|
141
166
|
## `csdx auth:whoami`
|
|
142
167
|
|
|
@@ -148,7 +173,10 @@ USAGE
|
|
|
148
173
|
|
|
149
174
|
ALIASES
|
|
150
175
|
$ csdx whoami
|
|
176
|
+
|
|
177
|
+
EXAMPLE
|
|
178
|
+
$ csdx auth:whoami
|
|
151
179
|
```
|
|
152
180
|
|
|
153
|
-
_See code: [src/commands/auth/whoami.
|
|
181
|
+
_See code: [src/commands/auth/whoami.ts](https://github.com/contentstack/cli/blob/v1.0.0/src/commands/auth/whoami.ts)_
|
|
154
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,152 @@
|
|
|
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
|
+
this.managementAPIClient = { host: this.cmaHost, authtoken: this.authToken };
|
|
10
|
+
const { flags: addTokenFlags } = this.parse(TokensAddCommand);
|
|
11
|
+
let isAliasExist = false;
|
|
12
|
+
const skipAliasReplaceConfirmation = addTokenFlags.force || addTokenFlags.yes;
|
|
13
|
+
let alias = addTokenFlags.alias;
|
|
14
|
+
let apiKey = addTokenFlags['api-key'] || addTokenFlags['stack-api-key'];
|
|
15
|
+
let token = addTokenFlags.token;
|
|
16
|
+
let isDelivery = addTokenFlags.delivery;
|
|
17
|
+
let isManagement = addTokenFlags.management;
|
|
18
|
+
let environment = addTokenFlags.environment;
|
|
19
|
+
const configKeyTokens = 'tokens';
|
|
20
|
+
if (!isDelivery && !isManagement && !Boolean(environment)) {
|
|
21
|
+
let tokenType = await (0, interactive_1.askTokenType)();
|
|
22
|
+
isDelivery = tokenType === 'delivery';
|
|
23
|
+
isManagement = tokenType === 'management';
|
|
24
|
+
}
|
|
25
|
+
const type = isDelivery || Boolean(environment) ? 'delivery' : 'management';
|
|
26
|
+
cli_utilities_1.logger.info(`adding ${type} token`);
|
|
27
|
+
try {
|
|
28
|
+
if (!alias) {
|
|
29
|
+
alias = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ASK_TOKEN_ALIAS', name: 'alias' });
|
|
30
|
+
}
|
|
31
|
+
isAliasExist = Boolean(cli_utilities_1.configHandler.get(`${configKeyTokens}.${alias}`)); // get to Check if alias already present
|
|
32
|
+
if (isAliasExist && !skipAliasReplaceConfirmation) {
|
|
33
|
+
const shouldAliasReplace = await cli_utilities_1.cliux.inquire({
|
|
34
|
+
type: 'confirm',
|
|
35
|
+
message: `CLI_AUTH_TOKENS_ADD_CONFIRM_ALIAS_REPLACE`,
|
|
36
|
+
name: 'confirm',
|
|
37
|
+
});
|
|
38
|
+
if (!shouldAliasReplace) {
|
|
39
|
+
cli_utilities_1.logger.info('Exiting from the process of replacing the token');
|
|
40
|
+
cli_utilities_1.cliux.print('CLI_AUTH_EXIT_PROCESS');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!apiKey) {
|
|
45
|
+
apiKey = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_API_KEY', name: 'apiKey' });
|
|
46
|
+
}
|
|
47
|
+
const apiKeyValidationResult = await utils_1.tokenValidation.validateAPIKey(this.managementAPIClient, apiKey);
|
|
48
|
+
if (!apiKeyValidationResult.valid) {
|
|
49
|
+
throw new cli_utilities_1.CLIError({ message: apiKeyValidationResult.message });
|
|
50
|
+
}
|
|
51
|
+
if (!token) {
|
|
52
|
+
token = await cli_utilities_1.cliux.inquire({ type: 'input', message: 'CLI_AUTH_TOKENS_ADD_ENTER_TOKEN', name: 'token' });
|
|
53
|
+
}
|
|
54
|
+
let tokenValidationResult;
|
|
55
|
+
if (type === 'delivery') {
|
|
56
|
+
tokenValidationResult = await utils_1.tokenValidation.validateDeliveryToken(this.managementAPIClient, apiKey, token);
|
|
57
|
+
}
|
|
58
|
+
else if (type === 'management') {
|
|
59
|
+
tokenValidationResult = await utils_1.tokenValidation.validateManagementToken(this.managementAPIClient, apiKey, token);
|
|
60
|
+
}
|
|
61
|
+
if (!tokenValidationResult.valid) {
|
|
62
|
+
throw new cli_utilities_1.CLIError(tokenValidationResult.message);
|
|
63
|
+
}
|
|
64
|
+
if (isDelivery && !environment) {
|
|
65
|
+
environment = await cli_utilities_1.cliux.inquire({
|
|
66
|
+
type: 'input',
|
|
67
|
+
message: 'CLI_AUTH_TOKENS_ADD_ENTER_ENVIRONMENT',
|
|
68
|
+
name: 'env',
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (environment) {
|
|
72
|
+
const envValidationResult = await utils_1.tokenValidation.validateEnvironment(this.managementAPIClient, apiKey, environment);
|
|
73
|
+
if (!envValidationResult.valid) {
|
|
74
|
+
throw new cli_utilities_1.CLIError(envValidationResult.message);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (isManagement) {
|
|
78
|
+
cli_utilities_1.configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, type });
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
cli_utilities_1.configHandler.set(`${configKeyTokens}.${alias}`, { token, apiKey, environment, type });
|
|
82
|
+
}
|
|
83
|
+
if (isAliasExist) {
|
|
84
|
+
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_ADD_REPLACE_SUCCESS');
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
cli_utilities_1.cliux.success('CLI_AUTH_TOKENS_ADD_SUCCESS');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
cli_utilities_1.logger.error('token add error', error.message);
|
|
92
|
+
cli_utilities_1.cliux.print('CLI_AUTH_TOKENS_ADD_FAILED', { color: 'yellow' });
|
|
93
|
+
cli_utilities_1.cliux.print(error.message.message ? error.message.message : error.message, { color: 'red' });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.default = TokensAddCommand;
|
|
98
|
+
TokensAddCommand.description = 'Adds management/delivery tokens to your session to use it with other CLI commands';
|
|
99
|
+
TokensAddCommand.examples = [
|
|
100
|
+
'$ csdx auth:tokens:add',
|
|
101
|
+
'$ csdx auth:tokens:add -a <alias>',
|
|
102
|
+
'$ csdx auth:tokens:add -k <stack api key>',
|
|
103
|
+
'$ csdx auth:tokens:add --delivery',
|
|
104
|
+
'$ csdx auth:tokens:add --management',
|
|
105
|
+
'$ csdx auth:tokens:add -e <environment>',
|
|
106
|
+
'$ csdx auth:tokens:add --token <token>',
|
|
107
|
+
'$ csdx auth:tokens:add -a <alias> -k <stack api key> --management --token <management token>',
|
|
108
|
+
'$ csdx auth:tokens:add -a <alias> -k <stack api key> --delivery -e <environment> --token <delivery token>',
|
|
109
|
+
'$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --management --token <management token>',
|
|
110
|
+
'$ csdx auth:tokens:add --alias <alias> --stack-api-key <stack api key> --delivery -e <environment> --token <delivery token>',
|
|
111
|
+
];
|
|
112
|
+
TokensAddCommand.flags = {
|
|
113
|
+
alias: cli_command_1.flags.string({ char: 'a', description: 'Name of the token alias' }),
|
|
114
|
+
delivery: cli_command_1.flags.boolean({
|
|
115
|
+
char: 'd',
|
|
116
|
+
description: 'Set this flag to save delivery token',
|
|
117
|
+
exclusive: ['management'],
|
|
118
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-d'], ['--delivery']),
|
|
119
|
+
}),
|
|
120
|
+
management: cli_command_1.flags.boolean({
|
|
121
|
+
char: 'm',
|
|
122
|
+
description: 'Set this flag to save management token',
|
|
123
|
+
exclusive: ['delivery', 'environment'],
|
|
124
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-m'], ['--management']),
|
|
125
|
+
}),
|
|
126
|
+
environment: cli_command_1.flags.string({
|
|
127
|
+
char: 'e',
|
|
128
|
+
description: 'Environment name for delivery token',
|
|
129
|
+
exclusive: ['management'],
|
|
130
|
+
}),
|
|
131
|
+
'stack-api-key': cli_command_1.flags.string({ char: 'k', description: 'Stack API Key' }),
|
|
132
|
+
yes: cli_command_1.flags.boolean({ char: 'y', description: 'Use this flag to skip confirmation' }),
|
|
133
|
+
token: cli_command_1.flags.string({
|
|
134
|
+
char: 't',
|
|
135
|
+
description: 'Add the token name',
|
|
136
|
+
env: 'TOKEN',
|
|
137
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-t'], ['--token']),
|
|
138
|
+
}),
|
|
139
|
+
//To be deprecated
|
|
140
|
+
'api-key': cli_command_1.flags.string({
|
|
141
|
+
description: 'API Key',
|
|
142
|
+
hidden: true,
|
|
143
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['api-key'], ['-k', 'stack-api-key']),
|
|
144
|
+
}),
|
|
145
|
+
force: cli_command_1.flags.boolean({
|
|
146
|
+
char: 'f',
|
|
147
|
+
hidden: true,
|
|
148
|
+
description: 'Force adding',
|
|
149
|
+
parse: (0, cli_utilities_1.printFlagDeprecation)(['-f', '--force'], ['-y', '--yes']),
|
|
150
|
+
}),
|
|
151
|
+
};
|
|
152
|
+
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
|