Skip to content
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

refresh on is optional field #312

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;

import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.PlainJWT;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections;

public class CachePersistenceIT {

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

String ID_TOKEN_PLACEHOLDER = "<idToken_placeholder>";
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.audience(Collections.singletonList("jwtAudience"))
.issuer("issuer")
.subject("subject")
.build();
PlainJWT jwt = new PlainJWT(claimsSet);

dataToInitCache = dataToInitCache.replace(ID_TOKEN_PLACEHOLDER, jwt.serialize());

ITokenCacheAccessAspect persistenceAspect = new TokenPersistence(dataToInitCache);

PublicClientApplication app = PublicClientApplication.builder("my_client_id")
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/microsoft/aad/msal4j/TokenCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ static private AccessTokenCacheEntity createAccessTokenCacheEntity(TokenRequestE
long currTimestampSec = System.currentTimeMillis() / 1000;
at.cachedAt(Long.toString(currTimestampSec));
at.expiresOn(Long.toString(authenticationResult.expiresOn()));
at.refreshOn(authenticationResult.refreshOn() > 0 ? Long.toString(authenticationResult.refreshOn()) : "0");
if(authenticationResult.refreshOn() > 0 ){
at.refreshOn(Long.toString(authenticationResult.refreshOn()));
}
if (authenticationResult.extExpiresOn() > 0) {
at.extExpiresOn(Long.toString(authenticationResult.extExpiresOn()));
}
Expand Down Expand Up @@ -556,8 +558,10 @@ AuthenticationResult getCachedAuthenticationResult(
if (atCacheEntity.isPresent()) {
builder.
accessToken(atCacheEntity.get().secret).
expiresOn(Long.parseLong(atCacheEntity.get().expiresOn())).
refreshOn(Long.parseLong(atCacheEntity.get().refreshOn()));
expiresOn(Long.parseLong(atCacheEntity.get().expiresOn()));
if(atCacheEntity.get().refreshOn() != null){
builder.refreshOn(Long.parseLong(atCacheEntity.get().refreshOn()));
}
}
if (idTokenCacheEntity.isPresent()) {
builder.
Expand Down Expand Up @@ -602,8 +606,10 @@ AuthenticationResult getCachedAuthenticationResult(
if (atCacheEntity.isPresent()) {
builder.
accessToken(atCacheEntity.get().secret).
expiresOn(Long.parseLong(atCacheEntity.get().expiresOn())).
refreshOn(Long.parseLong(atCacheEntity.get().refreshOn()));
expiresOn(Long.parseLong(atCacheEntity.get().expiresOn()));
if(atCacheEntity.get().refreshOn() != null){
builder.refreshOn(Long.parseLong(atCacheEntity.get().refreshOn()));
}
}
} finally {
lock.readLock().unlock();
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/cache_data/serialized_cache.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"realm": "contoso",
"environment": "login.windows.net",
"credential_type": "IdToken",
"secret": "",
"secret": "<idToken_placeholder>",
"client_id": "my_client_id",
"home_account_id": "uid.utid"
}
Expand Down