msal
Version:
Microsoft Authentication Library for js
195 lines • 7.74 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
var Constants_1 = require("./Constants");
var ScopeSet_1 = require("../ScopeSet");
var StringUtils_1 = require("./StringUtils");
/**
* @hidden
*/
var UrlUtils = /** @class */ (function () {
function UrlUtils() {
}
/**
* generates the URL with QueryString Parameters
* @param scopes
*/
UrlUtils.createNavigateUrl = function (serverRequestParams) {
var str = this.createNavigationUrlString(serverRequestParams);
var authEndpoint = serverRequestParams.authorityInstance.AuthorizationEndpoint;
// if the endpoint already has queryparams, lets add to it, otherwise add the first one
if (authEndpoint.indexOf("?") < 0) {
authEndpoint += "?";
}
else {
authEndpoint += "&";
}
var requestUrl = "" + authEndpoint + str.join("&");
return requestUrl;
};
/**
* Generate the array of all QueryStringParams to be sent to the server
* @param scopes
*/
UrlUtils.createNavigationUrlString = function (serverRequestParams) {
var scopes = serverRequestParams.scopes;
if (scopes.indexOf(serverRequestParams.clientId) === -1) {
scopes.push(serverRequestParams.clientId);
}
var str = [];
str.push("response_type=" + serverRequestParams.responseType);
this.translateclientIdUsedInScope(scopes, serverRequestParams.clientId);
str.push("scope=" + encodeURIComponent(ScopeSet_1.ScopeSet.parseScope(scopes)));
str.push("client_id=" + encodeURIComponent(serverRequestParams.clientId));
str.push("redirect_uri=" + encodeURIComponent(serverRequestParams.redirectUri));
str.push("state=" + encodeURIComponent(serverRequestParams.state));
str.push("nonce=" + encodeURIComponent(serverRequestParams.nonce));
str.push("client_info=1");
str.push("x-client-SKU=" + serverRequestParams.xClientSku);
str.push("x-client-Ver=" + serverRequestParams.xClientVer);
if (serverRequestParams.promptValue) {
str.push("prompt=" + encodeURIComponent(serverRequestParams.promptValue));
}
if (serverRequestParams.claimsValue) {
str.push("claims=" + encodeURIComponent(serverRequestParams.claimsValue));
}
if (serverRequestParams.queryParameters) {
str.push(serverRequestParams.queryParameters);
}
if (serverRequestParams.extraQueryParameters) {
str.push(serverRequestParams.extraQueryParameters);
}
str.push("client-request-id=" + encodeURIComponent(serverRequestParams.correlationId));
return str;
};
/**
* append the required scopes: https://openid.net/specs/openid-connect-basic-1_0.html#Scopes
* @param scopes
*/
UrlUtils.translateclientIdUsedInScope = function (scopes, clientId) {
var clientIdIndex = scopes.indexOf(clientId);
if (clientIdIndex >= 0) {
scopes.splice(clientIdIndex, 1);
if (scopes.indexOf("openid") === -1) {
scopes.push("openid");
}
if (scopes.indexOf("profile") === -1) {
scopes.push("profile");
}
}
};
/**
* Returns current window URL as redirect uri
*/
UrlUtils.getDefaultRedirectUri = function () {
return window.location.href.split("?")[0].split("#")[0];
};
/**
* Given a url like https://a:b/common/d?e=f#g, and a tenantId, returns https://a:b/tenantId/d
* @param href The url
* @param tenantId The tenant id to replace
*/
UrlUtils.replaceTenantPath = function (url, tenantId) {
url = url.toLowerCase();
var urlObject = this.GetUrlComponents(url);
var pathArray = urlObject.PathSegments;
if (tenantId && (pathArray.length !== 0 && (pathArray[0] === Constants_1.Constants.common || pathArray[0] === Constants_1.SSOTypes.ORGANIZATIONS))) {
pathArray[0] = tenantId;
}
return this.constructAuthorityUriFromObject(urlObject, pathArray);
};
UrlUtils.constructAuthorityUriFromObject = function (urlObject, pathArray) {
return this.CanonicalizeUri(urlObject.Protocol + "//" + urlObject.HostNameAndPort + "/" + pathArray.join("/"));
};
/**
* Parses out the components from a url string.
* @returns An object with the various components. Please cache this value insted of calling this multiple times on the same url.
*/
UrlUtils.GetUrlComponents = function (url) {
if (!url) {
throw "Url required";
}
// https://gist.github.com/curtisz/11139b2cfcaef4a261e0
var regEx = RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
var match = url.match(regEx);
if (!match || match.length < 6) {
throw "Valid url required";
}
var urlComponents = {
Protocol: match[1],
HostNameAndPort: match[4],
AbsolutePath: match[5]
};
var pathSegments = urlComponents.AbsolutePath.split("/");
pathSegments = pathSegments.filter(function (val) { return val && val.length > 0; }); // remove empty elements
urlComponents.PathSegments = pathSegments;
return urlComponents;
};
/**
* Given a url or path, append a trailing slash if one doesnt exist
*
* @param url
*/
UrlUtils.CanonicalizeUri = function (url) {
if (url) {
url = url.toLowerCase();
}
if (url && !UrlUtils.endsWith(url, "/")) {
url += "/";
}
return url;
};
/**
* Checks to see if the url ends with the suffix
* Required because we are compiling for es5 instead of es6
* @param url
* @param str
*/
// TODO: Rename this, not clear what it is supposed to do
UrlUtils.endsWith = function (url, suffix) {
if (!url || !suffix) {
return false;
}
return url.indexOf(suffix, url.length - suffix.length) !== -1;
};
/**
* Utils function to remove the login_hint and domain_hint from the i/p extraQueryParameters
* @param url
* @param name
*/
UrlUtils.urlRemoveQueryStringParameter = function (url, name) {
if (StringUtils_1.StringUtils.isEmpty(url)) {
return url;
}
var regex = new RegExp("(\\&" + name + "=)[^\&]+");
url = url.replace(regex, "");
// name=value&
regex = new RegExp("(" + name + "=)[^\&]+&");
url = url.replace(regex, "");
// name=value
regex = new RegExp("(" + name + "=)[^\&]+");
url = url.replace(regex, "");
return url;
};
/**
* @hidden
* @ignore
*
* Returns the anchor part(#) of the URL
*/
UrlUtils.getHashFromUrl = function (urlStringOrFragment) {
var hashIndex1 = urlStringOrFragment.indexOf("#");
var hashIndex2 = urlStringOrFragment.indexOf("#/");
if (hashIndex2 > -1) {
return urlStringOrFragment.substring(hashIndex2 + 2);
}
else if (hashIndex1 > -1) {
return urlStringOrFragment.substring(hashIndex1 + 1);
}
return urlStringOrFragment;
};
return UrlUtils;
}());
exports.UrlUtils = UrlUtils;
//# sourceMappingURL=UrlUtils.js.map