Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -45,6 +45,8 @@
value = "org.openmetadata.it.bootstrap.TestSuiteBootstrap#isRedisEnabled",
disabledReason = "Sessions live in Redis on this profile; see SessionRedisMultiNodeIT")
class SessionMultiNodeIT {
private static final String USER_REFRESH_TOKENS =
" WHERE userId = :userId AND tokenType = 'REFRESH_TOKEN'";

@Test
void refreshAndLogoutAreSharedAcrossNodes(TestNamespace ns) throws Exception {
Expand Down Expand Up @@ -324,6 +326,23 @@ void expiredSessionIsRejectedForRefreshAndApiAcrossNodes(TestNamespace ns) throw
assertEquals(401, apiOnB.statusCode(), apiOnB.body());
}

@Test
void expiredRefreshTokenIsRejectedWithoutEchoingIt(TestNamespace ns) throws Exception {
SessionMultiNodeCluster cluster = SessionMultiNodeCluster.getInstance();
User user = createUser(ns);

SessionCookies cookies = new SessionCookies();
HttpClient client = HttpClient.newHttpClient();
login(client, cookies, cluster.nodeABaseUrl(), user.getEmail(), passwordFor(ns));
String refreshToken = expireRefreshTokens(user);

HttpResponse<String> refresh =
postRaw(client, cookies, cluster.nodeABaseUrl() + "/api/v1/auth/refresh", null);

assertEquals(401, refresh.statusCode(), refresh.body());
assertFalse(refresh.body().contains(refreshToken), refresh.body());
}

@Test
void sessionLimitEvictsLeastRecentlyUsedAcrossNodes(TestNamespace ns) throws Exception {
SessionMultiNodeCluster cluster = SessionMultiNodeCluster.getInstance();
Expand Down Expand Up @@ -500,6 +519,26 @@ private void persistSession(UserSession session) {
});
}

private String expireRefreshTokens(User user) {
return TestSuiteBootstrap.getJdbi()
.withHandle(
handle -> {
String sql =
isPostgres(handle)
? "UPDATE user_tokens SET json = jsonb_set(json, '{expiryDate}', '1000')"
: "UPDATE user_tokens SET json = JSON_SET(json, '$.expiryDate', 1000)";
handle
.createUpdate(sql + USER_REFRESH_TOKENS)
.bind("userId", user.getId().toString())
.execute();
return handle
.createQuery("SELECT token FROM user_tokens" + USER_REFRESH_TOKENS)
.bind("userId", user.getId().toString())
.mapTo(String.class)
.one();
});
}

private long sessionCountForUser(User user) {
return TestSuiteBootstrap.getJdbi()
.withHandle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public RefreshToken createRefreshTokenForLogin(UUID currentUserId) {
@Override
public JwtResponse getNewAccessToken(TokenRefreshRequest request) {
if (CommonUtil.nullOrEmpty(request.getRefreshToken())) {
throw new BadRequestException("Token Cannot be Null or Empty String");
throw new AuthenticationException("Refresh token is required. Please login again.");
}
TokenInterface tokenInterface = tokenRepository.findByToken(request.getRefreshToken());
User storedUser =
Expand Down Expand Up @@ -430,10 +430,7 @@ public RefreshToken validateAndReturnNewRefresh(
RefreshToken storedRefreshToken =
(RefreshToken) tokenRepository.findByToken(requestRefreshToken);
if (storedRefreshToken.getExpiryDate().compareTo(Instant.now().toEpochMilli()) < 0) {
throw new CustomExceptionMessage(
BAD_REQUEST,
PASSWORD_RESET_TOKEN_EXPIRED,
"Expired token. Please login again : " + storedRefreshToken.getToken().toString());
throw new AuthenticationException("Refresh token expired. Please login again.");
}
// TODO: currently allow single login from a place, later multiple login can be added
// just delete the existing token
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openmetadata.service.security.auth;

import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;
import static jakarta.ws.rs.core.Response.Status.FORBIDDEN;
import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static jakarta.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE;
Expand All @@ -12,7 +11,6 @@
import static org.openmetadata.service.exception.CatalogExceptionMessage.LDAP_MISSING_ATTR;
import static org.openmetadata.service.exception.CatalogExceptionMessage.MAX_FAILED_LOGIN_ATTEMPT;
import static org.openmetadata.service.exception.CatalogExceptionMessage.MULTIPLE_EMAIL_ENTRIES;
import static org.openmetadata.service.exception.CatalogExceptionMessage.PASSWORD_RESET_TOKEN_EXPIRED;
import static org.openmetadata.service.util.UserUtil.getRoleListFromUser;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -33,7 +31,6 @@
import com.unboundid.ldap.sdk.SearchScope;
import com.unboundid.util.ssl.SSLUtil;
import freemarker.template.TemplateException;
import jakarta.ws.rs.BadRequestException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.time.Instant;
Expand Down Expand Up @@ -639,7 +636,7 @@ public RefreshToken createRefreshTokenForLogin(UUID currentUserId) {
@Override
public JwtResponse getNewAccessToken(TokenRefreshRequest request) {
if (CommonUtil.nullOrEmpty(request.getRefreshToken())) {
throw new BadRequestException("Token Cannot be Null or Empty String");
throw new AuthenticationException("Refresh token is required. Please login again.");
}
TokenInterface tokenInterface = tokenRepository.findByToken(request.getRefreshToken());
User storedUser =
Expand Down Expand Up @@ -674,10 +671,7 @@ public RefreshToken validateAndReturnNewRefresh(
RefreshToken storedRefreshToken =
(RefreshToken) tokenRepository.findByToken(requestRefreshToken);
if (storedRefreshToken.getExpiryDate().compareTo(Instant.now().toEpochMilli()) < 0) {
throw new CustomExceptionMessage(
BAD_REQUEST,
PASSWORD_RESET_TOKEN_EXPIRED,
"Expired token. Please login again : " + storedRefreshToken.getToken().toString());
throw new AuthenticationException("Refresh token expired. Please login again.");
}

// just delete the existing token
Expand Down
Loading