-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Invalidate in progress authentication sessions on credential reset #50702
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright 2025 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.models.sessions.infinispan.changes.remote.remover.query; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| import org.keycloak.models.sessions.infinispan.changes.remote.remover.ConditionalRemover; | ||
| import org.keycloak.models.sessions.infinispan.entities.RootAuthenticationSessionEntity; | ||
|
|
||
| /** | ||
| * A {@link ConditionalRemover} implementation to delete {@link RootAuthenticationSessionEntity} based on either the | ||
| * realm or the authenticated user. | ||
| * | ||
| * The removal is performed server-side with a single Infinispan Ickle query. Removing by user relies on the indexed | ||
| * {@code authenticatedUserIds} field of {@link RootAuthenticationSessionEntity}. | ||
| */ | ||
| public class AuthenticationSessionQueryConditionalRemover extends MultipleConditionQueryRemover<String, RootAuthenticationSessionEntity> { | ||
|
|
||
| private final String entity; | ||
|
|
||
| public AuthenticationSessionQueryConditionalRemover(String entity) { | ||
| this.entity = entity; | ||
| } | ||
|
|
||
| @Override | ||
| String getEntity() { | ||
| return entity; | ||
| } | ||
|
|
||
| public void removeByRealmId(String realmId) { | ||
| add(new RemoveByRealm(nextParameter(), realmId)); | ||
| } | ||
|
|
||
| public void removeByUser(String realmId, String userId, String rootAuthenticationSessionIdToKeep) { | ||
| var keepParameter = rootAuthenticationSessionIdToKeep == null ? null : nextParameter(); | ||
| add(new RemoveByUser(nextParameter(), realmId, nextParameter(), userId, keepParameter, rootAuthenticationSessionIdToKeep)); | ||
| } | ||
|
|
||
| private record RemoveByRealm(String realmParameter, | ||
| String realmId) implements RemoveCondition<String, RootAuthenticationSessionEntity> { | ||
|
|
||
| @Override | ||
| public String getConditionalClause() { | ||
| return "(realmId = :%s)".formatted(realmParameter); | ||
| } | ||
|
|
||
| @Override | ||
| public void addParameters(Map<String, Object> parameters) { | ||
| parameters.put(realmParameter, realmId); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean willRemove(String key, RootAuthenticationSessionEntity value) { | ||
| return Objects.equals(realmId, value.getRealmId()); | ||
| } | ||
| } | ||
|
|
||
| private record RemoveByUser(String realmParameter, String realmId, String userParameter, String userId, | ||
| String keepParameter, String rootAuthenticationSessionIdToKeep) | ||
| implements RemoveCondition<String, RootAuthenticationSessionEntity> { | ||
|
|
||
| @Override | ||
| public String getConditionalClause() { | ||
| if (keepParameter == null) { | ||
| return "(realmId = :%s && authenticatedUserIds = :%s)".formatted(realmParameter, userParameter); | ||
| } | ||
| return "(realmId = :%s && authenticatedUserIds = :%s && id != :%s)".formatted(realmParameter, userParameter, keepParameter); | ||
| } | ||
|
|
||
| @Override | ||
| public void addParameters(Map<String, Object> parameters) { | ||
| parameters.put(realmParameter, realmId); | ||
| parameters.put(userParameter, userId); | ||
| if (keepParameter != null) { | ||
| parameters.put(keepParameter, rootAuthenticationSessionIdToKeep); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean willRemove(String key, RootAuthenticationSessionEntity value) { | ||
| return Objects.equals(realmId, value.getRealmId()) | ||
| && value.hasAuthenticationSessionForUser(userId) | ||
| && !Objects.equals(key, rootAuthenticationSessionIdToKeep); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,16 @@ | |
| query = "SELECT sess.id FROM RootAuthenticationSessionEntity sess" + | ||
| " WHERE sess.realmId = :realmId AND sess.timestamp < :timestamp" | ||
| ), | ||
| @NamedQuery( | ||
| name = "deleteRootAuthSessionsByUser", | ||
| query = "DELETE FROM RootAuthenticationSessionEntity sess" + | ||
| " WHERE sess.realmId = :realmId" + | ||
| " AND (:rootSessionIdToKeep IS NULL OR sess.id <> :rootSessionIdToKeep)" + | ||
| " AND EXISTS (" + | ||
| " SELECT auth.tabId FROM AuthenticationSessionEntity auth" + | ||
| " WHERE auth.rootAuthenticationSession = sess AND auth.authUserId = :userId" + | ||
| " )" | ||
| ), | ||
|
Comment on lines
+50
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gaoyikeshuer I think this type of queries are very slow in MySQL/MariaDB. I remember we added some specific queries per db type. Just check if you need something special here for the delete. See queries-mariadb.properties and the other file for mysql. Maybe you need to change this query for a delete with join. Not sure, but please recheck it. Please ensure at least one test for this is executed in the DB jobs for every database we support. |
||
| @NamedQuery( | ||
| name = "deleteExpiredRootAuthSessionByIds", | ||
| query = "DELETE FROM RootAuthenticationSessionEntity e WHERE e.id IN :ids AND e.timestamp < :timestamp" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
| <!-- | ||
| ~ * Copyright 2025 Red Hat, Inc. and/or its affiliates | ||
| ~ * and other contributors as indicated by the @author tags. | ||
| ~ * | ||
| ~ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ * you may not use this file except in compliance with the License. | ||
| ~ * You may obtain a copy of the License at | ||
| ~ * | ||
| ~ * http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ * | ||
| ~ * Unless required by applicable law or agreed to in writing, software | ||
| ~ * distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ * See the License for the specific language governing permissions and | ||
| ~ * limitations under the License. | ||
| --> | ||
| <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> | ||
|
|
||
| <changeSet author="keycloak" id="26.8.0-auth-session-auth-user-id-index"> | ||
| <!-- Index used when invalidating a user's in-progress authentication sessions on credential reset | ||
| with "sign out of other devices" (findRootAuthSessionIdsByUser filters on AUTH_USER_ID). --> | ||
| <createIndex tableName="AUTH_SESSION" indexName="IDX_AUTH_SESSION_AUTH_USER_ID"> | ||
| <column name="AUTH_USER_ID"/> | ||
| </createIndex> | ||
| </changeSet> | ||
|
|
||
| </databaseChangeLog> |
Uh oh!
There was an error while loading. Please reload this page.