KEYCLOAK-10209 - AuthenticationSessionModel made available through

KeycloakContext in KeycloakSession
This commit is contained in:
Vlastimil Elias 2019-07-10 15:09:49 +02:00 committed by Marek Posolda
parent 8b203d48ce
commit 4571f65d1e
5 changed files with 37 additions and 4 deletions

View file

@ -99,6 +99,7 @@ public class RootAuthenticationSessionAdapter implements RootAuthenticationSessi
AuthenticationSessionModel authSession = getAuthenticationSessions().get(tabId);
if (authSession != null && client.equals(authSession.getClient())) {
session.getContext().setAuthenticationSession(authSession);
return authSession;
} else {
return null;
@ -118,7 +119,9 @@ public class RootAuthenticationSessionAdapter implements RootAuthenticationSessi
update();
return new AuthenticationSessionAdapter(session, this, tabId, authSessionEntity);
AuthenticationSessionAdapter authSession = new AuthenticationSessionAdapter(session, this, tabId, authSessionEntity);
session.getContext().setAuthenticationSession(authSession);
return authSession;
}
@Override

View file

@ -18,9 +18,9 @@
package org.keycloak.models;
import org.keycloak.common.ClientConnection;
import org.keycloak.sessions.AuthenticationSessionModel;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.util.Locale;
@ -53,4 +53,12 @@ public interface KeycloakContext {
Locale resolveLocale(UserModel user);
/**
* Get current AuthenticationSessionModel, can be null out of the AuthenticationSession context.
*
* @return current AuthenticationSessionModel or null
*/
AuthenticationSessionModel getAuthenticationSession();
void setAuthenticationSession(AuthenticationSessionModel authenticationSession);
}

View file

@ -23,6 +23,7 @@ import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.models.KeycloakContext;
import org.keycloak.models.KeycloakSession;
import org.keycloak.sessions.AuthenticationSessionModel;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.HttpHeaders;
@ -84,6 +85,14 @@ public class JBossLoggingEventListenerProvider implements EventListenerProvider
}
}
AuthenticationSessionModel authSession = session.getContext().getAuthenticationSession();
if(authSession!=null) {
sb.append(", authSessionParentId=");
sb.append(authSession.getParentSession().getId());
sb.append(", authSessionTabId=");
sb.append(authSession.getTabId());
}
if(logger.isTraceEnabled()) {
setKeycloakContext(sb);
}

View file

@ -27,6 +27,7 @@ import org.keycloak.models.KeycloakUriInfo;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.services.resources.KeycloakApplication;
import org.keycloak.sessions.AuthenticationSessionModel;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;
@ -48,6 +49,8 @@ public class DefaultKeycloakContext implements KeycloakContext {
private KeycloakUriInfo uriInfo;
private AuthenticationSessionModel authenticationSession;
public DefaultKeycloakContext(KeycloakSession session) {
this.session = session;
}
@ -119,4 +122,14 @@ public class DefaultKeycloakContext implements KeycloakContext {
public Locale resolveLocale(UserModel user) {
return session.getProvider(LocaleSelectorProvider.class).resolveLocale(realm, user);
}
@Override
public AuthenticationSessionModel getAuthenticationSession() {
return authenticationSession;
}
@Override
public void setAuthenticationSession(AuthenticationSessionModel authenticationSession) {
this.authenticationSession = authenticationSession;
}
}

View file

@ -299,12 +299,12 @@ public class AuthenticationManager {
// See if we have logoutAuthSession inside current rootSession. Create new if not
Optional<AuthenticationSessionModel> found = rootLogoutSession.getAuthenticationSessions().values().stream().filter((AuthenticationSessionModel authSession) -> {
return client.equals(authSession.getClient()) && Objects.equals(AuthenticationSessionModel.Action.LOGGING_OUT.name(), authSession.getAction());
}).findFirst();
AuthenticationSessionModel logoutAuthSession = found.isPresent() ? found.get() : rootLogoutSession.createAuthenticationSession(client);
session.getContext().setAuthenticationSession(logoutAuthSession);
logoutAuthSession.setAction(AuthenticationSessionModel.Action.LOGGING_OUT.name());
return logoutAuthSession;