msal
Version:
Microsoft Authentication Library for js
146 lines • 5.67 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ClientConfigurationErrorMessage } from "./error/ClientConfigurationError";
import { XhrClient } from "./XHRClient";
import { UrlUtils } from "./utils/UrlUtils";
/**
* @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) {
this.IsValidationEnabled = validateAuthority;
this.CanonicalAuthority = authority;
this.validateAsUri();
}
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}", this.Tenant);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Authority.prototype, "EndSessionEndpoint", {
get: function () {
this.validateResolved();
return this.tenantDiscoveryResponse.EndSessionEndpoint.replace("{tenant}", this.Tenant);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Authority.prototype, "SelfSignedJwtAudience", {
get: function () {
this.validateResolved();
return this.tenantDiscoveryResponse.Issuer.replace("{tenant}", this.Tenant);
},
enumerable: true,
configurable: true
});
Authority.prototype.validateResolved = function () {
if (!this.tenantDiscoveryResponse) {
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) {
var client = new XhrClient();
return client.sendRequestAsync(openIdConfigurationEndpoint, "GET", /*enableCaching: */ true)
.then(function (response) {
return {
AuthorizationEndpoint: response.authorization_endpoint,
EndSessionEndpoint: response.end_session_endpoint,
Issuer: response.issuer
};
});
};
/**
* 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 () {
var _this = this;
var openIdConfigurationEndpoint = "";
return this.GetOpenIdConfigurationEndpointAsync().then(function (openIdConfigurationEndpointResponse) {
openIdConfigurationEndpoint = openIdConfigurationEndpointResponse;
return _this.DiscoverEndpoints(openIdConfigurationEndpoint);
}).then(function (tenantDiscoveryResponse) {
_this.tenantDiscoveryResponse = tenantDiscoveryResponse;
return _this;
});
};
return Authority;
}());
export { Authority };
//# sourceMappingURL=Authority.js.map