"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 });
const zosmf_for_zowe_sdk_1 = require("@zowe/zosmf-for-zowe-sdk");
const imperative_1 = require("@zowe/imperative");
const core_for_zowe_sdk_1 = require("@zowe/core-for-zowe-sdk");
/**
 * This class is used by the auth command handlers as the base class for their implementation.
 */
class ApimlAuthHandler extends imperative_1.BaseAuthHandler {
    constructor() {
        super(...arguments);
        /**
         * The profile type where token type and value should be stored
         */
        this.mProfileType = "base";
        /**
         * The default token type to use if not specified as a command line option
         */
        this.mDefaultTokenType = imperative_1.SessConstants.TOKEN_TYPE_APIML;
        /**
         * The description of your service to be used in CLI prompt messages
         */
        this.mServiceDescription = "your API Mediation Layer";
        /**
         * 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.
         * @param {ICommandArguments} args The command line arguments to use for building the session
         * @returns {ISession} The session object built from the command line arguments.
         */
        this.createSessCfgFromArgs = zosmf_for_zowe_sdk_1.ZosmfSession.createSessCfgFromArgs;
    }
    /**
     * Extends the functionality of BaseAuthHandler.processLogin to place a property of
     * authOrder: "token" into the default zosmf profile.
     * @param {IHandlerParameters} params Command parameters sent by imperative.
     */
    processLogin(params) {
        const _super = Object.create(null, {
            processLogin: { get: () => super.processLogin }
        });
        return __awaiter(this, void 0, void 0, function* () {
            var _a;
            yield _super.processLogin.call(this, params);
            // get the name of the default zosmf profile
            const addAuthOrderMsg = 'Add the following authOrder property to a zosmf profile that contains a basePath property.\n' +
                '    "authOrder": "token, bearer"';
            const zosmfProfNm = (_a = imperative_1.ImperativeConfig.instance.config) === null || _a === void 0 ? void 0 : _a.properties.defaults["zosmf"];
            if (!zosmfProfNm) {
                params.response.console.log(`\nYou have no default zosmf profile. ${addAuthOrderMsg}`);
                return;
            }
            const config = imperative_1.ImperativeConfig.instance.config;
            if (!config.api.profiles.exists(zosmfProfNm)) {
                params.response.console.log(`\nYour default zosmf profile (${zosmfProfNm}) does not exist. ${addAuthOrderMsg}`);
                return;
            }
            const zosmfProfObj = config.api.profiles.get(zosmfProfNm, true);
            if (!(zosmfProfObj === null || zosmfProfObj === void 0 ? void 0 : zosmfProfObj.basePath)) {
                params.response.console.log(`\nYour default zosmf profile (${zosmfProfNm}) has no basePath, thus it cannot be used with APIML. ` +
                    `${addAuthOrderMsg}`);
                return;
            }
            // Ensure that the zosmf profile uses the newly acquired token by setting authOrder to token
            yield imperative_1.AuthOrder.putNewAuthsFirstOnDisk(zosmfProfNm, [imperative_1.SessConstants.AUTH_TYPE_TOKEN, imperative_1.SessConstants.AUTH_TYPE_BEARER], { onlyTheseAuths: true, clientConfig: config });
        });
    }
    /**
     * This is called by the "auth login" command after it creates a session, to
     * obtain a token that can be stored in a profile.
     * @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
     */
    doLogin(session) {
        return __awaiter(this, void 0, void 0, function* () {
            return core_for_zowe_sdk_1.Login.apimlLogin(session);
        });
    }
    /**
     * This is called by the "auth logout" command after it creates a session, to
     * revoke a token before removing it from a profile.
     * @param {AbstractSession} session The session object to use to connect to the auth service
     */
    doLogout(session) {
        return __awaiter(this, void 0, void 0, function* () {
            return core_for_zowe_sdk_1.Logout.apimlLogout(session);
        });
    }
}
exports.default = ApimlAuthHandler;
//# sourceMappingURL=ApimlAuthHandler.js.map