@microsoft/kiota-abstractions
Version:
Core abstractions for kiota generated libraries in TypeScript and JavaScript
42 lines • 1.49 kB
JavaScript
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { inNodeEnv } from "../utils/index.js";
const localhostStrings = new Set(["localhost", "[::1]", "::1", "127.0.0.1"]);
/**
* Validates the protocol of the url.
* @param url - The url to validate.
*/
export function validateProtocol(url) {
if (!isLocalhostUrl(url) && !url.toLocaleLowerCase().startsWith("https://") && !windowUrlStartsWithHttps()) {
throw new Error("Authentication scheme can only be used with https requests");
}
}
/**
* Checks if the window url starts with https.
* @returns True if the window url starts with https, false otherwise.
*/
function windowUrlStartsWithHttps() {
if (!inNodeEnv()) {
return window.location.protocol.toLocaleLowerCase() === "https:";
}
return false;
}
/**
* Checks if the url is a localhost url.
* @param urlString - The url to check.
* @returns True if the url is a localhost url, false otherwise.
*/
export function isLocalhostUrl(urlString) {
try {
const url = new URL(urlString);
return localhostStrings.has(url.hostname);
}
catch (_a) {
return false;
}
}
//# sourceMappingURL=validateProtocol.js.map