This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
BaseAuthHandler.ts
250 lines (215 loc) · 10.5 KB
/
BaseAuthHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { ICommandHandler, IHandlerParameters, ICommandArguments } from "../../../../cmd";
import { Constants } from "../../../../constants";
import { ISession, ConnectionPropsForSessCfg, Session, SessConstants, AbstractSession } from "../../../../rest";
import { Imperative } from "../../Imperative";
import { ImperativeExpect } from "../../../../expect";
import { ImperativeError } from "../../../../error";
import { ISaveProfileFromCliArgs } from "../../../../profiles";
import { CliUtils } from "../../../../utilities";
/**
* This class is used by the auth command handlers as the base class for their implementation.
*/
export abstract class BaseAuthHandler implements ICommandHandler {
/**
* The profile type where token type and value should be stored
*/
protected abstract mProfileType: string;
/**
* The default token type to use if not specified as a command line option
*/
protected abstract mDefaultTokenType: SessConstants.TOKEN_TYPE_CHOICES;
/**
* The session being created from the command line arguments / profile
*/
protected mSession: AbstractSession;
/**
* This handler is used for both "auth login" and "auth logout" commands.
* It determines the correct action to take and calls either `processLogin`
* or `processLogout` accordingly.
*
* @param {IHandlerParameters} commandParameters Command parameters sent by imperative.
*
* @returns {Promise<void>}
*/
public async process(commandParameters: IHandlerParameters) {
switch (commandParameters.positionals[1]) {
case Constants.LOGIN_ACTION:
await this.processLogin(commandParameters);
break;
case Constants.LOGOUT_ACTION:
await this.processLogout(commandParameters);
break;
default:
throw new ImperativeError({
msg: `The group name "${commandParameters.positionals[1]}" was passed to the BaseAuthHandler, but it is not valid.`
});
break;
}
}
/**
* This is called by the {@link BaseAuthHandler#process} when it needs a
* session. Should be used to create a session to connect to the auth
* service.
* @abstract
* @param {ICommandArguments} args The command line arguments to use for building the session
* @returns {ISession} The session object built from the command line arguments.
*/
protected abstract createSessCfgFromArgs(args: ICommandArguments): ISession;
/**
* This is called by the "auth login" command after it creates a session, to
* obtain a token that can be stored in a profile.
* @abstract
* @param {AbstractSession} session The session object to use to connect to the auth service
* @returns {Promise<string>} The response from the auth service containing a token
*/
protected abstract async doLogin(session: AbstractSession): Promise<string>;
/**
* This is called by the "auth logout" command after it creates a session, to
* revoke a token before removing it from a profile.
* @abstract
* @param {AbstractSession} session The session object to use to connect to the auth service
*/
protected abstract async doLogout(session: AbstractSession): Promise<void>;
/**
* Performs the login operation. Builds a session to connect to the auth
* service, sends a login request to it to obtain a token, and stores the
* resulting token in the profile of type `mProfileType`.
* @param {IHandlerParameters} params Command parameters sent by imperative.
*/
private async processLogin(params: IHandlerParameters) {
const loadedProfile = params.profiles.getMeta(this.mProfileType, false);
const sessCfg: ISession = this.createSessCfgFromArgs(
params.arguments
);
const sessCfgWithCreds = await ConnectionPropsForSessCfg.addPropsOrPrompt<ISession>(
sessCfg, params.arguments,
{ requestToken: true, defaultTokenType: this.mDefaultTokenType }
);
this.mSession = new Session(sessCfgWithCreds);
// login to obtain a token.
const tokenValue = await this.doLogin(this.mSession);
// validate a token was returned
if (tokenValue == null) {
throw new ImperativeError({msg: "A token value was not returned from the login handler."});
}
// update the profile given
if (!params.arguments.showToken) {
let profileWithToken: string = null;
if (loadedProfile != null && loadedProfile.name != null) {
await Imperative.api.profileManager(this.mProfileType).update({
name: loadedProfile.name,
args: {
"token-type": this.mSession.ISession.tokenType,
"token-value": tokenValue
},
merge: true
});
profileWithToken = loadedProfile.name;
} else {
// Do not store non-profile arguments, user, or password. Set param arguments for prompted values from session.
const copyArgs = {...params.arguments};
copyArgs.createProfile = undefined;
copyArgs.showToken = undefined;
copyArgs.user = undefined;
copyArgs.password = undefined;
copyArgs.host = this.mSession.ISession.hostname;
copyArgs.port = this.mSession.ISession.port;
copyArgs.tokenType = this.mSession.ISession.tokenType;
copyArgs["token-type"] = this.mSession.ISession.tokenType;
copyArgs.tokenValue = tokenValue;
copyArgs["token-value"] = tokenValue;
const createParms: ISaveProfileFromCliArgs = {
name: "default",
type: this.mProfileType,
args: copyArgs,
overwrite: false,
profile: {}
};
const answer: string = await CliUtils.promptWithTimeout(
`Do you want to store the host, port, and token on disk for use with future commands? If you answer Yes, the credentials will ` +
`be saved to a ${this.mProfileType} profile named '${createParms.name}'. If you answer No, the token will be printed to the ` +
`terminal and will not be stored on disk. [y/N]: `);
if (answer != null && (answer.toLowerCase() === "y" || answer.toLowerCase() === "yes")) {
await Imperative.api.profileManager(this.mProfileType).save(createParms);
profileWithToken = createParms.name;
} else {
params.arguments.showToken = true;
}
}
if (profileWithToken != null) {
params.response.console.log(`\n` +
`Login successful. The authentication token is stored in the '${profileWithToken}' ` +
`${this.mProfileType} profile for future use. To revoke this token and remove it from your profile, review the ` +
`'zowe auth logout' command.`);
}
}
// show token instead of updating profile
if (params.arguments.showToken) {
params.response.console.log(`\n` +
`Received a token of type = ${this.mSession.ISession.tokenType}.\n` +
`The following token was retrieved and will not be stored in your profile:\n` +
`${tokenValue}\n\n` +
`Login successful. To revoke this token, review the 'zowe auth logout' command.`
);
params.response.data.setObj({tokenType: this.mSession.ISession.tokenType, tokenValue});
}
}
/**
* Performs the logout operation. Deletes the token and token type from the profile,
* and rebuilds the session.
* @param {IHandlerParameters} params Command parameters sent by imperative.
*/
private async processLogout(params: IHandlerParameters) {
const loadedProfile = params.profiles.getMeta(this.mProfileType, false);
ImperativeExpect.toNotBeNullOrUndefined(params.arguments.tokenValue, "Token value not supplied, but is required for logout.");
// Force to use of token value, in case user and/or password also on base profile, make user undefined.
if (params.arguments.user != null) {
params.arguments.user = undefined;
}
params.arguments.tokenType = this.mDefaultTokenType;
const sessCfg: ISession = this.createSessCfgFromArgs(
params.arguments
);
const sessCfgWithCreds = await ConnectionPropsForSessCfg.addPropsOrPrompt<ISession>(
sessCfg, params.arguments,
{ requestToken: false }
);
this.mSession = new Session(sessCfgWithCreds);
await this.doLogout(this.mSession);
// If you specified a token on the command line, then don't delete the one in the profile if it doesn't match
let profileWithToken: string = null;
if (loadedProfile != null &&
loadedProfile.name != null &&
loadedProfile.profile != null &&
loadedProfile.profile.tokenValue != null &&
params.arguments.tokenValue === loadedProfile.profile.tokenValue) {
await Imperative.api.profileManager(this.mProfileType).save({
name: loadedProfile.name,
type: loadedProfile.type,
overwrite: true,
profile: {
...loadedProfile.profile,
tokenType: undefined,
tokenValue: undefined
}
});
profileWithToken = loadedProfile.name;
}
this.mSession.ISession.type = SessConstants.AUTH_TYPE_BASIC;
this.mSession.ISession.tokenType = undefined;
this.mSession.ISession.tokenValue = undefined;
params.response.console.log("Logout successful. The authentication token has been revoked" +
(profileWithToken != null ? ` and removed from your '${profileWithToken}' ${this.mProfileType} profile` : "") +
".");
}
}