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

MSAL - update module for to work with msal-browser 3 #1228

Open
odinr opened this issue Sep 1, 2023 · 2 comments
Open

MSAL - update module for to work with msal-browser 3 #1228

odinr opened this issue Sep 1, 2023 · 2 comments
Assignees

Comments

@odinr
Copy link
Collaborator

odinr commented Sep 1, 2023

Microsoft has rewritten the interface for MSAL 🥺

see changes for msal-browser@3, more than 2k files changed 😓

the "main" change is that after creating a PublicClientApplication, the instance need to await initialization

Proposed changes:

proposed change will not affect any end-users (consumer of the package)

change logic from JiT to initialize clients in the module.initialize

initialize: async ({ config }) => {
const authProvider = new AuthProvider(config);
if (config.requiresAuth) {
await authProvider.handleRedirect();
if (!authProvider.defaultAccount) {
await authProvider.login();
}
}
return authProvider;
},

--- a/packages/modules/msal/src/module.ts
+++ b/packages/modules/msal/src/modulets
@@ -19,9 +19,10 @@
initialize: async ({ config }) => {
+  const clients = await createAuthClients(config); // add function for resolving all provided clients 
+  const authProvider = new AuthProvider({clients});
-  const authProvider = new AuthProvider(config); 
    if (config.requiresAuth) {
        await authProvider.handleRedirect();
        if (!authProvider.defaultAccount) {
            await authProvider.login();
        }
    }
    return authProvider;
},

cause of error:

getClient(name: string): AuthClient {
if (!this._clients[name]) {
this._clients[name] = this.createClient(name);
}
return this._clients[name];
}
createClient(name?: string): AuthClient {
const config = name ? this._config.getClientConfig(name) : this._config.defaultConfig;
if (!config) {
throw Error('Could not find any config');
}
const client = createAuthClient(
config.tenantId,
config.clientId,
config.redirectUri,
config.config,
);
// TODO - fix with log streamer
client.setLogger(new ConsoleLogger(0));
return client;
}

other:

PublicClientApplication.logger = PublicClientApplication.getLogger()
// TODO check if there is a better way to figure out the `request.origin`
PublicClientApplication.browserStorage = PublicClientApplication.getBrowserStorage();

see #1167

@odinr
Copy link
Collaborator Author

odinr commented Sep 1, 2023

we might be able to remove some code, since the package now provide:

  • getAccountByHomeId
  • getAccountByLocalId
  • getAccountByUsername
  • getActiveAccount

this was "hacked" together with:

get account(): AccountInfo | undefined {
const accounts = this.getAllAccounts();
const account = accounts.find(
(a) => (a as AccountInfo).idTokenClaims?.aud === this.clientId,
);
return account as AccountInfo;
}

@odinr
Copy link
Collaborator Author

odinr commented Sep 1, 2023

check if there is a feature to replace:

get hasValidClaims(): boolean {
const idTokenClaims = this.account?.idTokenClaims;
if (idTokenClaims) {
const epoch = Math.ceil(Date.now() / 1000);
return idTokenClaims.exp > epoch;
}
return false;
}

@odinr odinr self-assigned this Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant