Skip to content

Renew marketplace resource tokens before expiry - #343688

Open
Michael Cummings (MSFT) (mcumming) wants to merge 11 commits into
microsoft:mainfrom
mcumming:dev/mcumming/marketplace-auth-follow-ups
Open

Michael Cummings (MSFT) (mcumming) wants to merge 11 commits into
microsoft:mainfrom
mcumming:dev/mcumming/marketplace-auth-follow-ups

Conversation

@mcumming

@mcumming Michael Cummings (MSFT) (mcumming) commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Related to microsoft/vscode#325412.

Summary

Renew Microsoft Entra marketplace resource credentials through one coalesced account-owned path, including recovery after OS resume. Handle trusted marketplace bearer-rejection challenges without a private workbench/provider refresh contract.

Renewal and recovery

  • Use the authentication provider's expiresAfter lifetime rather than parsing the resource token. Schedule resolution at 75% of the reported lifetime, bounded between one second and six hours; tokens without usable lifetime metadata are rechecked every five minutes.
  • Microsoft authentication owns near-expiry cache refresh inside the extension. The workbench does not send a private force-refresh option. Neither a cache bypass for rejected but otherwise unexpired credentials nor a public forceRefresh API is planned.
  • Keep the scheduler disposable and coalesce concurrent account resolutions. Retry transient Unknown results after 30 seconds only when this provider/marketplace has an active renewal hint; providers without one are not newly polled.
  • Preserve the current catalog while renewal is pending. Sign-out, account/provider/marketplace changes, and disposal supersede stale work. On transient service-index failures, keep an already accepted catalog only for the same account and marketplace with a replacement credential that has not been rejected. The next scheduled renewal or 401 recovery fetches the index again; this is not a dedicated index-backoff loop.
  • Check the absolute renewal deadline before token use and route OS resume/current-bearer 401s through the same resolution path. Shared/remote rejection feedback is routed to the last snapshot owner. Final-URL and retained-Authorization proof prevent foreign, stripped out-and-back, anonymous, or unproven responses from driving recovery.

WWW-Authenticate contract

The VSPM server contract is planned separately and is not yet deployed. This PR implements the client side; it does not claim live server interoperability.

Per VSPM, Continuous Access Evaluation (CAE) and claims challenges are non-goals. Revoked-token recovery relies on the server's planned invalid_token response and near-expiry renewal of short-lived non-CAE tokens. If silent resolution returns the same rejected token, the client requires sign-in rather than forcing a cache refresh.

  • Forward WWW-Authenticate on both service-index 401s and gallery 401 feedback from the shared process or remote server, only after proving the response is same-origin and the final request carried the bearer.
  • Interpret a single well-formed Bearer challenge. Malformed, duplicate-parameter, multiple-challenge, and non-Bearer headers do not supply authentication instructions; they retain legacy recovery behavior. Header names and the Bearer scheme are case-insensitive, and repeated header fields are preserved for validation.
  • With no error parameter, preserve today's acquisition flow: silent resolution and the existing interactive sign-in path when needed.
  • With error="invalid_token", silently re-resolve once without bypassing the provider cache. If the provider returns the same rejected token, withdraw the catalog and replicated credential and transition to RequiresSignIn. Renewal events cannot reinstall that rejected credential for the same account and marketplace.
  • Replay only explicitly safe read-only operations, at most once and only with a different token. A replayed 401 reports rejection without permitting another silent resolution. If that replayed 401 carries error="invalid_token", the client transitions to RequiresSignIn; without it, today's behavior is kept (the current catalog and credential remain until normal renewal). Statistics writes are not replayed.
  • A 403, including insufficient_scope, surfaces the access error without another token retry.
  • insufficient_claims is a defensive sign-in fallback only. It does not process a claims challenge or perform automatic CAE recovery; VSPM does not plan to emit this challenge.

Process and UI updates

Publish the accepted manifest/token/service-URL snapshot directly to the shared process and remote server, independently of onDidChangeExtensionGalleryManifest. Compare catalog contents structurally, and emit the public manifest event only for actual content/availability changes. Receivers install token-only updates without emitting redundant manifest/status notifications.

