From 49619e5b39fb60ee139870f5fc7b5de90f8288c8 Mon Sep 17 00:00:00 2001 From: Charles Gagnon Date: Tue, 2 Jul 2019 21:01:53 +0000 Subject: [PATCH] Clear Azure token if connection doesn't need it (#6244) * Clear Azure token if connection doesn't need it * Update function name --- .../common/connectionManagementService.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/sql/platform/connection/common/connectionManagementService.ts b/src/sql/platform/connection/common/connectionManagementService.ts index d4f6814679..b25c4100a5 100644 --- a/src/sql/platform/connection/common/connectionManagementService.ts +++ b/src/sql/platform/connection/common/connectionManagementService.ts @@ -237,7 +237,7 @@ export class ConnectionManagementService extends Disposable implements IConnecti * @param connectionProfile Connection Profile */ public async addSavedPassword(connectionProfile: IConnectionProfile): Promise { - await this.fillInAzureTokenIfNeeded(connectionProfile); + await this.fillInOrClearAzureToken(connectionProfile); return this._connectionStore.addSavedPassword(connectionProfile).then(result => result.profile); } @@ -277,7 +277,7 @@ export class ConnectionManagementService extends Disposable implements IConnecti } // Fill in the Azure account token if needed and open the connection dialog if it fails - let tokenFillSuccess = await self.fillInAzureTokenIfNeeded(newConnection); + let tokenFillSuccess = await self.fillInOrClearAzureToken(newConnection); // If the password is required and still not loaded show the dialog if ((!foundPassword && self._connectionStore.isPasswordRequired(newConnection) && !newConnection.password) || !tokenFillSuccess) { @@ -446,7 +446,7 @@ export class ConnectionManagementService extends Disposable implements IConnecti if (callbacks.onConnectStart) { callbacks.onConnectStart(); } - let tokenFillSuccess = await this.fillInAzureTokenIfNeeded(connection); + let tokenFillSuccess = await this.fillInOrClearAzureToken(connection); if (!tokenFillSuccess) { throw new Error(nls.localize('connection.noAzureAccount', 'Failed to get Azure account token for connection')); } @@ -778,8 +778,17 @@ export class ConnectionManagementService extends Disposable implements IConnecti return defaultProvider && this._providers.has(defaultProvider) ? defaultProvider : undefined; } - private async fillInAzureTokenIfNeeded(connection: IConnectionProfile): Promise { - if (connection.authenticationType !== Constants.azureMFA || connection.options['azureAccountToken']) { + /** + * Fills in the Azure account token if it's needed for this connection and doesn't already have one + * and clears it if it isn't. + * @param connection The connection to fill in or update + */ + private async fillInOrClearAzureToken(connection: IConnectionProfile): Promise { + if (connection.authenticationType !== Constants.azureMFA) { + connection.options['azureAccountToken'] = undefined; + return true; + } + if (connection.options['azureAccountToken']) { return true; } let accounts = await this._accountManagementService.getAccountsForProvider('azurePublicCloud');