8000 when the realm session limit is exceeded and behavior is set to Termi… by martinkrcka · Pull Request #48064 · keycloak/keycloak · GitHub
[go: up one dir, main page]

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 @@ -116,7 +116,7 @@ private boolean exceedsLimit(long count, long limit) {
}

private long getNumberOfSessionsThatNeedToBeLoggedOut(long count, long limit) {
return count - (limit - 1);
return Math.max(0, count - (limit - 1));
}

private int getIntConfigProperty(String key, Map<String, String> config) {
Expand Down Expand Up @@ -194,7 +194,11 @@ private List<UserSessionModel> handleLimitExceeded(AuthenticationFlowContext con
*/
private List<UserSessionModel> logoutOldestSessions(List<UserSessionModel> userSessions, long limit, EventBuilder eventBuilder) {
long numberOfSessionsThatNeedToBeLoggedOut = getNumberOfSessionsThatNeedToBeLoggedOut(userSessions.size(), limit);
if (numberOfSessionsThatNeedToBeLoggedOut == 1) {

if (numberOfSessionsThatNeedToBeLoggedOut == 0) {
logger.debug("No additional sessions that need to be logged out");
return Collections.emptyList();
} else if (numberOfSessionsThatNeedToBeLoggedOut == 1) {
logger.info("Logging out oldest session");
} else {
logger.infof("Logging out oldest %s sessions", numberOfSessionsThatNeedToBeLoggedOut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,40 @@ public void testClientSessionCountExceededAndNewSessionDeniedDirectGrantFlow() t
assertEquals(ERROR_TO_DISPLAY, response.getError());
}

@Test
public void testRealmSessionLimitExceededButClientLimitNotExceededShouldNotThrow() throws Exception {
// Reproduces: realm limit exceeded on client-a, then login to client-b whose client
// session count is below the client limit. Without the fix, logoutOldestSessions()
// receives a negative count and Stream.limit() throws IllegalArgumentException.
try {
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.BEHAVIOR, UserSessionLimitsAuthenticatorFactory.TERMINATE_OLDEST_SESSION);
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.USER_REALM_LIMIT, "2");
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.USER_CLIENT_LIMIT, "3");

AccessTokenResponse response = oauth.client("direct-grant-1", "password")
.doPasswordGrantRequest("test-user@localhost", "password");
assertEquals(200, response.getStatusCode());

response = oauth.client("direct-grant-1", "password")
.doPasswordGrantRequest("test-user@localhost", "password");
assertEquals(200, response.getStatusCode());

// realm limit reached (2 sessions on direct-grant-1); direct-grant-2 has 0 sessions,
// below the client limit of 3. login must succeed and must not throw a 500.
response = oauth.client("direct-grant-2", "password")
.doPasswordGrantRequest("test-user@localhost", "password");
assertEquals(200, response.getStatusCode());

testingClient.server(realmName).run(assertSessionCount(realmName, username, 2));
testingClient.server(realmName).run(assertClientSessionCount(realmName, username, "direct-grant-1", 1));
testingClient.server(realmName).run(assertClientSessionCount(realmName, username, "direct-grant-2", 1));
} finally {
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.BEHAVIOR, UserSessionLimitsAuthenticatorFactory.DENY_NEW_SESSION);
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.USER_REALM_LIMIT, "0");
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.USER_CLIENT_LIMIT, "1");
}
}

@Test
public void testRealmSessionCountAndClientSessionCountExceededAndOldestClientSessionShouldBePrioritized() throws Exception {
try {
Expand Down
Loading
0