8000 Add support for new platform broker flow via DOM API by lalimasharda · Pull Request #7632 · AzureAD/microsoft-authentication-library-for-js · GitHub
[go: up one dir, main page]

Skip to content
10000

Add support for new platform broker flow via DOM API #7632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
5e6321f
added isPlatformBrokerAvailable API
lalimasharda Mar 11, 2025
ce0d102
updated isPlatformBrokerAvailable implementation
lalimasharda Mar 12, 2025
a1b0fa1
code cleanup
lalimasharda Mar 12, 2025
883daa4
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda Mar 12, 2025
381473c
updated isPlatformBrokerAvailable()
lalimasharda Mar 18, 2025
1f9481c
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda Mar 18, 2025
0707c51
Merge branch 'dev' into addEdgeNativeApi
sameerag Mar 19, 2025
15dcb07
added initializeNativeDOMRequest
lalimasharda Mar 31, 2025
259f730
resolving merge conflicts
lalimasharda Apr 10, 2025
1ffd693
updated StandardController with latest changes
lalimasharda Apr 10, 2025
97c61f6
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda Apr 16, 2025
623c4b2
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda Apr 22, 2025
e0c368a
native dom api updates
lalimasharda Apr 23, 2025
bffb842
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda Apr 23, 2025
88d9323
saving updates
lalimasharda Apr 23, 2025
7a29309
updates to dom native support and added feature flag to enable dom n…
lalimasharda Apr 29, 2025
86d5753
Merge branch 'addEdgeNativeApi' of https://github.com/AzureAD/microso…
lalimasharda Apr 29, 2025
ff88027
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda Apr 29, 2025
ecf1b3a
refactoring NativeInteractionClient and addressing some comments
lalimasharda May 1, 2025
00b6cb5
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda May 1, 2025
074e618
added IPlatformBrokerHandler interface
lalimasharda May 1, 2025
0811338
added IPlatformAuthHandler interface and renamed Native to PlatformAuth
lalimasharda May 2, 2025
fb078dc
addressing comments
lalimasharda May 2, 2025
6feef52
addressing comments
lalimasharda May 6, 2025
9bcc2a8
added support for enabling dom api feature through sessionStorage
lalimasharda May 6, 2025
0aa2809
resolving merge conflicts
lalimasharda May 6, 2025
1a6541c
updated sample with isPlatformBrokerAvailable usage
lalimasharda May 6, 2025
e3d0606
updating comment
lalimasharda May 6, 2025
94007c2
Change files
lalimasharda May 6, 2025
635dbbe
change file updates
lalimasharda May 6, 2025
6e7443c
hardcoding brokerId param
lalimasharda May 6, 2025
5ad7546
fixing isDomEnabledForPlatformAuth
lalimasharda May 6, 2025
7643939
addressed commits and fixed unit tests
lalimasharda May 7, 2025
63e27f6
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda May 7, 2025
6a519bc
added tests for PlatformAuthDOMHandler
lalimasharda May 9, 2025
75f3721
removing console statement
lalimasharda May 9, 2025
d4339e9
small updates
lalimasharda May 9, 2025
3f0455e
addressing comments and added unit tests
lalimasharda May 12, 2025
05570d3
Update lib/msal-browser/src/broker/nativeBroker/PlatformAuthProvider.ts
lalimasharda May 12, 2025
e975305
updating msal-browser-api
lalimasharda May 12, 2025
2c5400b
Merge branch 'addEdgeNativeApi' of https://github.com/AzureAD/microso…
lalimasharda May 12, 2025
bcb22a5
undoing prettier formatting changes
lalimasharda May 12, 2025
b4391f7
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
lalimasharda May 14, 2025
05e23d6
updating logger statements
lalimasharda May 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Add support for new platform broker flow via DOM API #7632",
"packageName": "@azure/msal-browser",
"email": "lalimasharda@microsoft.com",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions lib/msal-browser/apiReview/msal-browser.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,13 @@ function isInIframe(): boolean;
// @public
function isInPopup(): boolean;

// Warning: (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
// Warning: (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
// Warning: (ae-missing-release-tag) "isPlatformBrokerAvailable" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public
export function isPlatformBrokerAvailable(loggerOptions?: LoggerOptions, perfClient?: IPerformanceClient, correlationId?: string): Promise<boolean>;

// Warning: (ae-missing-release-tag) "ITokenCache" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { PlatformBrokerRequest } from "./PlatformBrokerRequest.js";
import { PlatformBrokerResponse } from "./PlatformBrokerResponse.js";

/**
* Interface for the Platform Broker Handlers
*/
export interface IPlatformAuthHandler {
getExtensionId(): string | undefined;
getExtensionVersion(): string | undefined;
getExtensionName(): string | undefined;
sendMessage(
request: PlatformBrokerRequest
): Promise<PlatformBrokerResponse>;
}
241 changes: 241 additions & 0 deletions lib/msal-browser/src/broker/nativeBroker/PlatformAuthDOMHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import {
Logger,
createAuthError,
AuthErrorCodes,
IPerformanceClient,
StringDict,
} from "@azure/msal-common/browser";
import {
PlatformBrokerRequest,
PlatformDOMTokenRequest,
} from "./PlatformBrokerRequest.js";
import { PlatformAuthConstants } from "../../utils/BrowserConstants.js";
import {
PlatformBrokerResponse,
PlatformDOMTokenResponse,
} from "./PlatformBrokerResponse.js";
import { createNativeAuthError } from "../../error/NativeAuthError.js";
import { IPlatformAuthHandler } from "./IPlatformAuthHandler.js";