Sign-out and access retraction clear replicated credentials and retain their actual availability/status notifications. Token-only renewal does not repeat the Extensions search or automatic update check.

Extension-resource consumers apply genuine catalog changes and retraction rather than retaining their bootstrap resource template.

Validation

The pnpm scripts below were executed through Node 24/Corepack in the isolated Windows worktree.

Command Result
pnpm run transpile-client Passed.
pnpm run typecheck-client Passed.
$env:VSCODE_SKIP_PRELAUNCH='1'; .\scripts\test.bat --run src\vs\workbench\services\extensionManagement\test\electron-browser\extensionGalleryManifestService.test.ts --run src\vs\workbench\services\extensionManagement\test\electron-browser\extensionGalleryAccountService.test.ts --run src\vs\platform\extensionManagement\test\common\extensionGalleryAuthorization.test.ts --reporter dot Passed: 294 tests.
pnpm run eslint src\vs\platform\extensionManagement\common\extensionGalleryManifest.ts src\vs\platform\extensionManagement\common\extensionGalleryManifestService.ts src\vs\platform\extensionManagement\common\extensionGalleryManifestServiceIpc.ts src\vs\platform\extensionManagement\test\common\extensionGalleryAuthorization.test.ts src\vs\workbench\services\extensionManagement\electron-browser\extensionGalleryManifestService.ts src\vs\workbench\services\extensionManagement\test\electron-browser\extensionGalleryManifestService.test.ts Passed: all six changed TypeScript files.

Focused regressions cover cached/replacement/rejected replacement credentials, the single-resolution/replay budget, rejection cooldown, account changes, transient index failure, legacy/no-error behavior, malformed/ambiguous challenges, IPC propagation, initial and replay transport proof, 403 handling, and the defensive sign-in fallback for unsupported claims challenges. Gallery recovery tests exercise the request helper through IPC feedback, the manifest/account services, and the resource-session provider fixture with unexpired cached tokens.

Live VSPM responses, Entra/broker behavior, actual OS sleep/resume, remote deployment flows, and full integration/smoke suites were not exercised. This update changes only this PR's head branch; #325815 is unchanged.

Drafted with GitHub Copilot.

Copilot AI balanced review requested due to automatic review settings October 23, 2026 03:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Transient resource-token lookup failures can retract working marketplace access and stop automatic renewal retries.

1 open issue
What changed in this PR

This PR adds proactive renewal of Microsoft Entra marketplace resource tokens, extending the existing authentication flow so long-lived windows can retain marketplace access.

Changes:

  • Schedule renewal using provider-reported token lifetimes, with fallback checks, request coalescing, and stale-result rejection.
  • Publish authorization changes to shared and remote processes even when the manifest is unchanged.
  • Add deterministic tests for renewal timing, transient recovery, cancellation, and concurrent resolution.
File Description
src/​vs/​workbench/​services/​extensionManagement/​test/​electron-browser/​extensionGalleryManifestService.test.ts Adds renewal and lifecycle regression tests.
src/​vs/​workbench/​services/​extensionManagement/​electron-browser/​extensionGalleryManifestService.ts Emits updates when authorization changes independently of the manifest.
src/​vs/​workbench/​services/​extensionManagement/​electron-browser/​extensionGalleryAccountService.ts Adds renewal scheduling, request coalescing, and stale-result guards.
src/​vs/​workbench/​services/​extensionManagement/​common/​extensionGalleryAccount.ts Adds optional account refresh timing metadata.

Review effort: Balanced

@mariaghiondea

Copy link
Copy Markdown

Karthik Nadig (@karthiknadig) this is ready for your review. Thank you.

@mcumming
Michael Cummings (MSFT) (mcumming) force-pushed the dev/mcumming/marketplace-auth-follow-ups branch from 6169719 to 8922cf6 Compare October 26, 2026 19:00
@mariaghiondea

Copy link
Copy Markdown

