text: /*
   * Copyright (c) Microsoft Corporation. All rights reserved.
   * Licensed under the MIT License.
   */
  import * as tslib_1 from "tslib";
  import { ClientConfigurationErrorMessage } from "../error/ClientConfigurationError";
  import { XhrClient } from "../XHRClient";
  import { UrlUtils } from "../utils/UrlUtils";
  import HttpEvent from "../telemetry/HttpEvent";
  /**
   * @hidden
   */
  export var AuthorityType;
  (function (AuthorityType) {
      AuthorityType[AuthorityType["Aad"] = 0] = "Aad";
      AuthorityType[AuthorityType["Adfs"] = 1] = "Adfs";
      AuthorityType[AuthorityType["B2C"] = 2] = "B2C";
  })(AuthorityType || (AuthorityType = {}));
  /**
   * @hidden
   */
  var Authority = /** @class */ (function () {
      function Authority(authority, validateAuthority, authorityMetadata) {
          this.IsValidationEnabled = validateAuthority;
          this.CanonicalAuthority = authority;
          this.validateAsUri();
          this.tenantDiscoveryResponse = authorityMetadata;
      }
      Object.defineProperty(Authority.prototype, "Tenant", {
          get: function () {
              return this.CanonicalAuthorityUrlComponents.PathSegments[0];
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(Authority.prototype, "AuthorizationEndpoint", {
          get: function () {
              this.validateResolved();
              return this.tenantDiscoveryResponse.AuthorizationEndpoint.replace(/{tenant}|{tenantid}/g, this.Tenant);
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(Authority.prototype, "EndSessionEndpoint", {
          get: function () {
              this.validateResolved();
              return this.tenantDiscoveryResponse.EndSessionEndpoint.replace(/{tenant}|{tenantid}/g, this.Tenant);
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(Authority.prototype, "SelfSignedJwtAudience", {
          get: function () {
              this.validateResolved();
              return this.tenantDiscoveryResponse.Issuer.replace(/{tenant}|{tenantid}/g, this.Tenant);
          },
          enumerable: true,
          configurable: true
      });
      Authority.prototype.validateResolved = function () {
          if (!this.hasCachedMetadata()) {
              throw "Please call ResolveEndpointsAsync first";
          }
      };
      Object.defineProperty(Authority.prototype, "CanonicalAuthority", {
          /**
           * A URL that is the authority set by the developer
           */
          get: function () {
              return this.canonicalAuthority;
          },
          set: function (url) {
              this.canonicalAuthority = UrlUtils.CanonicalizeUri(url);
              this.canonicalAuthorityUrlComponents = null;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(Authority.prototype, "CanonicalAuthorityUrlComponents", {
          get: function () {
              if (!this.canonicalAuthorityUrlComponents) {
                  this.canonicalAuthorityUrlComponents = UrlUtils.GetUrlComponents(this.CanonicalAuthority);
              }
              return this.canonicalAuthorityUrlComponents;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(Authority.prototype, "DefaultOpenIdConfigurationEndpoint", {
          /**
           * // http://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata
           */
          get: function () {
              return this.CanonicalAuthority + "v2.0/.well-known/openid-configuration";
          },
          enumerable: true,
          configurable: true
      });
      /**
       * Given a string, validate that it is of the form https://domain/path
       */
      Authority.prototype.validateAsUri = function () {
          var components;
          try {
              components = this.CanonicalAuthorityUrlComponents;
          }
          catch (e) {
              throw ClientConfigurationErrorMessage.invalidAuthorityType;
          }
          if (!components.Protocol || components.Protocol.toLowerCase() !== "https:") {
              throw ClientConfigurationErrorMessage.authorityUriInsecure;
          }
          if (!components.PathSegments || components.PathSegments.length < 1) {
              throw ClientConfigurationErrorMessage.authorityUriInvalidPath;
          }
      };
      /**
       * Calls the OIDC endpoint and returns the response
       */
      Authority.prototype.DiscoverEndpoints = function (openIdConfigurationEndpoint, telemetryManager, correlationId) {
          var client = new XhrClient();
          var httpMethod = "GET";
          var httpEvent = new HttpEvent(correlationId, "openIdConfigurationEndpoint");
          httpEvent.url = openIdConfigurationEndpoint;
          httpEvent.httpMethod = httpMethod;
          telemetryManager.startEvent(httpEvent);
          return client.sendRequestAsync(openIdConfigurationEndpoint, httpMethod, /* enableCaching: */ true)
              .then(function (response) {
              httpEvent.httpResponseStatus = response.statusCode;
              telemetryManager.stopEvent(httpEvent);
              return {
                  AuthorizationEndpoint: response.body.authorization_endpoint,
                  EndSessionEndpoint: response.body.end_session_endpoint,
                  Issuer: response.body.issuer
              };
          })
              .catch(function (err) {
              httpEvent.serverErrorCode = err;
              telemetryManager.stopEvent(httpEvent);
              throw err;
          });
      };
      /**
       * Returns a promise.
       * Checks to see if the authority is in the cache
       * Discover endpoints via openid-configuration
       * If successful, caches the endpoint for later use in OIDC
       */
      Authority.prototype.resolveEndpointsAsync = function (telemetryManager, correlationId) {
          return tslib_1.__awaiter(this, void 0, void 0, function () {
              var openIdConfigurationEndpointResponse, _a;
              return tslib_1.__generator(this, function (_b) {
                  switch (_b.label) {
                      case 0: return [4 /*yield*/, this.GetOpenIdConfigurationEndpointAsync(telemetryManager, correlationId)];
                      case 1:
                          openIdConfigurationEndpointResponse = _b.sent();
                          _a = this;
                          return [4 /*yield*/, this.DiscoverEndpoints(openIdConfigurationEndpointResponse, telemetryManager, correlationId)];
                      case 2:
                          _a.tenantDiscoveryResponse = _b.sent();
                          return [2 /*return*/, this.tenantDiscoveryResponse];
                  }
              });
          });
      };
      /**
       * Checks if there is a cached tenant discovery response with required fields.
       */
      Authority.prototype.hasCachedMetadata = function () {
          return !!(this.tenantDiscoveryResponse &&
              this.tenantDiscoveryResponse.AuthorizationEndpoint &&
              this.tenantDiscoveryResponse.EndSessionEndpoint &&
              this.tenantDiscoveryResponse.Issuer);
      };
      return Authority;
  }());
  export { Authority };
  //# sourceMappingURL=Authority.js.map