export class PlatformAuthDOMHandler implements IPlatformAuthHandler {
protected logger: Logger;
protected performanceClient: IPerformanceClient;
protected correlationId: string;
platformAuthType: string;

constructor(
logger: Logger,
performanceClient: IPerformanceClient,
correlationId: string
) {
this.logger = logger;
this.performanceClient = performanceClient;
this.correlationId = correlationId;
this.platformAuthType = PlatformAuthConstants.PLATFORM_DOM_PROVIDER;
}

static async createProvider(
logger: Logger,
performanceClient: IPerformanceClient,
correlationId: string
): Promise<PlatformAuthDOMHandler | undefined> {
logger.trace("PlatformAuthDOMHandler: createProvider called");

// @ts-ignore
if (window.navigator?.platformAuthentication) {
const supportedContracts =
// @ts-ignore
await window.navigator.platformAuthentication.getSupportedContracts(
PlatformAuthConstants.MICROSOFT_ENTRA_BROKERID
);
if (
supportedContracts?.includes(
PlatformAuthConstants.PLATFORM_DOM_APIS
)
) {
logger.trace("Platform auth api available in DOM");
return new PlatformAuthDOMHandler(
logger,
performanceClient,
correlationId
);
}
}
return undefined;
}

/**
* Returns the Id for the broker extension this handler is communicating with
* @returns
*/
getExtensionId(): string {
return PlatformAuthConstants.MICROSOFT_ENTRA_BROKERID;
}

getExtensionVersion(): string | undefined {
return "";
}

getExtensionName(): string | undefined {
return PlatformAuthConstants.DOM_API_NAME;
}

/**
* Send token request to platform broker via browser DOM API
* @param request
* @returns
*/
async sendMessage(
request: PlatformBrokerRequest
): Promise<PlatformBrokerResponse> {
this.logger.trace(
this.platformAuthType + " - Sending request to browser DOM API"
);

try {
const platformDOMRequest: PlatformDOMTokenRequest =
this.initializePlatformDOMRequest(request);
const response: object =
// @ts-ignore
await window.navigator.platformAuthentication.executeGetToken(
platformDOMRequest
);
return this.validatePlatformBrokerResponse(response);
} catch (e) {
this.logger.error(
this.platformAuthType + " - executeGetToken DOM API error"
);
throw e;
}
}

private initializePlatformDOMRequest(
request: PlatformBrokerRequest
): PlatformDOMTokenRequest {
this.logger.trace(
this.platformAuthType + " - initializeNativeDOMRequest called"
);

const {
accountId,
clientId,
authority,
scope,
redirectUri,
correlationId,
state,
storeInCache,
embeddedClientId,
extraParameters,
...remainingProperties
} = request;

const validExtraParameters =
this.stringifyExtraParameters(remainingProperties);

const platformDOMRequest: PlatformDOMTokenRequest = {
accountId: accountId,
brokerId: this.getExtensionId(),
authority: authority,
clientId: clientId,
correlationId: correlationId || this.correlationId,
extraParameters: { ...extraParameters, ...validExtraParameters },
isSecurityTokenService: false,
redirectUri: redirectUri,
scope: scope,
state: state,
storeInCache: storeInCache,
embeddedClientId: embeddedClientId,
};

return platformDOMRequest;
}

private validatePlatformBrokerResponse(
response: object
): PlatformBrokerResponse {
if (response.hasOwnProperty("isSuccess")) {
if (
response.hasOwnProperty("accessToken") &&
response.hasOwnProperty("idToken") &&
response.hasOwnProperty("clientInfo") &&
response.hasOwnProperty("account") &&
response.hasOwnProperty("scopes") &&
response.hasOwnProperty("expiresIn")
) {
this.logger.trace(
this.platformAuthType +
" - platform broker returned successful and valid response"
);
return this.convertToPlatformBrokerResponse(
response as PlatformDOMTokenResponse
);
} else if (response.hasOwnProperty("error")) {
const errorResponse = response as PlatformDOMTokenResponse;
if (
errorResponse.isSuccess === false &&
errorResponse.error &&
errorResponse.error.code
) {
this.logger.trace(
this.platformAuthType +
" - platform broker returned error response"
);
throw createNativeAuthError(
errorResponse.error.code,
errorResponse.error.description,
{
error: parseInt(errorResponse.error.errorCode),
protocol_error: errorResponse.error.protocolError,
status: errorResponse.error.status,
properties: errorResponse.error.properties,
}
);
}
}
}
throw createAuthError(
AuthErrorCodes.unexpectedError,
"Response missing expected properties."
);
}

private convertToPlatformBrokerResponse(
response: PlatformDOMTokenResponse
): PlatformBrokerResponse {
this.logger.trace(
this.platformAuthType + " - convertToNativeResponse called"
);
const nativeResponse: PlatformBrokerResponse = {
access_token: response.accessToken,
id_token: response.idToken,
client_info: response.clientInfo,
account: response.account,
expires_in: response.expiresIn,
scope: response.scopes,
state: response.state || "",
properties: response.properties || {},
extendedLifetimeToken: response.extendedLifetimeToken ?? false,
shr: response.proofOfPossessionPayload,
};

return nativeResponse;
}

private stringifyExtraParameters(
extraParameters: Record<string, unknown>
): StringDict {
return Object.entries(extraParameters).reduce(
(record, [key, value]) => {
record[key] = String(value);
return record;
},
{} as StringDict
);
}
}
Loading
0