Karthik Nadig (@karthiknadig) this is ready for your review 🙏

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks. Renewing before expiry is the right fix, and keeping the manifest during a transient failure is good. Requesting changes for two behavior issues (details inline):

  1. Every renewal fires onDidChangeExtensionGalleryManifest, which refreshes the Extensions view and runs a full update check.
  2. Renewal relies only on a timer, which doesn't reliably catch up after sleep, and nothing reacts to a 401 from gallery requests.

This also conflicts with #343687 and #343689 in extensionGalleryAccountService.ts (and with #343686 in the test file), and this branch still contains the sign-in-token fallback that #343689 removes. Please rebase after those land.


Review assisted by Copilot.

@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

TylerLeonhardt

Matched files:

  • extensions/microsoft-authentication/src/node/authProvider.ts
  • extensions/microsoft-authentication/src/node/cachedPublicClientApplication.ts
  • src/vs/workbench/services/authentication/common/authentication.ts

Robo (@deepak1556)

Matched files:

  • src/vs/code/electron-utility/sharedProcess/sharedProcessMain.ts

@mcumming
Michael Cummings (MSFT) (mcumming) force-pushed the dev/mcumming/marketplace-auth-follow-ups branch from 01db914 to f597f66 Compare November 2, 2026 19:56
@mariaghiondea

Copy link
Copy Markdown

Karthik Nadig (@karthiknadig) ready for your review again

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this addresses both items from my last review: token-only renewals no longer fire onDidChangeExtensionGalleryManifest, and renewal now also runs on OS resume, on the absolute due time, and after a 401.

Requesting changes for three things:

  1. The new authorization channel lets the shared process or a remote server force unlimited token refreshes (inline).
  2. Owner review. This now changes the microsoft-authentication extension and adds the workbench-only _workbenchForceRefresh session option (TylerLeonhardt). It also registers a reverse channel from sharedProcessMain.ts and serverServices.ts and adds url/requestHasAuthorization to IRequestContext and request IPC (Robo (@deepak1556), Christof Marti (@chrmarti)). Those owners should sign off.
  3. Rebase. doGetAccount still falls back to the sign-in token in both places #343689 removes, and the branch conflicts with #343686, #343687, #343689 and #343690. Please rebase after those land so the 401-recovery path is reviewed against the final code.

Review assisted by Copilot.

Use the authentication provider's remaining lifetime to schedule silent resource-token renewal. Coalesce account resolutions, cancel stale generations, retain available manifests during transient failures, and publish rotated process snapshots.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 859d436-9028-4925-a02c-73264226d5f8
…lures

Distinguish failed renewal of the same account and gallery from initial consent and account switches. Exercise a resource-session failure rather than an identity-session proxy, and ensure a different account cannot keep the previous bearer.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 859d436-9028-4925-a02c-73264226d5f8
Avoid introducing polling or a second renewal cadence for GitHub and other account providers. Clear retry ownership when the provider or marketplace changes, and exercise both initial failures and provider replacement.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 859d436-9028-4925-a02c-73264226d5f8
Separate marketplace credential projection from catalog notifications and recover current-bearer failures through one coalesced silent renewal path. Preserve account/cancellation guards and safe request replay.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 859d436-9028-4925-a02c-73264226d5f8
Share a one-minute completion cooldown across rejection reporters and rotating bearers. Avoid forcing the same rejected current credential twice, retain a server-accepted catalog when renewal returns an unchanged bearer, and preserve genuine authentication failure handling.

Cover remote/shared rejection loops, exact cooldown boundaries, slow and failed refreshes, normal expiry renewal, and account/session-change races.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 859d436-9028-4925-a02c-73264226d5f8
Retain the merge-only regression coverage from df60f49 in the rebased history: same-origin, cross-origin and out-and-back authorization proof, renewable-account 403 handling without forced renewal, and metadata-cache reuse during expiry renewal.

Co-authored-by: Copilot App <[email protected]>
@mcumming
Michael Cummings (MSFT) (mcumming) force-pushed the dev/mcumming/marketplace-auth-follow-ups branch from df60f49 to d6fe40a Compare November 10, 2026 15:19
@mcumming

