Skip to content

Commit

Permalink
Make sure refresh token expiration is based on the current time when …
Browse files Browse the repository at this point in the history
…the token is issued

Closes keycloak#27180

Signed-off-by: Pedro Igor <[email protected]>
  • Loading branch information
pedroigor committed Mar 7, 2024
1 parent b459630 commit 5b4cdab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
21 changes: 11 additions & 10 deletions services/src/main/java/org/keycloak/protocol/oidc/TokenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,6 @@ public AccessTokenResponseBuilder refreshAccessToken(KeycloakSession session, Ur

validateTokenReuseForRefresh(session, realm, refreshToken, validation);

int currentTime = Time.currentTime();
clientSession.setTimestamp(currentTime);
validation.userSession.setLastSessionRefresh(currentTime);

if (refreshToken.getAuthorization() != null) {
validation.newToken.setAuthorization(refreshToken.getAuthorization());
}
Expand Down Expand Up @@ -1142,24 +1138,29 @@ public AccessTokenResponseBuilder generateRefreshToken(String scope) {
}

private void generateRefreshToken(boolean offlineTokenRequested) {
refreshToken = new RefreshToken(accessToken);
refreshToken.id(KeycloakModelUtils.generateId());
refreshToken.issuedNow();
int currentTime = Time.currentTime();
AuthenticatedClientSessionModel clientSession = clientSessionCtx.getClientSession();
clientSession.setTimestamp(currentTime);
UserSessionModel userSession = clientSession.getUserSession();
userSession.setLastSessionRefresh(currentTime);

if (offlineTokenRequested) {
UserSessionManager sessionManager = new UserSessionManager(session);
if (!sessionManager.isOfflineTokenAllowed(clientSessionCtx)) {
event.error(Errors.NOT_ALLOWED);
throw new ErrorResponseException("not_allowed", "Offline tokens not allowed for the user or client", Response.Status.BAD_REQUEST);
}

refreshToken = new RefreshToken(accessToken);
refreshToken.type(TokenUtil.TOKEN_TYPE_OFFLINE);
if (realm.isOfflineSessionMaxLifespanEnabled())
if (realm.isOfflineSessionMaxLifespanEnabled()) {
refreshToken.expiration(getExpiration(true));
}
sessionManager.createOrUpdateOfflineSession(clientSessionCtx.getClientSession(), userSession);
} else {
refreshToken = new RefreshToken(accessToken);
refreshToken.expiration(getExpiration(false));
}
refreshToken.id(KeycloakModelUtils.generateId());
refreshToken.issuedNow();
}

private int getExpiration(boolean offline) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,32 @@ public void testRefreshTokenFromClientOtherThanAudience() throws Exception {
assertFalse(token.getAuthorization().getPermissions().isEmpty());
}

@Test
public void testTokenExpirationRenewalWhenIssuingTokens() {
oauth.realm("authz-test");
oauth.clientId(PUBLIC_TEST_CLIENT);
oauth.doLogin("marta", "password");
String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
OAuthClient.AccessTokenResponse accessTokenResponse = oauth.doAccessTokenRequest(code, null);
assertNotNull(accessTokenResponse.getAccessToken());
assertNotNull(accessTokenResponse.getRefreshToken());

try {
for (int i = 0; i < 3; i++) {
AuthorizationRequest request = new AuthorizationRequest();
request.setAudience(RESOURCE_SERVER_TEST);
AuthorizationResponse authorizationResponse = getAuthzClient(PUBLIC_TEST_CLIENT_CONFIG).authorization(accessTokenResponse.getAccessToken()).authorize(request);
AccessToken refreshToken = toAccessToken(authorizationResponse.getRefreshToken());
AccessToken accessTokenToken = toAccessToken(authorizationResponse.getToken());
assertEquals(refreshToken.getExp() - refreshToken.getIat(), 1800);
assertEquals(accessTokenToken.getExp() - accessTokenToken.getIat(), 300);
setTimeOffset(i);
}
} finally {
resetTimeOffset();
}
}

@Test
public void testUsingExpiredToken() throws Exception {
ClientResource client = getClient(getRealm(), RESOURCE_SERVER_TEST);
Expand Down

0 comments on commit 5b4cdab

Please sign in to comment.