text: "use strict";
  /**
   * Copyright 2019 IBM Corp. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  var __extends = (this && this.__extends) || (function () {
      var extendStatics = function (d, b) {
          extendStatics = Object.setPrototypeOf ||
              ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
              function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
          return extendStatics(d, b);
      };
      return function (d, b) {
          extendStatics(d, b);
          function __() { this.constructor = d; }
          d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
      };
  })();
  Object.defineProperty(exports, "__esModule", { value: true });
  var extend = require("extend");
  var logger_1 = require("../../lib/logger");
  var utils_1 = require("../utils");
  var jwt_token_manager_1 = require("./jwt-token-manager");
  var CLIENT_ID_SECRET_WARNING = 'Warning: Client ID and Secret must BOTH be given, or the header will not be included.';
  var DEFAULT_IAM_URL = 'https://iam.cloud.ibm.com';
  var OPERATION_PATH = '/identity/token';
  /**
   * The IamRequestBasedTokenManager class contains code relevant to any token manager that
   * interacts with the IAM service to manage a token. It stores information relevant to all
   * IAM requests, such as the client ID and secret, and performs the token request with a set
   * of request options common to any IAM token management scheme. It is intended that this
   * class be extended with specific implementations.
   */
  var IamRequestBasedTokenManager = /** @class */ (function (_super) {
      __extends(IamRequestBasedTokenManager, _super);
      /**
       *
       * Create a new [[IamRequestBasedTokenManager]] instance.
       *
       * @param {object} options Configuration options.
       * @param {string} [options.clientId] The `clientId` and `clientSecret` fields are used to form a "basic"
       *   authorization header for IAM token requests.
       * @param {string} [options.clientSecret] The `clientId` and `clientSecret` fields are used to form a "basic"
       *   authorization header for IAM token requests.
       * @param {string} [url='https://iam.cloud.ibm.com'] The IAM endpoint for token requests.
       * @param {boolean} [options.disableSslVerification] A flag that indicates
       *   whether verification of the token server's SSL certificate should be
       *   disabled or not.
       * @param {object<string, string>} [options.headers] Headers to be sent with every
       *   outbound HTTP requests to token services.
       * @constructor
       */
      function IamRequestBasedTokenManager(options) {
          var _this = this;
          // all parameters are optional
          options = options || {};
          _this = _super.call(this, options) || this;
          // Canonicalize the URL by removing the operation path if it was specified by the user.
          _this.url = _this.url ? utils_1.removeSuffix(_this.url, OPERATION_PATH) : DEFAULT_IAM_URL;
          if (options.clientId) {
              _this.clientId = options.clientId;
          }
          if (options.clientSecret) {
              _this.clientSecret = options.clientSecret;
          }
          if (options.scope) {
              _this.scope = options.scope;
          }
          if (utils_1.onlyOne(options.clientId, options.clientSecret)) {
              // tslint:disable-next-line
              logger_1.default.warn(CLIENT_ID_SECRET_WARNING);
          }
          // initialize the form data object
          _this.formData = {};
          return _this;
      }
      /**
       * Set the IAM `scope` value.
       * This value is the form parameter to use when fetching the bearer token
       * from the IAM token server.
       *
       * @param {string} scope - A space seperated string that makes up the scope parameter.
       * @returns {void}
       */
      IamRequestBasedTokenManager.prototype.setScope = function (scope) {
          this.scope = scope;
      };
      /**
       * Set the IAM `clientId` and `clientSecret` values.
       * These values are used to compute the Authorization header used
       * when retrieving the IAM access token.
       * If these values are not set, no Authorization header will be
       * set on the request (it is not required).
       *
       * @param {string} clientId - The client id.
       * @param {string} clientSecret - The client secret.
       * @returns {void}
       */
      IamRequestBasedTokenManager.prototype.setClientIdAndSecret = function (clientId, clientSecret) {
          this.clientId = clientId;
          this.clientSecret = clientSecret;
          if (utils_1.onlyOne(clientId, clientSecret)) {
              // tslint:disable-next-line
              logger_1.default.warn(CLIENT_ID_SECRET_WARNING);
          }
      };
      /**
       * Request an IAM token using an API key.
       *
       * @returns {Promise}
       */
      IamRequestBasedTokenManager.prototype.requestToken = function () {
          // these cannot be overwritten
          var requiredHeaders = {
              'Content-type': 'application/x-www-form-urlencoded',
          };
          // If both the clientId and secret were specified by the user, then use them.
          if (this.clientId && this.clientSecret) {
              requiredHeaders.Authorization = utils_1.computeBasicAuthHeader(this.clientId, this.clientSecret);
          }
          if (this.scope) {
              this.formData.scope = this.scope;
          }
          var parameters = {
              options: {
                  url: this.url + OPERATION_PATH,
                  method: 'POST',
                  headers: extend(true, {}, this.headers, requiredHeaders),
                  form: this.formData,
                  rejectUnauthorized: !this.disableSslVerification,
              },
          };
          return this.requestWrapperInstance.sendRequest(parameters);
      };
      return IamRequestBasedTokenManager;
  }(jwt_token_manager_1.JwtTokenManager));
  exports.IamRequestBasedTokenManager = IamRequestBasedTokenManager;
  