UNPKG

@itwin/core-frontend

Version:
54 lines 2.39 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.headersIncludeAuthMethod = headersIncludeAuthMethod; exports.setBasicAuthorization = setBasicAuthorization; exports.setRequestTimeout = setRequestTimeout; const core_common_1 = require("@itwin/core-common"); /** * Check whether or not one of the requested authentication method is listed in the HTTP 'WWW-Authenticate' response header * @param headers Headers object * @param query List of authentication method to lookup (case-insensitive) * @note For CORS requests, the 'Access-Control-Expose-Headers' header from the server must make the 'WWW-Authenticate' available to the browser, otherwise this won't work. * @internal */ function headersIncludeAuthMethod(headers, query) { const wwwAuthenticate = headers.get("WWW-authenticate"); const lowerCaseQuery = query.map(((value) => value.toLowerCase())); // not case-sensitive if (wwwAuthenticate !== null) { const authMethods = wwwAuthenticate.split(",").map(((value) => value.toLowerCase().trim())); for (const queryValue of lowerCaseQuery) { if (authMethods.includes(queryValue)) return true; } } return false; } /** @internal */ function setBasicAuthorization(headers, userOrCreds, password) { let username; let pwd; if (typeof userOrCreds === "string") { username = userOrCreds; pwd = password; } else { username = userOrCreds.user; pwd = userOrCreds.password; } if (username !== undefined && pwd !== undefined) headers.set("Authorization", `Basic ${core_common_1.Base64EncodedString.encode(`${username}:${pwd}`)}`); } /** * Set number of milliseconds a request can take before automatically being terminated * @internal */ function setRequestTimeout(opts, ms, abortController) { const controller = abortController ?? new AbortController(); setTimeout(() => controller.abort(), ms); opts.signal = controller.signal; } //# sourceMappingURL=utils.js.map