UNPKG

10.3 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License.
5 */
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.UrlUtils = void 0;
8var Constants_1 = require("./Constants");
9var ScopeSet_1 = require("../ScopeSet");
10var StringUtils_1 = require("./StringUtils");
11var CryptoUtils_1 = require("./CryptoUtils");
12/**
13 * @hidden
14 */
15var UrlUtils = /** @class */ (function () {
16 function UrlUtils() {
17 }
18 /**
19 * generates the URL with QueryString Parameters
20 * @param scopes
21 */
22 UrlUtils.createNavigateUrl = function (serverRequestParams) {
23 var str = this.createNavigationUrlString(serverRequestParams);
24 var authEndpoint = serverRequestParams.authorityInstance.AuthorizationEndpoint;
25 // if the endpoint already has queryparams, lets add to it, otherwise add the first one
26 if (authEndpoint.indexOf("?") < 0) {
27 authEndpoint += "?";
28 }
29 else {
30 authEndpoint += "&";
31 }
32 var requestUrl = "" + authEndpoint + str.join("&");
33 return requestUrl;
34 };
35 /**
36 * Generate the array of all QueryStringParams to be sent to the server
37 * @param scopes
38 */
39 UrlUtils.createNavigationUrlString = function (serverRequestParams) {
40 var scopes = ScopeSet_1.ScopeSet.appendDefaultScopes(serverRequestParams.scopes);
41 var str = [];
42 str.push("response_type=" + serverRequestParams.responseType);
43 str.push("scope=" + encodeURIComponent(ScopeSet_1.ScopeSet.parseScope(scopes)));
44 str.push("client_id=" + encodeURIComponent(serverRequestParams.clientId));
45 str.push("redirect_uri=" + encodeURIComponent(serverRequestParams.redirectUri));
46 str.push("state=" + encodeURIComponent(serverRequestParams.state));
47 str.push("nonce=" + encodeURIComponent(serverRequestParams.nonce));
48 str.push("client_info=1");
49 str.push("x-client-SKU=" + serverRequestParams.xClientSku);
50 str.push("x-client-Ver=" + serverRequestParams.xClientVer);
51 if (serverRequestParams.promptValue) {
52 str.push("prompt=" + encodeURIComponent(serverRequestParams.promptValue));
53 }
54 if (serverRequestParams.claimsValue) {
55 str.push("claims=" + encodeURIComponent(serverRequestParams.claimsValue));
56 }
57 if (serverRequestParams.queryParameters) {
58 str.push(serverRequestParams.queryParameters);
59 }
60 if (serverRequestParams.extraQueryParameters) {
61 str.push(serverRequestParams.extraQueryParameters);
62 }
63 str.push("client-request-id=" + encodeURIComponent(serverRequestParams.correlationId));
64 return str;
65 };
66 /**
67 * Returns current window URL as redirect uri
68 */
69 UrlUtils.getCurrentUrl = function () {
70 return window.location.href.split("?")[0].split("#")[0];
71 };
72 /**
73 * Returns given URL with query string removed
74 */
75 UrlUtils.removeHashFromUrl = function (url) {
76 return url.split("#")[0];
77 };
78 /**
79 * Given a url like https://a:b/common/d?e=f#g, and a tenantId, returns https://a:b/tenantId/d
80 * @param href The url
81 * @param tenantId The tenant id to replace
82 */
83 UrlUtils.replaceTenantPath = function (url, tenantId) {
84 var lowerCaseUrl = url.toLowerCase();
85 var urlObject = this.GetUrlComponents(lowerCaseUrl);
86 var pathArray = urlObject.PathSegments;
87 if (tenantId && (pathArray.length !== 0 && (pathArray[0] === Constants_1.Constants.common || pathArray[0] === Constants_1.SSOTypes.ORGANIZATIONS || pathArray[0] === Constants_1.SSOTypes.CONSUMERS))) {
88 pathArray[0] = tenantId;
89 }
90 return this.constructAuthorityUriFromObject(urlObject, pathArray);
91 };
92 UrlUtils.constructAuthorityUriFromObject = function (urlObject, pathArray) {
93 return this.CanonicalizeUri(urlObject.Protocol + "//" + urlObject.HostNameAndPort + "/" + pathArray.join("/"));
94 };
95 /**
96 * Checks if an authority is common (ex. https://a:b/common/)
97 * @param url The url
98 * @returns true if authority is common and false otherwise
99 */
100 UrlUtils.isCommonAuthority = function (url) {
101 var authority = this.CanonicalizeUri(url);
102 var pathArray = this.GetUrlComponents(authority).PathSegments;
103 return (pathArray.length !== 0 && pathArray[0] === Constants_1.Constants.common);
104 };
105 /**
106 * Checks if an authority is for organizations (ex. https://a:b/organizations/)
107 * @param url The url
108 * @returns true if authority is for and false otherwise
109 */
110 UrlUtils.isOrganizationsAuthority = function (url) {
111 var authority = this.CanonicalizeUri(url);
112 var pathArray = this.GetUrlComponents(authority).PathSegments;
113 return (pathArray.length !== 0 && pathArray[0] === Constants_1.SSOTypes.ORGANIZATIONS);
114 };
115 /**
116 * Checks if an authority is for consumers (ex. https://a:b/consumers/)
117 * @param url The url
118 * @returns true if authority is for and false otherwise
119 */
120 UrlUtils.isConsumersAuthority = function (url) {
121 var authority = this.CanonicalizeUri(url);
122 var pathArray = this.GetUrlComponents(authority).PathSegments;
123 return (pathArray.length !== 0 && pathArray[0] === Constants_1.SSOTypes.CONSUMERS);
124 };
125 /**
126 * Parses out the components from a url string.
127 * @returns An object with the various components. Please cache this value insted of calling this multiple times on the same url.
128 */
129 UrlUtils.GetUrlComponents = function (url) {
130 if (!url) {
131 throw "Url required";
132 }
133 // https://gist.github.com/curtisz/11139b2cfcaef4a261e0
134 var regEx = RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
135 var match = url.match(regEx);
136 if (!match || match.length < 6) {
137 throw "Valid url required";
138 }
139 var urlComponents = {
140 Protocol: match[1],
141 HostNameAndPort: match[4],
142 AbsolutePath: match[5]
143 };
144 var pathSegments = urlComponents.AbsolutePath.split("/");
145 pathSegments = pathSegments.filter(function (val) { return val && val.length > 0; }); // remove empty elements
146 urlComponents.PathSegments = pathSegments;
147 if (match[6]) {
148 urlComponents.Search = match[6];
149 }
150 if (match[8]) {
151 urlComponents.Hash = match[8];
152 }
153 return urlComponents;
154 };
155 /**
156 * Given a url or path, append a trailing slash if one doesnt exist
157 *
158 * @param url
159 */
160 UrlUtils.CanonicalizeUri = function (url) {
161 if (url) {
162 var lowerCaseUrl = url.toLowerCase();
163 if (!UrlUtils.endsWith(lowerCaseUrl, "/")) {
164 lowerCaseUrl += "/";
165 }
166 return lowerCaseUrl;
167 }
168 return url;
169 };
170 /**
171 * Checks to see if the url ends with the suffix
172 * Required because we are compiling for es5 instead of es6
173 * @param url
174 * @param str
175 */
176 // TODO: Rename this, not clear what it is supposed to do
177 UrlUtils.endsWith = function (url, suffix) {
178 if (!url || !suffix) {
179 return false;
180 }
181 return url.indexOf(suffix, url.length - suffix.length) !== -1;
182 };
183 /**
184 * Utils function to remove the login_hint and domain_hint from the i/p extraQueryParameters
185 * @param url
186 * @param name
187 */
188 UrlUtils.urlRemoveQueryStringParameter = function (url, name) {
189 if (StringUtils_1.StringUtils.isEmpty(url)) {
190 return url;
191 }
192 var updatedUrl = url;
193 var regex = new RegExp("(\\&" + name + "=)[^\&]+");
194 updatedUrl = url.replace(regex, "");
195 // name=value&
196 regex = new RegExp("(" + name + "=)[^\&]+&");
197 updatedUrl = url.replace(regex, "");
198 // name=value
199 regex = new RegExp("(" + name + "=)[^\&]+");
200 updatedUrl = url.replace(regex, "");
201 return updatedUrl;
202 };
203 /**
204 * @hidden
205 * @ignore
206 *
207 * Returns the anchor part(#) of the URL
208 */
209 UrlUtils.getHashFromUrl = function (urlStringOrFragment) {
210 var hashIndex1 = urlStringOrFragment.indexOf("#");
211 var hashIndex2 = urlStringOrFragment.indexOf("#/");
212 if (hashIndex2 > -1) {
213 return urlStringOrFragment.substring(hashIndex2 + 2);
214 }
215 else if (hashIndex1 > -1) {
216 return urlStringOrFragment.substring(hashIndex1 + 1);
217 }
218 return urlStringOrFragment;
219 };
220 /**
221 * @hidden
222 * Check if the url contains a hash with known properties
223 * @ignore
224 */
225 UrlUtils.urlContainsHash = function (urlString) {
226 var parameters = UrlUtils.deserializeHash(urlString);
227 return (parameters.hasOwnProperty(Constants_1.ServerHashParamKeys.ERROR_DESCRIPTION) ||
228 parameters.hasOwnProperty(Constants_1.ServerHashParamKeys.ERROR) ||
229 parameters.hasOwnProperty(Constants_1.ServerHashParamKeys.ACCESS_TOKEN) ||
230 parameters.hasOwnProperty(Constants_1.ServerHashParamKeys.ID_TOKEN));
231 };
232 /**
233 * @hidden
234 * Returns deserialized portion of URL hash
235 * @ignore
236 */
237 UrlUtils.deserializeHash = function (urlFragment) {
238 var hash = UrlUtils.getHashFromUrl(urlFragment);
239 return CryptoUtils_1.CryptoUtils.deserialize(hash);
240 };
241 /**
242 * @ignore
243 * @param {string} URI
244 * @returns {string} host from the URI
245 *
246 * extract URI from the host
247 */
248 UrlUtils.getHostFromUri = function (uri) {
249 // remove http:// or https:// from uri
250 var extractedUri = String(uri).replace(/^(https?:)\/\//, "");
251 extractedUri = extractedUri.split("/")[0];
252 return extractedUri;
253 };
254 return UrlUtils;
255}());
256exports.UrlUtils = UrlUtils;
257//# sourceMappingURL=UrlUtils.js.map
\No newline at end of file