Skip to content

Adding token refresh hooks for VS Code account-based Entra MFA auth#21986

Open
Benjin wants to merge 22 commits into
mainfrom
dev/benjin/tokenRefreshHooks
Open

Adding token refresh hooks for VS Code account-based Entra MFA auth#21986
Benjin wants to merge 22 commits into
mainfrom
dev/benjin/tokenRefreshHooks

Conversation

@Benjin
Copy link
Copy Markdown
Contributor

@Benjin Benjin commented Apr 22, 2026

Description

Adding the MSSQL-side changes for token refresh requests when VS Code accounts are being used for Entra MFA auth

Paired with microsoft/sqltoolsservice#2665

Code Changes Checklist

  • New or updated unit tests added
  • All existing tests pass (npm run test)
  • Code follows contributing guidelines
  • Telemetry/logging updated if relevant
  • No regressions or UX breakage

Reviewers: Please read our reviewer guidelines

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 22, 2026

PR Changes

Category Target Branch PR Branch Difference
vscode-mssql VSIX 78068 KB 78586 KB ⚪ 518 KB ( 0% )
sql-database-projects VSIX 6310 KB 6310 KB ⚪ 0 KB ( 0% )
data-workspace VSIX 535 KB 535 KB ⚪ 0 KB ( 0% )
keymap VSIX 7 KB 7 KB ⚪ 0 KB ( 0% )

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 41.66667% with 329 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.67%. Comparing base (218a589) to head (fabf40a).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...ensions/mssql/src/controllers/connectionManager.ts 35.93% 271 Missing ⚠️
extensions/mssql/src/azure/vscodeEntraMfaUtils.ts 28.00% 18 Missing ⚠️
...nectionconfig/connectionDialogWebviewController.ts 15.38% 11 Missing ⚠️
extensions/mssql/src/constants/locConstants.ts 26.66% 11 Missing ⚠️
...ensions/mssql/src/languageservice/serviceclient.ts 12.50% 7 Missing ⚠️
...ensions/mssql/src/connectionconfig/azureHelpers.ts 70.00% 6 Missing ⚠️
...xtensions/mssql/src/models/contracts/connection.ts 89.47% 4 Missing ⚠️
.../mssql/src/deployment/fabricProvisioningHelpers.ts 0.00% 1 Missing ⚠️

