@azure/msal-browser
Version:
Microsoft Authentication Library for js
169 lines (166 loc) • 7.66 kB
JavaScript
/*! @azure/msal-browser v5.30.0 2026-11-18 */
;
import { invokeAsync, ProtocolMode, Constants, createClientConfigurationError, ClientConfigurationErrorCodes, PopTokenGenerator, PerformanceEvents, JsonWebTokenAlgorithms } from '@azure/msal-common/browser';
import { InitializeBaseRequest } from '../telemetry/BrowserPerformanceEvents.mjs';
import { CryptoOps } from '../crypto/CryptoOps.mjs';
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const SUPPORTED_AUTHENTICATION_SCHEMES = new Set([
Constants.AuthenticationScheme.BEARER,
Constants.AuthenticationScheme.POP,
Constants.AuthenticationScheme.DPOP,
Constants.AuthenticationScheme.SSH,
]);
function validateSshRequest(request, correlationId) {
if (!request.sshJwk) {
throw createClientConfigurationError(ClientConfigurationErrorCodes.missingSshJwk, correlationId);
}
if (!request.sshKid) {
throw createClientConfigurationError(ClientConfigurationErrorCodes.missingSshKid, correlationId);
}
}
function validateDpopRequest(request, correlationId) {
if (!request.resourceRequestMethod?.trim() ||
!request.resourceRequestUri?.trim()) {
throw createClientConfigurationError(ClientConfigurationErrorCodes.dpopMissingResourceContext, correlationId);
}
}
/**
* Resolves the token-binding parameters needed before building authorize or token requests.
* DPoP requests provision a browser key for token endpoint redemption. Platform broker
* authorization requests retain the key for auth-code fallback but suppress dpop_jkt on
* /authorize. Platform broker PoP requests use a reqCnf confirmation claim.
*/
async function getTokenBindingRequestParams(request, tokenBindingKeyManager, logger, performanceClient) {
switch (request.authenticationScheme) {
case Constants.AuthenticationScheme.DPOP:
return {
dpopJkt: await tokenBindingKeyManager.provisionTokenBindingKey({
tokenBindingKeyType: Constants.AuthenticationScheme.DPOP.toLowerCase(),
tokenBindingKeyAlgorithm: JsonWebTokenAlgorithms.ES256,
correlationId: request.correlationId,
}),
};
case Constants.AuthenticationScheme.POP:
if (!request.platformBroker) {
return {};
}
const cryptoOps = new CryptoOps(logger, performanceClient);
if (request.popKid) {
return { reqCnf: cryptoOps.encodeKid(request.popKid) };
}
const popTokenGenerator = new PopTokenGenerator(cryptoOps, tokenBindingKeyManager, performanceClient);
const generatedReqCnfData = await invokeAsync(popTokenGenerator.generateCnf.bind(popTokenGenerator), PerformanceEvents.PopTokenGenerateCnf, logger, performanceClient, request.correlationId)(request, logger);
return { reqCnf: generatedReqCnfData.reqCnfString };
default:
return {};
}
}
/**
* Removes a DPoP key that was provisioned for an authorization attempt which
* did not complete. Cleanup failures are recorded without replacing the
* original authorization error.
*/
async function removeTokenBindingKeyOnFailure(request, tokenBindingKeyManager, logger, performanceClient) {
if (request.authenticationScheme !== Constants.AuthenticationScheme.DPOP ||
!request.dpopJkt) {
return;
}
try {
await tokenBindingKeyManager.removeTokenBindingKey(request.dpopJkt, request.correlationId);
}
catch {
logger.error("1su7ne", request.correlationId);
performanceClient.incrementFields({ removeTokenBindingKeyFailure: 1 }, request.correlationId);
}
}
/**
* Returns whether an MSAL-owned DPoP key is available in persistent storage
* before a redirect navigation. Custom managers without a persistence probe
* retain their existing behavior.
*/
async function isTokenBindingKeyPersistedForRedirect(request, tokenBindingKeyManager) {
const keyId = request.dpopJkt ?? request.keyId;
if (!keyId || !tokenBindingKeyManager.isTokenBindingKeyPersisted) {
return true;
}
return tokenBindingKeyManager.isTokenBindingKeyPersisted(keyId, request.correlationId);
}
/**
* Initializer function for all request APIs
* @param request
* @param config
* @param performanceClient
* @param logger
* @param correlationId
*/
async function initializeBaseRequest(request, config, performanceClient, logger, correlationId) {
const authority = request.authority || config.auth.authority;
const scopes = [...((request && request.scopes) || [])];
const validatedRequest = {
...request,
correlationId: request.correlationId,
authority,
scopes,
};
// Set authenticationScheme to BEARER if not explicitly set in the request
if (!validatedRequest.authenticationScheme) {
validatedRequest.authenticationScheme =
Constants.AuthenticationScheme.BEARER;
logger.verbose("1l4fwv", correlationId);
}
else {
if (!SUPPORTED_AUTHENTICATION_SCHEMES.has(validatedRequest.authenticationScheme)) {
throw createClientConfigurationError(ClientConfigurationErrorCodes.unsupportedAuthenticationScheme, correlationId);
}
switch (validatedRequest.authenticationScheme) {
case Constants.AuthenticationScheme.SSH:
validateSshRequest(request, correlationId);
break;
case Constants.AuthenticationScheme.DPOP: {
validateDpopRequest(request, correlationId);
break;
}
}
logger.verbose(`1ecmns ${validatedRequest.authenticationScheme}`, correlationId);
}
return validatedRequest;
}
async function initializeSilentRequest(request, account, config, performanceClient, logger) {
const baseRequest = await invokeAsync(initializeBaseRequest, InitializeBaseRequest, logger, performanceClient, request.correlationId)(request, config, performanceClient, logger, request.correlationId);
return {
...request,
...baseRequest,
account: account,
forceRefresh: request.forceRefresh || false,
};
}
/**
* Validates that the combination of request method, protocol mode and authorize body parameters is correct.
* Returns the validated or defaulted HTTP method or throws if the configured combination is invalid.
* @param interactionRequest
* @param protocolMode
* @returns
*/
function validateRequestMethod(interactionRequest, protocolMode) {
let httpMethod;
const requestMethod = interactionRequest.httpMethod;
if (protocolMode === ProtocolMode.EAR) {
// Validate that method can only be POST when protocol mode is EAR
if (requestMethod && requestMethod !== Constants.HttpMethod.POST) {
throw createClientConfigurationError(ClientConfigurationErrorCodes.invalidRequestMethodForEAR, "");
}
else {
httpMethod = Constants.HttpMethod.POST;
}
}
else {
// For non-EAR protocol modes, default to GET if httpMethod is not set
httpMethod = requestMethod || Constants.HttpMethod.GET;
}
return httpMethod;
}
export { getTokenBindingRequestParams, initializeBaseRequest, initializeSilentRequest, isTokenBindingKeyPersistedForRedirect, removeTokenBindingKeyOnFailure, validateRequestMethod };
//# sourceMappingURL=RequestHelpers.mjs.map