Skip to content

Commit

Permalink
Seperate claims and forced refresh reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
Avery-Dunn committed Jan 29, 2025
1 parent e312512 commit 94da54f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ private boolean shouldRefresh(SilentParameters parameters, AuthenticationResult

//If forceRefresh is true, no reason to check any other option
if (parameters.forceRefresh()) {
setCacheTelemetry(CacheRefreshReason.FORCE_REFRESH_OR_CLAIMS);
setCacheTelemetry(CacheRefreshReason.FORCE_REFRESH);
log.debug("Refreshing access token because forceRefresh parameter is true.");
return true;
}

//If the request contains claims then the token should be refreshed, to ensure that the returned token has the correct claims
// Note: these are the types of claims found in (for example) a claims challenge, and do not include client capabilities
if (parameters.claims() != null) {
setCacheTelemetry(CacheRefreshReason.FORCE_REFRESH_OR_CLAIMS);
setCacheTelemetry(CacheRefreshReason.CLAIMS);
log.debug("Refreshing access token because the claims parameter is not null.");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ public enum CacheRefreshReason {
*/
NOT_APPLICABLE(0),
/**
* Silent call was made with the force refresh option, or claims were set
* Silent call was made with the force refresh option
*/
FORCE_REFRESH_OR_CLAIMS(1),
FORCE_REFRESH(1),
/**
* Silent call was made with claims set
*/
CLAIMS(1),
/**
* Access token was missing from the cache, but a valid refresh token was used to retrieve a new access token
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SilentRequest extends MsalRequest {

if (parameters.forceRefresh()) {
application.serviceBundle().getServerSideTelemetry().getCurrentRequest().cacheInfo(
CacheRefreshReason.FORCE_REFRESH_OR_CLAIMS);
CacheRefreshReason.FORCE_REFRESH);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,16 @@ void testTokenRefreshReasons() throws Exception {
silentParameters = SilentParameters.builder(Collections.singleton("someScopes"), result.account()).forceRefresh(true).build();
result = cca.acquireTokenSilently(silentParameters).get();

assertRefreshedToken(result, "forcedRefreshToken", CacheRefreshReason.FORCE_REFRESH_OR_CLAIMS, cca.tokenCache.accessTokens.size());
assertRefreshedToken(result, "forcedRefreshToken", CacheRefreshReason.FORCE_REFRESH, cca.tokenCache.accessTokens.size());

//Finally, force a refresh by setting claims
responseParameters.put("access_token", "claimsToken");
TestHelper.createTokenRequestMock(httpClientMock, TestHelper.getSuccessfulTokenResponse(responseParameters), 200);

silentParameters = SilentParameters.builder(Collections.singleton("someScopes"), result.account()).claims(new ClaimsRequest()).forceRefresh(true).build();
silentParameters = SilentParameters.builder(Collections.singleton("someScopes"), result.account()).claims(new ClaimsRequest()).build();
result = cca.acquireTokenSilently(silentParameters).get();

assertRefreshedToken(result, "claimsToken", CacheRefreshReason.FORCE_REFRESH_OR_CLAIMS, cca.tokenCache.accessTokens.size());
assertRefreshedToken(result, "claimsToken", CacheRefreshReason.CLAIMS, cca.tokenCache.accessTokens.size());
}

//Asserts that there is one expected token in the cache, and that it was refreshed with the expected reason
Expand Down

0 comments on commit 94da54f

Please sign in to comment.