❌ Your patch status has failed because the patch coverage (41.66%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #21986      +/-   ##
==========================================
- Coverage   74.83%   74.67%   -0.16%     
==========================================
  Files         394      394              
  Lines      120403   120786     +383     
  Branches     7197     7207      +10     
==========================================
+ Hits        90102    90196      +94     
- Misses      30301    30590     +289     
Flag Coverage Δ
data-workspace 77.10% <ø> (ø)
mssql 74.34% <41.66%> (-0.18%) ⬇️
sqlproj 77.33% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
extensions/mssql/src/configurations/config.ts 100.00% <100.00%> (ø)
...rc/controllers/addFirewallRuleWebviewController.ts 73.68% <100.00%> (ø)
...sql/src/controllers/sharedDisasterRecoveryUtils.ts 99.78% <100.00%> (-0.01%) ⬇️
extensions/mssql/src/models/contracts/azure.ts 100.00% <100.00%> (ø)
extensions/mssql/src/sharedInterfaces/telemetry.ts 100.00% <100.00%> (ø)
extensions/mssql/test/unit/azureHelperStubs.ts 100.00% <100.00%> (ø)
.../mssql/src/deployment/fabricProvisioningHelpers.ts 48.51% <0.00%> (ø)
...xtensions/mssql/src/models/contracts/connection.ts 93.21% <89.47%> (-0.48%) ⬇️
...ensions/mssql/src/connectionconfig/azureHelpers.ts 71.55% <70.00%> (-1.41%) ⬇️
...ensions/mssql/src/languageservice/serviceclient.ts 66.22% <12.50%> (-0.44%) ⬇️
... and 4 more

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Benjin Benjin marked this pull request as ready for review May 6, 2026 23:57
Copilot AI review requested due to automatic review settings May 6, 2026 23:57
Comment thread extensions/mssql/src/configurations/config.ts Outdated
Comment thread extensions/mssql/src/controllers/connectionManager.ts
Comment thread extensions/mssql/src/controllers/connectionManager.ts
Comment thread extensions/mssql/src/controllers/connectionManager.ts
Comment thread extensions/mssql/test/unit/connectionManager.test.ts Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds extension-side support for SQLToolsService (STS) to request/refresh Entra MFA access tokens when using VS Code account-based auth, aligning with the paired STS PR.

Changes:

  • Introduces new STS notifications for token refresh requests/responses and wires a handler in ConnectionManager.
  • Updates STS launch arguments and security token request handling to support client-driven token acquisition (VS Code accounts path) plus new telemetry action.
  • Bumps bundled STS version and changes the preview setting default for VS Code accounts Entra MFA.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
extensions/mssql/test/unit/connectionManager.test.ts Skips an existing VS Code accounts token test (no replacement added).
extensions/mssql/src/sharedInterfaces/telemetry.ts Adds a new telemetry action for VS Code account token acquisition failures.
extensions/mssql/src/models/contracts/connection.ts Adds new account/refreshToken and account/tokenRefreshed notification contracts.
extensions/mssql/src/models/contracts/azure.ts Extends security token request/response contracts with optional account/tenant and expiry.
extensions/mssql/src/languageservice/serviceclient.ts Switches STS launch flags based on the VS Code accounts preview feature.
extensions/mssql/src/controllers/connectionManager.ts Registers/implements refresh-token notification handling and adds VS Code accounts branch for security token requests.
extensions/mssql/src/configurations/config.ts Updates the SQLToolsService version to a newer build.
extensions/mssql/package.json Changes mssql.preview.useVscodeAccountsForEntraMFA default from null to true.

Comment on lines +661 to +669
const resource =
params.resource ??
getCloudProviderSettings(account.key.providerId).settings.sqlResource!;

const refreshedToken = await self.azureController.refreshAccessToken(
account,
self.accountStore,
params.tenantId,
resource as any, // TODO: fix type mismatch
Comment thread extensions/mssql/src/models/contracts/connection.ts Outdated
Comment thread extensions/mssql/package.json
Comment thread extensions/mssql/test/unit/connectionManager.test.ts Outdated
Comment thread extensions/mssql/src/controllers/connectionManager.ts
Comment on lines +2245 to +2272
if (params.accountId) {
this._logger.verbose("VS Code accounts token request received");
try {
const tokenInfo = await acquireSqlAccessTokenFromVscodeAccount(
params.accountId,
params.tenantId,
);
this._logger.verbose("VS Code accounts token acquired successfully");
return {
accountKey: params.accountId,
token: tokenInfo.token.token,
expiresOn: tokenInfo.token.expiresOn,
};
} catch (error) {
this._logger.error(
`VS Code accounts token acquisition failed: ${getErrorMessage(error)}`,
);
sendErrorEvent(
TelemetryViews.ConnectionManager,
TelemetryActions.AcquireVsCodeAccountToken,
error instanceof Error ? error : new Error(getErrorMessage(error)),
/* includeErrorMessage */ false,
/* errorCode */ undefined,
/* errorType */ undefined,
{},
);
return { accountKey: "", token: "", expiresOn: 0 };
}
aasimkhan30
aasimkhan30 previously approved these changes May 8, 2026
Copilot AI review requested due to automatic review settings May 12, 2026 03:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 12 comments.

Comment on lines +664 to +672
const resource =
params.resource ??
getCloudProviderSettings(account.key.providerId).settings.sqlResource!;

const refreshedToken = await self.azureController.refreshAccessToken(
account,
self.accountStore,
params.tenantId,
resource as any, // TODO: fix type mismatch
Comment on lines +638 to +646
if (
previewService.isFeatureEnabled(PreviewFeature.UseVscodeAccountsForEntraMFA)
) {
const tokenInfo = await acquireTokenFromVscodeAccountForResource(
getCloudResourceEndpoint("sqlResource"),
params.accountId,
params.tenantId,
);
token = tokenInfo.token.token;
Comment thread extensions/mssql/src/controllers/connectionManager.ts Outdated
// Return null to signal "sign in" was selected (account is undefined on that item)
resolve(account ?? null); // eslint-disable-line no-restricted-syntax
// eslint-disable-next-line no-restricted-syntax
resolve(selectedItem.value ?? null);
Comment thread extensions/mssql/src/controllers/connectionManager.ts Outdated
Comment thread extensions/mssql/src/azure/vscodeEntraMfaUtils.ts
Comment thread extensions/mssql/test/unit/connectionManager.test.ts Outdated
Comment thread extensions/mssql/src/connectionconfig/azureHelpers.ts Outdated
Comment thread extensions/mssql/src/controllers/connectionManager.ts Outdated
Comment thread extensions/mssql/src/connectionconfig/connectionDialogWebviewController.ts Outdated
Copilot AI review requested due to automatic review settings May 12, 2026 05:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 13 comments.

Comment thread extensions/mssql/src/models/contracts/connection.ts Outdated
Comment on lines +641 to +642
const tokenInfo = await acquireTokenFromVscodeAccountForResource(
getCloudResourceEndpoint("sqlResource"),
Comment on lines +664 to +672
const resource =
params.resource ??
getCloudProviderSettings(account.key.providerId).settings.sqlResource!;

const refreshedToken = await self.azureController.refreshAccessToken(
account,
self.accountStore,
params.tenantId,
resource as any, // TODO: fix type mismatch
Comment thread extensions/mssql/src/controllers/connectionManager.ts Outdated
Comment thread extensions/mssql/src/controllers/connectionManager.ts Outdated
Comment on lines +159 to +169
export function getCloudResourceEndpoint(endpoint: keyof IProviderResources): string {
const cloudSettings = getCloudProviderSettings();
const resource = cloudSettings.settings[endpoint];

if (!resource) {
throw new Error(
locConstants.Azure.noResourceConfiguredForCurrentCloud(
endpoint,
cloudSettings.displayName,
),
);
Comment thread extensions/mssql/src/connectionconfig/azureHelpers.ts
Comment thread extensions/mssql/src/connectionconfig/azureHelpers.ts Outdated
Comment thread extensions/mssql/test/unit/connectionManager.test.ts
Comment thread extensions/mssql/test/unit/connectionManager.test.ts
Copilot AI review requested due to automatic review settings May 12, 2026 23:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 5 comments.

Comment on lines +698 to +706
const resource =
params.resource ??
getCloudProviderSettings(account.key.providerId).settings.sqlResource!;

const refreshedToken = await self.azureController.refreshAccessToken(
account,
self.accountStore,
params.tenantId,
resource as any, // TODO: fix type mismatch
Comment on lines +651 to +672
if (!params.accountId) {
self._logger?.verbose(
`Cannot refresh token: no accountId provided in refresh request for URI ${params.uri}`,
);
sendErrorEvent(
TelemetryViews.ConnectionManager,
TelemetryActions.RefreshTokenNotification,
new Error("Missing accountId in refresh token notification"),
true, // includeErrorMessage
"missingAccountId",
undefined,
{
useVscodeAccountsForEntraMFA: String(
useVscodeAccountsForEntraMFA,
),
},
{
currentTimestamp: Math.floor(Date.now() / 1000),
},
);
return;
}
Comment on lines +768 to +781
currentTimestamp: Math.floor(Date.now() / 1000),
refreshedTokenExpirationTimestamp:
expiresOn !== undefined ? expiresOn : -1,
},
);

self.client.sendNotification(
ConnectionContracts.TokenRefreshedNotification.type,
{
token: token,
expiresOn: expiresOn ?? -1,
uri: params.uri,
},
);
Comment on lines +2513 to +2515
throw new Error(
`Failed to acquire token for account ${accountId} and tenant ${tenantId} (undefined result)`,
);
Comment on lines +625 to +650
/**
* Handles the account/refreshToken notification from the service.
* Acquires a fresh token using VS Code accounts or MSAL, then sends
* account/tokenRefreshed back to the service.
*/
public handleRefreshTokenNotification(): NotificationHandler<ConnectionContracts.RefreshTokenParams> {
const self = this;
return (params: ConnectionContracts.RefreshTokenParams): void => {
void (async () => {
const useVscodeAccountsForEntraMFA = previewService.isFeatureEnabled(
PreviewFeature.UseVscodeAccountsForEntraMFA,
);

try {
let token: string | undefined;
let expiresOn: number | undefined;

if (useVscodeAccountsForEntraMFA) {
const tokenInfo = await acquireTokenFromVscodeAccountForResource(
getCloudResourceEndpoint("sqlResource"),
params.accountId,
params.tenantId,
);
token = tokenInfo.token.token;
expiresOn = tokenInfo.token.expiresOn;
} else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants