Skip to content

fix: auto-clear MSAL cache on wrong-tenant token (WAM, issue #430)#444

Merged
sellakumaran merged 2 commits into
mainfrom
users/sellak/fix-wam-wrong-account
Jun 5, 2026
Merged

fix: auto-clear MSAL cache on wrong-tenant token (WAM, issue #430)#444
sellakumaran merged 2 commits into
mainfrom
users/sellak/fix-wam-wrong-account

Conversation

@sellakumaran
Copy link
Copy Markdown
Contributor

Issue: On Windows, WAM silently selects a cached work account from a different tenant when multiple accounts are present. The CLI acquires a token for the wrong tenant and uses it for Graph and Agent 365 API calls, returning 403s that look like missing Entra roles.

Fix: After token acquisition, the CLI decodes the tid claim and compares it to the configured tenant. On mismatch: logs a warning, deletes both token caches (auth-token.json + msal-token-cache), and retries once. If the retry also returns the wrong tenant, throws with an actionable error. No new commands — fully transparent to the user.

Closes #430

Copilot AI review requested due to automatic review settings June 4, 2026 23:08
@sellakumaran sellakumaran requested review from a team as code owners June 4, 2026 23:08
@github-actions github-actions Bot added bug Something isn't working documentation Improvements or additions to documentation labels Jun 4, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 4, 2026

⚠️ Deprecation Warning: The deny-licenses option is deprecated for possible removal in the next major release. For more information, see issue 997.

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

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

Fixes a Windows WAM/MSAL failure mode where the CLI can silently acquire a Microsoft Graph token for the wrong Entra tenant when multiple cached work accounts exist, by validating the JWT tid claim and attempting an automatic cache-clear + single retry to self-heal (#430).

Changes:

  • Added shared JwtHelper.TryDecodeClaim and refactored existing JWT-claim decoding call sites to use it.
  • Implemented wrong-tenant (tid) detection with cache clearing and retry in both AuthenticationService and MicrosoftGraphTokenProvider.
  • Added/updated unit tests plus a changelog entry describing the new behavior.

Reviewed changes

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

Show a summary per file
File Description
src/Tests/Microsoft.Agents.A365.DevTools.Cli.Tests/Services/MicrosoftGraphTokenProviderTests.cs Adds tests for wrong-tenant self-heal behavior in the Graph token provider.
src/Tests/Microsoft.Agents.A365.DevTools.Cli.Tests/Services/AuthenticationServiceTests.cs Adds tests for wrong-tenant self-heal behavior in AuthenticationService.
src/Microsoft.Agents.A365.DevTools.Cli/Services/MsalBrowserCredential.cs Uses a shared constant for the MSAL cache filename.
src/Microsoft.Agents.A365.DevTools.Cli/Services/Internal/MicrosoftGraphTokenProvider.cs Adds tid validation + cache clear + retry to mitigate wrong-tenant WAM account selection.
src/Microsoft.Agents.A365.DevTools.Cli/Services/Helpers/JwtHelper.cs Introduces centralized Base64Url JWT claim decoding helper.
src/Microsoft.Agents.A365.DevTools.Cli/Services/GraphApiService.cs Refactors token-claim decoding to use JwtHelper.
src/Microsoft.Agents.A365.DevTools.Cli/Services/AuthenticationService.cs Adds tid validation + cache clear + retry; adjusts cached tenant handling.
src/Microsoft.Agents.A365.DevTools.Cli/Constants/AuthenticationConstants.cs Introduces MsalCacheFileName constant.
CHANGELOG.md Documents the fix under “Fixed” for the upcoming release.

Comment thread src/Microsoft.Agents.A365.DevTools.Cli/Services/AuthenticationService.cs Outdated
Comment thread src/Microsoft.Agents.A365.DevTools.Cli/Services/AuthenticationService.cs Outdated
- Gate tid mismatch check on Guid.TryParse in both AuthenticationService and
  MicrosoftGraphTokenProvider; domain-form tenantIds (e.g. contoso.onmicrosoft.com)
  would previously always mismatch the JWT tid GUID, causing unnecessary cache clears
- Fix TokenInfo.TenantId to store domain-form tenantId as-is rather than overwriting
  it with the decoded JWT tid GUID, preserving round-trip cache consistency
- Throw InvalidOperationException (not just warn) in MicrosoftGraphTokenProvider when
  the retry also returns the wrong tenant, preventing a known-bad token from being
  cached and used
- Add MSAL cache backup/restore in AuthenticationServiceTests and
  MicrosoftGraphTokenProviderTests so tests do not destroy the developer's real
  credential cache on machines where a real cache exists
@sellakumaran sellakumaran enabled auto-merge (squash) June 5, 2026 15:32
@sellakumaran sellakumaran merged commit 9e7d8fd into main Jun 5, 2026
9 checks passed
@sellakumaran sellakumaran deleted the users/sellak/fix-wam-wrong-account branch June 5, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

a365 setup all uses wrong cached WAM account, causing misleading Graph permission failures

4 participants