UNPKG

@zowe/core-for-zowe-sdk

Version:

Core libraries shared by Zowe SDK packages

102 lines 5.25 kB
"use strict"; /* * 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. * */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Logout = void 0; const imperative_1 = require("@zowe/imperative"); const ZosmfRestClient_1 = require("../rest/ZosmfRestClient"); const LogoutConstants_1 = require("./LogoutConstants"); /** * Class to handle logging out of APIML. * @export * @class Logout */ class Logout { /** * Perform APIML logout to invalidate LTPA2 or other token types. * @static * @param {AbstractSession} session * @returns * @memberof Logout */ static apimlLogout(session) { return __awaiter(this, void 0, void 0, function* () { imperative_1.Logger.getAppLogger().trace("Logout.logout()"); imperative_1.ImperativeExpect.toNotBeNullOrUndefined(session, "Required session must be defined"); imperative_1.ImperativeExpect.toMatchRegExp(session.ISession.tokenType, "^apimlAuthenticationToken.*", `Token type (${session.ISession.tokenType}) for API ML logout must start with 'apimlAuthenticationToken'.`); imperative_1.ImperativeExpect.toNotBeNullOrUndefined(session.ISession.tokenValue, "Session token not populated. Unable to log out."); const client = new ZosmfRestClient_1.ZosmfRestClient(session); try { yield client.request({ request: "POST", resource: LogoutConstants_1.LogoutConstants.APIML_V1_RESOURCE }); } catch (err) { let errorToThrow = err; for (const errorKey in LogoutConstants_1.LogoutConstants.APIML_V2_LOGOUT_ERR_LIST) { if (err.message.includes(LogoutConstants_1.LogoutConstants.APIML_V2_LOGOUT_ERR_LIST[errorKey])) { switch (errorKey) { case "V2_TOKEN_INVALID": // Token is invalid (logged out) case "V2_TOKEN_EXPIRED": // Token expired (trully expired) case "V2_TOKEN_MISSING": // Token type is not ^apimlAuthenticationToken.* errorToThrow = new imperative_1.ImperativeError({ msg: "Token is not valid or expired.\n" + "For CLI usage, see `zowe auth logout apiml --help`", errorCode: client.response.statusCode.toString() }); break; case "V1_TOKEN_EXPIRED": default: errorToThrow = null; break; } } } /** * NOTE: We will continue to check for `V2_TOKEN_...` keys until we know for sure that * invalid credentials do not return a 403 on certain APIML configurations with ACF2 */ // if (client.response.statusCode.toString() === "401") { // errorToThrow = new ImperativeError({ // msg: "Token is not valid or expired.\n" + // "For CLI usage, see `zowe auth logout apiml --help`", // errorCode: client.response.statusCode.toString() // }); // } if (errorToThrow) { throw errorToThrow; } } if (client.response.statusCode !== imperative_1.RestConstants.HTTP_STATUS_204) { if (!(client.response.statusCode === imperative_1.RestConstants.HTTP_STATUS_500 && client.dataString.includes(LogoutConstants_1.LogoutConstants.APIML_V1_TOKEN_EXP_ERR))) { throw new imperative_1.ImperativeError(client.populateError({ msg: `REST API Failure with HTTP(S) status ${client.response.statusCode}`, causeErrors: client.dataString, source: imperative_1.SessConstants.HTTP_PROTOCOL })); } } }); } } exports.Logout = Logout; //# sourceMappingURL=Logout.js.map