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
/
AbstractSession.ts
338 lines (299 loc) · 12.9 KB
/
AbstractSession.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* 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 { ISession } from "./doc/ISession";
import { Logger } from "../../../logger";
import { ImperativeError } from "../../../error";
import { ImperativeExpect } from "../../../expect";
import * as SessConstants from "./SessConstants";
/**
* The API session object, serves as the base for sessions and contains the fields that are required by
* most API calls (hostname, port, credentials, etc).
* @export
* @abstract
* @class AbstractSession
*/
export abstract class AbstractSession {
/**
* Basic auth prefix
* @static
* @type {string}
* @memberof AbstractSession
*/
public static readonly BASIC_PREFIX: string = "Basic ";
/**
* Bearer auth prefix
* @static
* @type {string}
* @memberof AbstractSession
*/
public static readonly BEARER_PREFIX: string = "Bearer ";
/**
* Default protocol
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_PROTOCOL = SessConstants.HTTPS_PROTOCOL;
/**
* Default session type
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_TYPE = SessConstants.AUTH_TYPE_NONE;
/**
* Default http port 80
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_HTTP_PORT = 80;
/**
* Default https port 443
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_HTTPS_PORT = 443;
/**
* Default https port
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_PORT = AbstractSession.DEFAULT_HTTPS_PORT;
/**
* Default base path.
* Our empty string means that we do **not** use an API mediation layer
* base path at the beginning of every resource URL.
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_BASE_PATH = "";
/**
* Default reject unauthorized
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_REJECT_UNAUTHORIZED_SETTING = true;
/**
* Default strict ssl setting
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_STRICT_SSL = true;
/**
* Default SSL method
* @static
* @memberof AbstractSession
*/
public static readonly DEFAULT_SECURE_PROTOCOL = "SSLv23_method";
// TODO: Investigate - this does not seem to do anything, and Node defaults to TLS_method w/ TLS 1.3 support
/**
* Regex to extract basic from base64 encoded auth
* @static
* @type {RegExp}
* @memberof AbstractSession
*/
public static readonly BASIC: RegExp = /^Basic/ig;
/**
* Obtain user name from a base 64 credential
* @static
* @param {string} auth - base 64 encoded credentials
* @returns {string} - user name
* @memberof AbstractSession
*/
public static getUsernameFromAuth(auth: string): string {
auth = auth.replace(AbstractSession.BASIC, "");
const decoding = Buffer.from(auth, "base64").toString();
return decoding.substring(0, decoding.lastIndexOf(":"));
}
/**
* Obtain password from a base 64 credential
* @static
* @param {string} auth - base 64 encoded credentials
* @returns {string} - password
* @memberof AbstractSession
*/
public static getPasswordFromAuth(auth: string): string {
auth = auth.replace(AbstractSession.BASIC, "");
const decoding = Buffer.from(auth, "base64").toString();
return decoding.substring(decoding.lastIndexOf(":") + 1);
}
/**
* Create base 64 encoded representation of user and password
* @static
* @param user - plain text user
* @param password - plain text password
* @returns {string} - base 64 encoded auth
* @memberof AbstractSession
*/
public static getBase64Auth(user: string, password: string) {
return Buffer.from(user + ":" + password).toString("base64");
}
/**
* Logging object
*/
private mLog: Logger;
/**
* Creates an instance of AbstractSession.
* @param {ISession} session: Session parameter object
* @memberof AbstractSession
*/
constructor(private mISession: ISession) {
this.mLog = Logger.getImperativeLogger();
mISession = this.buildSession(mISession);
}
/**
* Method to parse the requested token type
* @param {*} cookie - cookie object from http(s) response
* @memberof AbstractSession
*/
public storeCookie(cookie: any) {
const headerKeys: string[] = Object.keys(cookie);
headerKeys.forEach((key) => {
const auth = cookie[key] as string;
const authArr = auth.split(";");
// see each field in the cookie, e/g. Path=/; Secure; HttpOnly; LtpaToken2=...
authArr.forEach((element: string) => {
// if element begins with tokenType, extract full tokenType and tokenValue.
if (element.indexOf(this.ISession.tokenType) === 0) {
// parse off token value, splitting element at first "=".
const split = element.indexOf("=");
if (split >= 0) {
this.ISession.tokenType = element.substring(0, split);
this.ISession.tokenValue = element.substring(split + 1);
}
}
});
});
}
/**
* Builds an ISession so all required pieces are filled in
* @private
* @param {ISession} session - the fully populated session
* @memberof AbstractSession
*/
private buildSession(session: ISession): ISession {
const populatedSession = session;
// set protocol if not set
if (populatedSession.protocol === undefined || populatedSession.protocol === null) {
populatedSession.protocol = AbstractSession.DEFAULT_PROTOCOL;
}
// set rejectUnauthorized
if (populatedSession.rejectUnauthorized === undefined || populatedSession.rejectUnauthorized === null) {
populatedSession.rejectUnauthorized = AbstractSession.DEFAULT_REJECT_UNAUTHORIZED_SETTING;
}
// set strictSSL
if (populatedSession.strictSSL === undefined || populatedSession.strictSSL === null) {
populatedSession.strictSSL = AbstractSession.DEFAULT_STRICT_SSL;
}
// set port if not set
if (populatedSession.port === undefined || populatedSession.port === null) {
if (populatedSession.protocol === SessConstants.HTTP_PROTOCOL) {
populatedSession.port = AbstractSession.DEFAULT_HTTP_PORT;
} else if (populatedSession.protocol === SessConstants.HTTPS_PROTOCOL) {
populatedSession.port = AbstractSession.DEFAULT_HTTPS_PORT;
}
}
// set protocol if not set
if (populatedSession.secureProtocol === undefined || populatedSession.secureProtocol === null) {
populatedSession.secureProtocol = AbstractSession.DEFAULT_SECURE_PROTOCOL;
}
// set basePath if not set
if (populatedSession.basePath === undefined || populatedSession.basePath === null) {
populatedSession.basePath = AbstractSession.DEFAULT_BASE_PATH;
}
// set type if not set
if (populatedSession.type === undefined || populatedSession.type === null) {
populatedSession.type = AbstractSession.DEFAULT_TYPE;
}
// populatedSession.type = populatedSession.type.toLocaleLowerCase();
ImperativeExpect.keysToBeDefinedAndNonBlank(populatedSession, ["hostname"]);
ImperativeExpect.toBeOneOf(populatedSession.type,
[SessConstants.AUTH_TYPE_NONE, SessConstants.AUTH_TYPE_BASIC, SessConstants.AUTH_TYPE_TOKEN,
SessConstants.AUTH_TYPE_BEARER, SessConstants.AUTH_TYPE_CERT_PEM]); // , SessConstants.AUTH_TYPE_CERT_PFX]);
ImperativeExpect.toBeOneOf(populatedSession.protocol, [SessConstants.HTTPS_PROTOCOL, SessConstants.HTTP_PROTOCOL]);
// if basic auth, must have user and password OR base 64 encoded credentials
if (session.type === SessConstants.AUTH_TYPE_BASIC) {
if (session.user !== undefined && session.user !== null &&
session.password !== undefined && session.password !== null)
{
// ok
} else if (session.base64EncodedAuth !== undefined && session.base64EncodedAuth !== null) {
// ok
} else {
throw new ImperativeError({
msg: "Must have user & password OR base64 encoded credentials",
additionalDetails: "For CLI usage, see '<your-cli> auth login <service> --help'"
});
}
ImperativeExpect.keysToBeUndefined(populatedSession, ["tokenType", "tokenValue", "cert", "certKey"]);
}
// if bearer auth, must have token
if (session.type === SessConstants.AUTH_TYPE_BEARER) {
ImperativeExpect.keysToBeDefinedAndNonBlank(populatedSession, ["tokenValue"] );
ImperativeExpect.keysToBeUndefined(populatedSession, ["tokenType", "user", "password", "cert", "certKey"] );
}
if (session.type === SessConstants.AUTH_TYPE_TOKEN) {
ImperativeExpect.keysToBeDefinedAndNonBlank(session, ["tokenType"], "You must provide a token type to use cookie authentication");
if (populatedSession.tokenValue === undefined || populatedSession.tokenValue === null) {
if (session.user !== undefined && session.user !== null &&
session.password !== undefined && session.password !== null)
{
// ok
} else if (session.base64EncodedAuth !== undefined && session.base64EncodedAuth !== null) {
// ok
} else {
throw new ImperativeError({
// msg: "Must have user & password OR tokenType & tokenValue OR cert & certKey OR cert & passphrase",
msg: "Must have user & password OR tokenType & tokenValue OR cert & certKey.",
additionalDetails: "For CLI usage, see '<your-cli> auth login <service> --help'"
});
}
}
}
if (session.type === SessConstants.AUTH_TYPE_CERT_PEM) {
ImperativeExpect.keysToBeDefinedAndNonBlank(populatedSession, ["cert", "certKey"]);
ImperativeExpect.keysToBeUndefined(populatedSession, ["tokenValue", "user", "password"] );
ImperativeExpect.toNotBeEqual(populatedSession.protocol, SessConstants.HTTP_PROTOCOL,
"Certificate based authentication cannot be used over HTTP. Please set protocol to HTTPS to use certificate authentication.");
}
// if (session.type === SessConstants.AUTH_TYPE_CERT_PFX) {
// ImperativeExpect.keysToBeDefinedAndNonBlank(populatedSession, ["cert", "passphrase"]);
// ImperativeExpect.toNotBeEqual(populatedSession.protocol, SessConstants.HTTP_PROTOCOL,
// "Certificate based authentication cannot be used over HTTP. Please set protocol to HTTPS to use certificate authentication.");
// }
// if basic auth
if (populatedSession.type === SessConstants.AUTH_TYPE_BASIC || populatedSession.type === SessConstants.AUTH_TYPE_TOKEN) {
// get base 64 encoded auth if not provided
if (populatedSession.base64EncodedAuth === undefined || populatedSession.base64EncodedAuth === null) {
if (populatedSession.user !== undefined && populatedSession.user !== null &&
populatedSession.password !== undefined && populatedSession.password !== null)
{
populatedSession.base64EncodedAuth = AbstractSession.getBase64Auth(populatedSession.user, populatedSession.password);
}
} else {
if (populatedSession.user === undefined || populatedSession.user === null) {
populatedSession.user = AbstractSession.getUsernameFromAuth(populatedSession.base64EncodedAuth);
}
if (populatedSession.password === undefined || populatedSession.password === null) {
populatedSession.password = AbstractSession.getPasswordFromAuth(populatedSession.base64EncodedAuth);
}
}
}
return populatedSession;
}
/**
* Obtain session info and defaults
* @readonly
* @type {ISession}
* @memberof AbstractSession
*/
get ISession(): ISession {
return this.mISession;
}
}