Copy link
Copy Markdown
Contributor Author

Karthik Nadig (@karthiknadig) ready for your review again

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, the rebase and throttling look good: the sign-in token fallback is gone, a rejected token can trigger at most one renewal per minute (and only for the current or previous token), and token-only renewals no longer fire manifest changes in the workbench, shared process, or remote.

Requesting changes for:

  1. A transient service-index failure during renewal drops a working session to AccessDenied (inline).
  2. Owner review is still missing: TylerLeonhardt for the microsoft-authentication changes and _workbenchForceRefresh, and Robo (@deepak1556) / Christof Marti (@chrmarti) for the shared-process and server authorization channel and the IRequestContext/requestIpc fields.

Review assisted by Copilot.

…lures during renewal

A token renewal re-fetches the service index. A network error, missing
status, 408, 429 or 5xx now raises MarketplaceIndexTransientError. When the
account and marketplace URL are unchanged and the renewed token has not been
rejected or superseded, the accepted manifest is kept and the renewed token is
republished. Account switches and definitive errors still retract access. The
next scheduled renewal, or 401 recovery, re-fetches the index.

Co-authored-by: Copilot App <[email protected]>
…orkbench sentinel

Remove the private _workbenchForceRefresh / WORKBENCH_FORCE_REFRESH option. The microsoft-authentication extension now forces a silent refresh when a cached access token is within five minutes of expiry, keeping the cached unexpired token if that refresh fails. The marketplace account provider no longer passes a sentinel.

Co-authored-by: Copilot App <[email protected]>
Forward trusted WWW-Authenticate feedback through service-index and gallery authorization paths. Bound recovery to one silent resolution and one safe replay, retract rejected cached credentials, and retain legacy no-error behavior.

Co-authored-by: Copilot App <[email protected]>
@mariaghiondea

Copy link
Copy Markdown

Karthik Nadig (@karthiknadig) this is ready for your review. Thank you

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, the transient service-index fix looks right: a transient failure during renewal keeps the accepted catalog only for the same account and marketplace with a fresh token, first loads and account switches still fail as before, and the tests cover both. Removing _workbenchForceRefresh addresses Tyler's concern about the private contract.

Still requesting changes for:

  1. Owner sign-off on the new microsoft-authentication behavior (inline), TylerLeonhardt.
  2. The hand-written WWW-Authenticate parser (inline).
  3. Owner review of the shared-process and server authorization channel and the IRequestContext/requestIpc fields, Robo (@deepak1556) / Christof Marti (@chrmarti).

Review assisted by Copilot.

// MSAL renews cached access tokens inside its renewal window, but the native broker can still
// hand back a cached token that is about to expire. Refresh it here so a caller asking again
// near the advertised `expiresAfter` receives a fresh token without any extra request option.
if (result.fromCache && isAccessTokenNearExpiry(result.expiresOn)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This applies to every acquireTokenSilent caller of the Microsoft provider, not just the marketplace: with the native broker, any cached token within five minutes of expiry now triggers a forced claims refresh. Without the broker it's effectively a no-op, since MSAL already renews inside the same five-minute window. TylerLeonhardt, is this OK for all consumers? If the broker gap is real, it may be better fixed in its own auth-extension PR.

}

/** Interpret a single Bearer challenge; malformed, non-Bearer and multiple challenges retain legacy behavior. */
export function getExtensionGalleryAuthenticationError(wwwAuthenticate: string | undefined): string | undefined {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This re-implements challenge parsing before calling parseWWWAuthenticateHeader. Could it use parseWWWAuthenticateHeader directly and read error from the Bearer challenge? For claims challenges, IAuthenticationService.getSessions already accepts { wwwAuthenticate, fallbackScopes }, and the Microsoft provider implements getSessionsFromChallenges. Since this handling isn't needed for renewing before expiry, it could also move to a follow-up to keep this PR focused.

This branch has not been deployed

No deployments
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.

5 participants