Skip to content
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

Throw when initialize has not been called #6233

Merged
merged 32 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
77a6237
Block all calls before initialize and fix public client app specs to …
hectormmg Jul 12, 2023
94a8f87
Fix PCANonBrowser tests to call initialize every time
hectormmg Jul 12, 2023
269dfd6
Fix BaseInteractionClient specs
hectormmg Jul 12, 2023
bdcd416
Fix popup and redirect client specs to match new initialize requirement
hectormmg Jul 12, 2023
b75de74
Change files
hectormmg Jul 12, 2023
43698ee
Merge branch 'dev' into throw-without-initialize
hectormmg Jul 12, 2023
58bff40
Remove unnecessary initialize not called block from getNativeAccountId
hectormmg Jul 12, 2023
94549ec
Merge branch 'throw-without-initialize' of github.com:AzureAD/microso…
hectormmg Jul 12, 2023
11379cf
Merge branch 'dev' into throw-without-initialize
hectormmg Jul 17, 2023
f42bdd8
Merge branch 'dev' into throw-without-initialize
hectormmg Jul 19, 2023
63bdf21
Update customizable-e2e-test sample so tests wait for initialization …
hectormmg Jul 19, 2023
7c20108
Merge branch 'throw-without-initialize' of github.com:AzureAD/microso…
hectormmg Jul 19, 2023
377a10c
Add pcaInitializePoller to pop sample tests and the rest of customiza…
hectormmg Jul 19, 2023
9933c50
Update onPageLoad sample with initialize poller
hectormmg Jul 19, 2023
80ba1e4
Update client-capabilities sample with initialize poller
hectormmg Jul 19, 2023
fb6fc87
Fix onPageLoad tests
hectormmg Jul 19, 2023
be2c781
Fix onPageLoad tests
hectormmg Jul 19, 2023
9647ee3
Merge branch 'dev' into throw-without-initialize
hectormmg Jul 20, 2023
8884c59
Fix multiple_resources samples to use initialized flag
hectormmg Jul 20, 2023
38346d3
Merge branch 'throw-without-initialize' of github.com:AzureAD/microso…
hectormmg Jul 20, 2023
723de29
Update react samples with initialize
hectormmg Jul 20, 2023
b4c1ede
Replace instantiation then initialization with createPublicClientAppl…
hectormmg Jul 20, 2023
51a0fd3
Update react samples to use initialize again
hectormmg Jul 21, 2023
724846c
Replace createPublicClientApplication in TS react sample with instant…
hectormmg Jul 21, 2023
3de9243
Merge branch 'dev' into throw-without-initialize
hectormmg Jul 24, 2023
4dc7f53
Merge branch 'dev' into throw-without-initialize
hectormmg Jul 24, 2023
6876eaf
Merge branch 'dev' into throw-without-initialize
hectormmg Jul 25, 2023
5ea4fcd
Remove pre-initialize blockers from account APIs
hectormmg Jul 25, 2023
29d72d6
Update PCA tests
hectormmg Jul 25, 2023
c8bc0eb
Fix MsalProvider test by calling initialize before handleRedirectPromise
hectormmg Jul 25, 2023
12ff94e
Change files
hectormmg Jul 25, 2023
bce4fb2
Rename initialization enforcing method to blockAPICallsBeforeInitialize
hectormmg Jul 25, 2023
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": "prerelease",
"comment": "Throw when initialize has not been called #6233",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Throw when initialize has not been called #6233",
"packageName": "@azure/msal-react",
"email": "[email protected]",
"dependentChangeType": "none"
}
35 changes: 15 additions & 20 deletions lib/msal-browser/src/controllers/StandardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
RequestThumbprint,
ServerError,
ServerResponseType,
UrlString
UrlString,
} from "@azure/msal-common";
import {
BrowserCacheManager,
Expand Down Expand Up @@ -320,15 +320,15 @@ export class StandardController implements IController {
hash?: string
): Promise<AuthenticationResult | null> {
this.logger.verbose("handleRedirectPromise called");
// Block token acquisition before initialize has been called if native brokering is enabled
BrowserUtils.blockNativeBrokerCalledBeforeInitialized(
this.config.system.allowNativeBroker,
this.initialized
);
// Block token acquisition before initialize has been called
BrowserUtils.blockAPICallsBeforeInitialize(this.initialized);

let foundServerResponse = hash;

if(this.config.auth.OIDCOptions?.serverResponseType === ServerResponseType.QUERY) {

if (
this.config.auth.OIDCOptions?.serverResponseType ===
ServerResponseType.QUERY
) {
const url = window.location.href;
foundServerResponse = UrlString.parseQueryServerResponse(url);
}
Expand All @@ -340,7 +340,8 @@ export class StandardController implements IController {
* otherwise return the promise from the first invocation. Prevents race conditions when handleRedirectPromise is called
* several times concurrently.
*/
const redirectResponseKey = foundServerResponse || Constants.EMPTY_STRING;
const redirectResponseKey =
foundServerResponse || Constants.EMPTY_STRING;
let response = this.redirectResponse.get(redirectResponseKey);
if (typeof response === "undefined") {
this.eventHandler.emitEvent(
Expand Down Expand Up @@ -394,7 +395,9 @@ export class StandardController implements IController {
const redirectClient =
this.createRedirectClient(correlationId);
redirectResponse =
redirectClient.handleRedirectPromise(foundServerResponse);
redirectClient.handleRedirectPromise(
foundServerResponse
);
}

response = redirectResponse
Expand Down Expand Up @@ -1306,16 +1309,8 @@ export class StandardController implements IController {
// Block redirectUri opened in a popup from calling MSAL APIs
BrowserUtils.blockAcquireTokenInPopups();

/*
* Block token acquisition before initialize has been called if native brokering is enabled in top-frame.
* Skip check if application is embedded.
*/
if (!isAppEmbedded) {
BrowserUtils.blockNativeBrokerCalledBeforeInitialized(
this.config.system.allowNativeBroker,
this.initialized
);
}
// Block token acquisition before initialize has been called
BrowserUtils.blockAPICallsBeforeInitialize(this.initialized);

// Block redirects if memory storage is enabled but storeAuthStateInCookie is not
if (
Expand Down
12 changes: 6 additions & 6 deletions lib/msal-browser/src/error/BrowserAuthError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ export const BrowserAuthErrorMessage = {
code: "native_connection_not_established",
desc: "Connection to native platform has not been established. Please install a compatible browser extension and run initialize(). For more please visit aka.ms/msaljs/browser-errors.",
},
nativeBrokerCalledBeforeInitialize: {
code: "native_broker_called_before_initialize",
desc: "You must call and await the initialize function before attempting to call any other MSAL API when native brokering is enabled. For more please visit aka.ms/msaljs/browser-errors.",
uninitializedPublicClientApplication: {
code: "uninitialized_public_client_application",
desc: "You must call and await the initialize function before attempting to call any other MSAL API. For more please visit aka.ms/msaljs/browser-errors.",
},
nativePromptNotSupported: {
code: "native_prompt_not_supported",
Expand Down Expand Up @@ -672,10 +672,10 @@ export class BrowserAuthError extends AuthError {
/**
* Create an error thrown when the initialize function hasn't been called
*/
static createNativeBrokerCalledBeforeInitialize(): BrowserAuthError {
static createUninitializedPublicClientApplication(): BrowserAuthError {
return new BrowserAuthError(
BrowserAuthErrorMessage.nativeBrokerCalledBeforeInitialize.code,
BrowserAuthErrorMessage.nativeBrokerCalledBeforeInitialize.desc
BrowserAuthErrorMessage.uninitializedPublicClientApplication.code,
BrowserAuthErrorMessage.uninitializedPublicClientApplication.desc
);
}

Expand Down
12 changes: 4 additions & 8 deletions lib/msal-browser/src/utils/BrowserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,12 @@ export class BrowserUtils {
}

/**
* Throws error if native brokering is enabled but initialize hasn't been called
* @param allowNativeBroker
* Throws error if initialize hasn't been called
* @param initialized
*/
static blockNativeBrokerCalledBeforeInitialized(
allowNativeBroker: boolean,
initialized: boolean
): void {
if (allowNativeBroker && !initialized) {
throw BrowserAuthError.createNativeBrokerCalledBeforeInitialize();
static blockAPICallsBeforeInitialize(initialized: boolean): void {
if (!initialized) {
throw BrowserAuthError.createUninitializedPublicClientApplication();
}
}

Expand Down
17 changes: 9 additions & 8 deletions lib/msal-browser/test/app/PCANonBrowser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,24 @@ describe("Non-browser environment", () => {
});
});

it("getAllAccounts returns empty array", () => {
it("getAllAccounts returns empty array", async () => {
const instance = new PublicClientApplication({
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
},
});

await instance.initialize();
const accounts = instance.getAllAccounts();
expect(accounts).toEqual([]);
});

it("getAccountByUsername returns null", () => {
it("getAccountByUsername returns null", async () => {
const instance = new PublicClientApplication({
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
},
});

await instance.initialize();
const account = instance.getAccountByUsername("[email protected]");
expect(account).toEqual(null);
});
Expand All @@ -167,10 +167,11 @@ describe("Non-browser environment", () => {
allowNativeBroker: false,
},
});

instance.handleRedirectPromise().then((result) => {
expect(result).toBeNull();
done();
instance.initialize().then(() => {
instance.handleRedirectPromise().then((result) => {
expect(result).toBeNull();
done();
});
});
});

Expand Down
Loading
Loading