Skip to content

Commit 8cf7abb

Browse files
authored
refresh on is optional field (#312)
* refresh on optional field
1 parent b447f9e commit 8cf7abb

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

src/integrationtest/java/com.microsoft.aad.msal4j/AcquireTokenSilentIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public void acquireTokenSilent_ADFS2019(String environment) throws Exception{
138138
assertResultRefreshed(result, resultAfterRefresh);
139139
}
140140

141-
@Test
141+
// Commented out due to unclear B2C behavior causing occasional errors
142+
//@Test
142143
public void acquireTokenSilent_B2C() throws Exception{
143144
UserQueryParameters query = new UserQueryParameters();
144145
query.parameters.put(UserQueryParameters.USER_TYPE, UserType.B2C);
@@ -244,8 +245,6 @@ public void acquireTokenSilent_WithRefreshOn(String environment) throws Exceptio
244245
AccessTokenCacheEntity token = pca.tokenCache.accessTokens.get(key);
245246
long currTimestampSec = new Date().getTime() / 1000;
246247

247-
Assert.assertEquals(token.refreshOn(), "0");
248-
249248
token.refreshOn(Long.toString(currTimestampSec + 60));
250249
pca.tokenCache.accessTokens.put(key, token);
251250

src/integrationtest/java/com.microsoft.aad.msal4j/CachePersistenceIT.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
// Licensed under the MIT License.
33
package com.microsoft.aad.msal4j;
44

5+
import com.nimbusds.jwt.JWTClaimsSet;
6+
import com.nimbusds.jwt.PlainJWT;
57
import org.testng.Assert;
68
import org.testng.annotations.Test;
79

810
import java.io.IOException;
911
import java.net.URISyntaxException;
12+
import java.util.Collections;
1013

1114
public class CachePersistenceIT {
1215

@@ -32,6 +35,16 @@ public void afterCacheAccess(ITokenCacheAccessContext iTokenCacheAccessContext)
3235
public void cacheDeserializationSerializationTest() throws IOException, URISyntaxException {
3336
String dataToInitCache = TestHelper.readResource(this.getClass(), "/cache_data/serialized_cache.json");
3437

38+
String ID_TOKEN_PLACEHOLDER = "<idToken_placeholder>";
39+
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
40+
.audience(Collections.singletonList("jwtAudience"))
41+
.issuer("issuer")
42+
.subject("subject")
43+
.build();
44+
PlainJWT jwt = new PlainJWT(claimsSet);
45+
46+
dataToInitCache = dataToInitCache.replace(ID_TOKEN_PLACEHOLDER, jwt.serialize());
47+
3548
ITokenCacheAccessAspect persistenceAspect = new TokenPersistence(dataToInitCache);
3649

3750
PublicClientApplication app = PublicClientApplication.builder("my_client_id")

src/main/java/com/microsoft/aad/msal4j/TokenCache.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ static private AccessTokenCacheEntity createAccessTokenCacheEntity(TokenRequestE
258258
long currTimestampSec = System.currentTimeMillis() / 1000;
259259
at.cachedAt(Long.toString(currTimestampSec));
260260
at.expiresOn(Long.toString(authenticationResult.expiresOn()));
261-
at.refreshOn(authenticationResult.refreshOn() > 0 ? Long.toString(authenticationResult.refreshOn()) : "0");
261+
if(authenticationResult.refreshOn() > 0 ){
262+
at.refreshOn(Long.toString(authenticationResult.refreshOn()));
263+
}
262264
if (authenticationResult.extExpiresOn() > 0) {
263265
at.extExpiresOn(Long.toString(authenticationResult.extExpiresOn()));
264266
}
@@ -556,8 +558,10 @@ AuthenticationResult getCachedAuthenticationResult(
556558
if (atCacheEntity.isPresent()) {
557559
builder.
558560
accessToken(atCacheEntity.get().secret).
559-
expiresOn(Long.parseLong(atCacheEntity.get().expiresOn())).
560-
refreshOn(Long.parseLong(atCacheEntity.get().refreshOn()));
561+
expiresOn(Long.parseLong(atCacheEntity.get().expiresOn()));
562+
if(atCacheEntity.get().refreshOn() != null){
563+
builder.refreshOn(Long.parseLong(atCacheEntity.get().refreshOn()));
564+
}
561565
}
562566
if (idTokenCacheEntity.isPresent()) {
563567
builder.
@@ -602,8 +606,10 @@ AuthenticationResult getCachedAuthenticationResult(
602606
if (atCacheEntity.isPresent()) {
603607
builder.
604608
accessToken(atCacheEntity.get().secret).
605-
expiresOn(Long.parseLong(atCacheEntity.get().expiresOn())).
606-
refreshOn(Long.parseLong(atCacheEntity.get().refreshOn()));
609+
expiresOn(Long.parseLong(atCacheEntity.get().expiresOn()));
610+
if(atCacheEntity.get().refreshOn() != null){
611+
builder.refreshOn(Long.parseLong(atCacheEntity.get().refreshOn()));
612+
}
607613
}
608614
} finally {
609615
lock.readLock().unlock();

src/test/resources/cache_data/serialized_cache.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"realm": "contoso",
4242
"environment": "login.windows.net",
4343
"credential_type": "IdToken",
44-
"secret": "",
44+
"secret": "<idToken_placeholder>",
4545
"client_id": "my_client_id",
4646
"home_account_id": "uid.utid"
4747
}

0 commit comments

Comments
 (0)