Skip to content

Commit 2de2b4a

Browse files
committed
Address PR feedback
1 parent 7d794be commit 2de2b4a

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenSilentSupplier.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ private boolean shouldRefresh(SilentParameters parameters, AuthenticationResult
120120
//If forceRefresh is true, no reason to check any other option
121121
if (parameters.forceRefresh()) {
122122
setCacheTelemetry(CacheRefreshReason.FORCE_REFRESH);
123-
log.debug("Refreshing access token because forceRefresh parameter is true.");
123+
log.debug(String.format("Refreshing access token. Cache refresh reason: %s", CacheRefreshReason.FORCE_REFRESH));
124124
return true;
125125
}
126126

127127
//If the request contains claims then the token should be refreshed, to ensure that the returned token has the correct claims
128128
// Note: these are the types of claims found in (for example) a claims challenge, and do not include client capabilities
129129
if (parameters.claims() != null) {
130130
setCacheTelemetry(CacheRefreshReason.CLAIMS);
131-
log.debug("Refreshing access token because the claims parameter is not null.");
131+
log.debug(String.format("Refreshing access token. Cache refresh reason: %s", CacheRefreshReason.CLAIMS));
132132
return true;
133133
}
134134

@@ -137,7 +137,7 @@ private boolean shouldRefresh(SilentParameters parameters, AuthenticationResult
137137
//If the access token is expired or within 5 minutes of becoming expired, refresh it
138138
if (!StringHelper.isBlank(cachedResult.accessToken()) && cachedResult.expiresOn() < (currTimeStampSec + ACCESS_TOKEN_EXPIRE_BUFFER_IN_SEC)) {
139139
setCacheTelemetry(CacheRefreshReason.EXPIRED);
140-
log.debug("Refreshing access token because it is expired.");
140+
log.debug(String.format("Refreshing access token. Cache refresh reason: %s", CacheRefreshReason.EXPIRED));
141141
return true;
142142
}
143143

@@ -146,14 +146,14 @@ private boolean shouldRefresh(SilentParameters parameters, AuthenticationResult
146146
cachedResult.refreshOn() != null && cachedResult.refreshOn() > 0 &&
147147
cachedResult.refreshOn() < currTimeStampSec && cachedResult.expiresOn() >= (currTimeStampSec + ACCESS_TOKEN_EXPIRE_BUFFER_IN_SEC)){
148148
setCacheTelemetry(CacheRefreshReason.PROACTIVE_REFRESH);
149-
log.debug("Attempting to refresh access token because it is after the refreshOn time.");
149+
log.debug(String.format("Refreshing access token. Cache refresh reason: %s", CacheRefreshReason.PROACTIVE_REFRESH));
150150
return true;
151151
}
152152

153153
//If there is a refresh token but no access token, we should use the refresh token to get the access token
154154
if (StringHelper.isBlank(cachedResult.accessToken()) && !StringHelper.isBlank(cachedResult.refreshToken())) {
155155
setCacheTelemetry(CacheRefreshReason.NO_CACHED_ACCESS_TOKEN);
156-
log.debug("Refreshing access token because it was missing from the cache.");
156+
log.debug(String.format("Refreshing access token. Cache refresh reason: %s", CacheRefreshReason.NO_CACHED_ACCESS_TOKEN));
157157
return true;
158158
}
159159

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/AcquireTokenSilentlyTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.HashMap;
2222
import java.util.concurrent.CompletableFuture;
2323
import java.util.concurrent.ExecutionException;
24+
import java.util.concurrent.TimeUnit;
2425

2526

2627
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@@ -157,15 +158,31 @@ void testTokenRefreshReasons() throws Exception {
157158
assertRefreshedToken(result, "nearlyExpiredToken", CacheRefreshReason.EXPIRED, cca.tokenCache.accessTokens.size());
158159

159160
//Attempt to retrieve the cached token, however it is within the 5-minute buffer and should be refreshed.
160-
// In this test, it will be replaced with a token that expires in 1 hour
161+
// In this test, it will be replaced with a token that expires in 1 hour but has a refresh_in time of 1 second
162+
responseParameters.put("access_token", "refreshInToken");
163+
responseParameters.put("expires_in", "3600");
164+
responseParameters.put("refresh_in", "1");
165+
TestHelper.createTokenRequestMock(httpClientMock, TestHelper.getSuccessfulTokenResponse(responseParameters), 200);
166+
167+
silentParameters = SilentParameters.builder(Collections.singleton("someScopes"), result.account()).build();
168+
result = cca.acquireTokenSilently(silentParameters).get();
169+
170+
assertRefreshedToken(result, "refreshInToken", CacheRefreshReason.EXPIRED, cca.tokenCache.accessTokens.size());
171+
172+
//Attempt to retrieve the cached token, however it is within the 5-minute buffer and should be refreshed.
173+
// In this test, it will be replaced with a token that expires in 1 hour (and does not have a valid refresh_in time)
161174
responseParameters.put("access_token", "normalToken");
162175
responseParameters.put("expires_in", "3600");
176+
responseParameters.put("refresh_in", "0");
163177
TestHelper.createTokenRequestMock(httpClientMock, TestHelper.getSuccessfulTokenResponse(responseParameters), 200);
164178

179+
//refresh_in values are in seconds, so we must wait to guarantee it is past the proactive refresh time
180+
TimeUnit.SECONDS.sleep(2);
181+
165182
silentParameters = SilentParameters.builder(Collections.singleton("someScopes"), result.account()).build();
166183
result = cca.acquireTokenSilently(silentParameters).get();
167184

168-
assertRefreshedToken(result, "normalToken", CacheRefreshReason.EXPIRED, cca.tokenCache.accessTokens.size());
185+
assertRefreshedToken(result, "normalToken", CacheRefreshReason.PROACTIVE_REFRESH, cca.tokenCache.accessTokens.size());
169186

170187
//Force the token to be refreshed
171188
responseParameters.put("access_token", "forcedRefreshToken");

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/TestHelper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TestHelper {
2929
static String signedAssertion = generateToken();
3030
private static final String successfulResponseFormat = "{\"access_token\":\"%s\",\"id_token\":\"%s\",\"refresh_token\":\"%s\"," +
3131
"\"client_id\":\"%s\",\"client_info\":\"%s\"," +
32-
"\"expires_on\": %d ,\"expires_in\": %d," +
32+
"\"refresh_in\": %d,\"expires_on\": %d,\"expires_in\": %d," +
3333
"\"token_type\":\"Bearer\"}";
3434

3535
static final String idTokenFormat = "{\"aud\": \"%s\"," +
@@ -87,13 +87,17 @@ static String getSuccessfulTokenResponse(HashMap<String, String> responseValues)
8787
long expiresOn = responseValues.containsKey("expires_on")
8888
? Long.parseLong(responseValues.get("expires_0n")) :
8989
(System.currentTimeMillis() / 1000) + expiresIn;
90+
long refreshIn = responseValues.containsKey("refresh_in")
91+
? Long.parseLong(responseValues.get("refresh_in")) :
92+
0;
9093

9194
return String.format(successfulResponseFormat,
9295
responseValues.getOrDefault("access_token", "access_token"),
9396
responseValues.getOrDefault("id_token", ""),
9497
responseValues.getOrDefault("refresh_token", "refresh_token"),
9598
responseValues.getOrDefault("client_id", "client_id"),
9699
responseValues.getOrDefault("client_info", "eyJ1aWQiOiI1OTdmODZjZC0xM2YzLTQ0YzAtYmVjZS1hMWU3N2JhNDMyMjgiLCJ1dGlkIjoiZjY0NWFkOTItZTM4ZC00ZDFhLWI1MTAtZDFiMDlhNzRhOGNhIn0"),
100+
refreshIn,
97101
expiresOn,
98102
expiresIn
99103
);

0 commit comments

Comments
 (0)