From 557130816284cfae86601274065b9f89db93cedb Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Mon, 3 Feb 2025 14:56:45 -0800 Subject: [PATCH] Force an update after acquiring a token interactively (#239539) * Force an update after acquiring a token interactively This will make sure the account cache is up-to-date before the acquireTokenInteractive ends. A greater fix is maybe turning the accounts cache to be a promise... bit this is the candidate fix for now. Fixes #235327 * also delete event --- .../src/node/authProvider.ts | 1 - .../src/node/cachedPublicClientApplication.ts | 27 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/extensions/microsoft-authentication/src/node/authProvider.ts b/extensions/microsoft-authentication/src/node/authProvider.ts index 0a352c8eb86..40000e08620 100644 --- a/extensions/microsoft-authentication/src/node/authProvider.ts +++ b/extensions/microsoft-authentication/src/node/authProvider.ts @@ -233,7 +233,6 @@ export class MsalAuthProvider implements AuthenticationProvider { const session = this.sessionFromAuthenticationResult(result, scopeData.originalScopes); this._telemetryReporter.sendLoginEvent(session.scopes); this._logger.info('[createSession]', `[${scopeData.scopeStr}]`, 'returned session'); - this._onDidChangeSessionsEmitter.fire({ added: [session], changed: [], removed: [] }); return session; } catch (e) { lastError = e; diff --git a/extensions/microsoft-authentication/src/node/cachedPublicClientApplication.ts b/extensions/microsoft-authentication/src/node/cachedPublicClientApplication.ts index a986b217983..8d081f1a825 100644 --- a/extensions/microsoft-authentication/src/node/cachedPublicClientApplication.ts +++ b/extensions/microsoft-authentication/src/node/cachedPublicClientApplication.ts @@ -149,22 +149,29 @@ export class CachedPublicClientApplication implements ICachedPublicClientApplica async acquireTokenInteractive(request: InteractiveRequest): Promise { this._logger.debug(`[acquireTokenInteractive] [${this._clientId}] [${this._authority}] [${request.scopes?.join(' ')}] loopbackClientOverride: ${request.loopbackClient ? 'true' : 'false'}`); - const result = await window.withProgress( + return await window.withProgress( { location: ProgressLocation.Notification, cancellable: true, title: l10n.t('Signing in to Microsoft...') }, - (_process, token) => this._sequencer.queue(() => raceCancellationAndTimeoutError( - this._pca.acquireTokenInteractive(request), - token, - 1000 * 60 * 5 - )) + (_process, token) => this._sequencer.queue(async () => { + const result = await raceCancellationAndTimeoutError( + this._pca.acquireTokenInteractive(request), + token, + 1000 * 60 * 5 + ); + if (this._isBrokerAvailable) { + await this._accountAccess.setAllowedAccess(result.account!, true); + } + // Force an update so that the account cache is updated. + // TODO:@TylerLeonhardt The problem is, we use the sequencer for + // change events but we _don't_ use it for the accounts cache. + // We should probably use it for the accounts cache as well. + await this._update(); + return result; + }) ); - if (this._isBrokerAvailable) { - await this._accountAccess.setAllowedAccess(result.account!, true); - } - return result; } /**