Skip to content

Enhancing Token Refresh Control in MSAL: Introducing .WithAccessTokenSha256ToRefresh() in AcquireTokenForClient Flows #5179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 17, 2025

Conversation

gladjohn
Copy link
Contributor

@gladjohn gladjohn commented Mar 8, 2025

Fixes #5111

Specification - https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/main/docs/msiv1_token_revocation.md

Changes proposed in this request
This pull request includes several changes to enhance token acquisition and revocation capabilities in the MSAL library. The most important changes focus on adding support for token hash-based refresh, improving cache handling, and updating error handling.

Enhancements to Token Acquisition and Revocation:

  • Added a new method WithAccessTokenSha256ToRefresh to the AcquireTokenForClientParameterBuilder for configuring the SDK to bypass the cache for a specific token based on its SHA-256 hash.
  • Introduced a new property AccessTokenHashToRefresh in AcquireTokenForClientParameters to store the hash of the token to be refreshed.
  • Updated ClientCredentialRequest to handle token hash-based refresh logic, including checking the cache for matching tokens and bypassing the cache if necessary. [1] [2] [3]

Error Handling Improvements:

  • Added a new error ForceRefreshNotCompatibleWithTokenHash to indicate that force refresh and token hash cannot be used together. [1] [2] [3]

Internal Code Changes:

  • Changed the visibility of the Parameters property in AcquireTokenForClientParameterBuilder from private to internal.
  • Added validation in AcquireTokenForClientParameterBuilder to throw an exception if both force refresh and token hash are used together.

Documentation Updates:

  • Removed outdated documentation on MSI v1 token revocation and capabilities from docs/msiv1_token_revocation.md.

These changes collectively improve the flexibility and robustness of token handling in the MSAL library.

Testing
unit tests

Performance impact
none

Documentation

  • All relevant documentation is updated.

@gladjohn gladjohn requested a review from a team as a code owner March 8, 2025 23:31
Copy link
Contributor

@rayluo rayluo left a comment

Choose a reason for hiding this comment

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

Overall looks good. Do we plan to implement the managed identity part of functionality in a different PR? As this PR currently stands, it is not sufficient to "fix #5111", so, better remove the first line in the PR description and remove #5111 from "Successfully merging this pull request may close these issues."

@gladjohn
Copy link
Contributor Author

Overall looks good. Do we plan to implement the managed identity part of functionality in a different PR?

yes @rayluo, here is the MIA PR for Token Revocation, I had marked this as blocked since we had to finalize the design. I can work on this now - #5139

@bgavrilMS bgavrilMS requested a review from jmprieur March 11, 2025 18:27
@gladjohn gladjohn force-pushed the gladjohn/sha256_token branch from bb24951 to fe2a0a2 Compare March 12, 2025 12:00
@gladjohn
Copy link
Contributor Author

@rayluo @Robbie-Microsoft can I get a review from you as well please

@gladjohn gladjohn requested review from bgavrilMS and rayluo March 12, 2025 19:20
@AzureAD AzureAD deleted a comment from kennyoye88 Mar 12, 2025
Copy link
Contributor

@rayluo rayluo left a comment

Choose a reason for hiding this comment

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

Functionality wise, this PR shall work. Adding a couple of minor comments below, but overall LGTM.

Copy link
Member

@bgavrilMS bgavrilMS left a comment

Choose a reason for hiding this comment

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

Approved with comments. Let's discuss about telemetry separately.

@bgavrilMS bgavrilMS force-pushed the gladjohn/sha256_token branch from fcec87b to 6902b17 Compare March 14, 2025 12:06
GladwinJohnson and others added 8 commits March 16, 2025 14:28
…redentialRequest.cs

Co-authored-by: Bogdan Gavril <bogavril@microsoft.com>
…redentialRequest.cs

Co-authored-by: Neha Bhargava <61847233+neha-bhargava@users.noreply.github.com>
@gladjohn gladjohn force-pushed the gladjohn/sha256_token branch from 92b760e to f32c808 Compare March 16, 2025 21:30
@gladjohn
Copy link
Contributor Author

Approved with comments. Let's discuss about telemetry separately.

All comments addressed and removed experimental flags check from the tests, as we are GA'ing the API.

(!string.IsNullOrEmpty(AuthenticationRequestParameters.Claims) &&
string.IsNullOrEmpty(_clientParameters.AccessTokenHashToRefresh));

if (skipCache)
{
AuthenticationRequestParameters.RequestContext.ApiEvent.CacheInfo = CacheRefreshReason.ForceRefreshOrClaims;
logger.Info("[ClientCredentialRequest] Skipped looking for a cached access token because ForceRefresh or Claims were set.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
logger.Info("[ClientCredentialRequest] Skipped looking for a cached access token because ForceRefresh or Claims were set.");
logger.Info("[ClientCredentialRequest] Skipped looking for a cached access token because either of ForceRefresh, Claims or AccessTokenHashToRefresh were set.");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Committed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch. Actually, the new change was still inaccurate. The accurate statement shall be "skipped ... cache ... because either ForceRefresh was set, or Claims was set without AccessTokenHashToRefresh".

Copy link
Contributor

Choose a reason for hiding this comment

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

Being addressed in #5201

…redentialRequest.cs

Co-authored-by: Neha Bhargava <61847233+neha-bhargava@users.noreply.github.com>
@gladjohn gladjohn merged commit e090343 into main Mar 17, 2025
7 checks passed
@gladjohn gladjohn deleted the gladjohn/sha256_token branch March 17, 2025 20:33
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.

[Feature Request] .WithAccessTokenSha256ToRefresh(string hash)
6 participants