Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.07k stars 1.19k forks source link

uninitialized_public_client_application error thrown on InteractiveBrowserCredential #28215

Closed PhllpSchrdr-boop closed 8 months ago

PhllpSchrdr-boop commented 9 months ago

Describe the bug When using an InteractiveBrowserCredential it throws an exception with the message:

uninitialized_public_client_application: You must call and await the initialize function before attempting to call any other MSAL API

To Reproduce Steps to reproduce the behavior:

  1. Create an angular app:
    C:\Projects\Sandbox.Angular\uninitialized_public_client_application>ng new uninitialized_public_client_application-demo -s -t -S
    ? Would you like to add Angular routing? No
    ? Which stylesheet format would you like to use? CSS
  2. install azure\identity: npm install @azure/identity
  3. Add a constructor to app.component.ts (replacing and with a valid Azure tenant id and client id):
    constructor() {
    var interactiveBrowserCredential: InteractiveBrowserCredential = new InteractiveBrowserCredential({
      tenantId: "<tenant_id>",
      clientId: "<client_id>",
      redirectUri: "http://localhost:4200"
    });
    interactiveBrowserCredential.getToken("User.Read.All")
      .then(accessTokenResponse => {
      })
      .catch(e => {
        console.log(e);
      })
    }
  4. Run the app: ng serve
  5. Browse the app (http://localhost:4200) with dev tools open to console and the error is disaplayed: BrowserAuthError: uninitialized_public_client_application: You must call and await the initialize function before attempting to call any other MSAL API. For more visit: aka.ms/msaljs/browser-errors

Expected behavior There should not be an exception thrown

Screenshots

Additional context https://github.com/Azure/azure-sdk-for-js/blob/c7c9bd4bef175a8914cdfdfe6d8e6fe1714a926d/sdk/identity/identity/src/msal/browserFlows/msalAuthCode.ts#L48C20-L48C20

The MSALAuthCode creates a PublicClientApplication which is never initialized.

The PublicClientApplication has a static function which will create a PublicClientApplication and initialize the standard controller:

https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/603098e62124b90c13dcd6e57a7d83d95cc07ce8/lib/msal-browser/src/app/PublicClientApplication.ts#L39C8-L39C8

Reverting to v2.1.0 of azure/identity resolves the issue. Any version later than v2.1.0 exhibits the issue.

maorleger commented 9 months ago

@KarishmaGhiya I was able to repro this - happy to pair if you'd like on the investigation. From looking around a little bit it does look like we should be calling initialize sometime before handleRedirect or (ideally) use the static factory function which will call initialize as part of its implementation

But.... I have not looked at this code closely yet and would need your ๐Ÿ‘€ to confirm ๐Ÿ˜„

PhllpSchrdr-boop commented 9 months ago

Maor,

Thanks for the contact.

For now, I am going to use v2.1.0 . I will keep an eye on the issue if your team determines the resolution. I would like to be able to use the latest version.

maorleger commented 9 months ago

@PhllpSchrdr-boop thank you for bringing this issue to our attention - I especially appreciate the minimal repro steps which allowed me to quickly reproduce this. Next we will discuss internally on an appropriate path forward - meanwhile I am happy to hear 2.1.0 works for you but I would love to get your upgrade unblocked as soon as we can ๐Ÿ˜„

Stay tuned!

KarishmaGhiya commented 9 months ago

@PhllpSchrdr-boop As we are investigating into this issue, can you check what version of @azure/msal-browser is this using maybe under your package-lock.json OR under node_modules folder?

PhllpSchrdr-boop commented 9 months ago

package-lock.json

@azure/msal-browser is 3.7.0

Attached is the full package-lock.json for your reference.

KarishmaGhiya commented 9 months ago

Hello, we have identified what the bug is and currently working on it for the fix.

PhllpSchrdr-boop commented 9 months ago

Great! I will be on the lookout.

maorleger commented 9 months ago

๐Ÿ‘‹ Hiya @PhllpSchrdr-boop and @bramvdbruggen - we have a fix implemented and released to our dev tag. We'd love your take on whether it resolves your issue!

In order to get the latest development version you'd want to npm install @azure/identity@dev

You could then confirm you have the right version with npm ls @azure/identity - if it shows as "4.0.1-alpha.20240118.2" you should be able to test this out.

Would love your confirmation before releasing this patch. Otherwise we will plan to release it early next week.

Thanks again!

github-actions[bot] commented 9 months ago

Hi @PhllpSchrdr-boop. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

PhllpSchrdr-boop commented 8 months ago

@maorleger Following your instructions and using 4.0.1-alpha.20240118.2 of @azure/identity has fixed the issue.

When can I expect a GA release?

Regards

KarishmaGhiya commented 8 months ago

We'll release on Monday. Thanks for confirming! :)

ghost commented 8 months ago

@maorleger Following your instructions and using 4.0.1-alpha.20240118.2 of @azure/identity has fixed the issue.

My issues have also been resolved with this fix! I will be awaiting the release on Monday.

maorleger commented 8 months ago

Hiya folks, 4.0.1 has been released with the fix. We will work on backporting the fix to 3.4 as well and will update this thread when that has been released

github-actions[bot] commented 8 months ago

Hi @PhllpSchrdr-boop. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

KarishmaGhiya commented 8 months ago

@PhllpSchrdr-boop The fix has been released to @azure/identity in both 4.0.1 and 3.4.2

github-actions[bot] commented 8 months ago

Hi @PhllpSchrdr-boop, since you havenโ€™t asked that we /unresolve the issue, weโ€™ll close this out. If you believe further discussion is needed, please add a comment /unresolve to reopen the issue.