diff --git a/src/test/java/pubsolar/keycloak/events/LastLoginTimeListenerTest.java b/src/.test/java/pubsolar/keycloak/events/LastLoginTimeListenerTest.java similarity index 94% rename from src/test/java/pubsolar/keycloak/events/LastLoginTimeListenerTest.java rename to src/.test/java/pubsolar/keycloak/events/LastLoginTimeListenerTest.java index ecd1b80..cf38887 100644 --- a/src/test/java/pubsolar/keycloak/events/LastLoginTimeListenerTest.java +++ b/src/.test/java/pubsolar/keycloak/events/LastLoginTimeListenerTest.java @@ -1,7 +1,7 @@ package pubsolar.keycloak.events; -import pubsolar.testcontainers.keycloak.KeycloakContainer; -import de.keycloak.test.TestBase; +import dasniko.testcontainers.keycloak.KeycloakContainer; +import dasniko.keycloak.test.TestBase; import org.junit.jupiter.api.Test; import org.keycloak.admin.client.Keycloak; import org.keycloak.representations.idm.RealmEventsConfigRepresentation; diff --git a/src/test/resources/demo-realm.json b/src/.test/resources/demo-realm.json similarity index 100% rename from src/test/resources/demo-realm.json rename to src/.test/resources/demo-realm.json diff --git a/src/test/resources/log4j.properties b/src/.test/resources/log4j.properties similarity index 100% rename from src/test/resources/log4j.properties rename to src/.test/resources/log4j.properties diff --git a/src/main/java/pubsolar/keycloak/events/JsonEventListenerProvider.java b/src/main/java/pubsolar/keycloak/events/JsonEventListenerProvider.java deleted file mode 100644 index c91f04c..0000000 --- a/src/main/java/pubsolar/keycloak/events/JsonEventListenerProvider.java +++ /dev/null @@ -1,114 +0,0 @@ -package pubsolar.keycloak.events; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.ws.rs.core.HttpHeaders; -import jakarta.ws.rs.core.UriInfo; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.keycloak.events.Event; -import org.keycloak.events.EventListenerProvider; -import org.keycloak.events.EventListenerTransaction; -import org.keycloak.events.admin.AdminEvent; -import org.keycloak.models.KeycloakContext; -import org.keycloak.models.KeycloakSession; -import org.keycloak.sessions.AuthenticationSessionModel; -import org.slf4j.event.Level; - -import java.util.Map; - -@Slf4j(topic = "org.keycloak.events") -public class JsonEventListenerProvider implements EventListenerProvider { - - private final KeycloakSession session; - private final ObjectMapper mapper; - private final Level successLevel; - private final Level errorLevel; - private final EventListenerTransaction tx = new EventListenerTransaction(this::sendAdminEvent, this::logEvent); - - public JsonEventListenerProvider(KeycloakSession session, ObjectMapper mapper, Level successLevel, Level errorLevel) { - this.session = session; - this.mapper = mapper; - this.successLevel = successLevel; - this.errorLevel = errorLevel; - - session.getTransactionManager().enlistAfterCompletion(tx); - } - - @Override - public void onEvent(Event event) { - tx.addEvent(event); - } - - @Override - public void onEvent(AdminEvent event, boolean includeRepresentation) { - tx.addAdminEvent(event, includeRepresentation); - } - - @Override - public void close() { - } - - private void logEvent(Event event) { - Level level = event.getError() != null ? errorLevel : successLevel; - - if (log.isEnabledForLevel(level)) { - String s = null; - try { - Map map = mapper.convertValue(event, new TypeReference<>() {}); - - AuthenticationSessionModel authSession = session.getContext().getAuthenticationSession(); - if(authSession!=null) { - map.put("authSessionParentId", authSession.getParentSession().getId()); - map.put("authSessionTabId", authSession.getTabId()); - } - - if (log.isTraceEnabled()) { - setKeycloakContext(map); - } - - s = mapper.writeValueAsString(map); - } catch (JsonProcessingException e) { - log.error("Error while trying to JSONify event %s".formatted(ToStringBuilder.reflectionToString(event)), e); - } - - log.atLevel(log.isTraceEnabled() ? Level.TRACE : level).log(s); - } - } - - private void sendAdminEvent(AdminEvent event, boolean includeRepresentation) { - Level level = event.getError() != null ? errorLevel : successLevel; - - if (log.isEnabledForLevel(level)) { - String s = null; - try { - Map map = mapper.convertValue(event, new TypeReference<>() { - }); - - if (log.isTraceEnabled()) { - setKeycloakContext(map); - } - - s = mapper.writeValueAsString(map); - } catch (JsonProcessingException e) { - log.error("Error while trying to JSONify admin event %s".formatted(ToStringBuilder.reflectionToString(event)), e); - } - - log.atLevel(log.isTraceEnabled() ? Level.TRACE : level).log(s); - } - } - - private void setKeycloakContext(Map map) { - KeycloakContext context = session.getContext(); - UriInfo uriInfo = context.getUri(); - if (uriInfo != null) { - map.put("requestUri", uriInfo.getRequestUri().toString()); - } - HttpHeaders headers = context.getRequestHeaders(); - if (headers != null) { - map.put("cookies", headers.getCookies()); - } - } - -} diff --git a/src/main/java/pubsolar/keycloak/events/JsonEventListenerProviderFactory.java b/src/main/java/pubsolar/keycloak/events/JsonEventListenerProviderFactory.java deleted file mode 100644 index 6038591..0000000 --- a/src/main/java/pubsolar/keycloak/events/JsonEventListenerProviderFactory.java +++ /dev/null @@ -1,76 +0,0 @@ -package pubsolar.keycloak.events; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.auto.service.AutoService; -import org.keycloak.Config; -import org.keycloak.events.EventListenerProvider; -import org.keycloak.events.EventListenerProviderFactory; -import org.keycloak.models.KeycloakSession; -import org.keycloak.models.KeycloakSessionFactory; -import org.keycloak.provider.ProviderConfigProperty; -import org.keycloak.provider.ProviderConfigurationBuilder; -import org.slf4j.event.Level; - -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; - -@AutoService(EventListenerProviderFactory.class) -public class JsonEventListenerProviderFactory implements EventListenerProviderFactory { - - public static final String PROVIDER_ID = "json-logging"; - - private static final ObjectMapper mapper = new ObjectMapper(); - - private Level successLevel; - private Level errorLevel; - - @Override - public EventListenerProvider create(KeycloakSession keycloakSession) { - return new JsonEventListenerProvider(keycloakSession, mapper, successLevel, errorLevel); - } - - @Override - public void init(Config.Scope config) { - successLevel = Level.valueOf(config.get("success-level", "debug").toUpperCase()); - errorLevel = Level.valueOf(config.get("error-level", "warn").toUpperCase()); - } - - @Override - public void postInit(KeycloakSessionFactory keycloakSessionFactory) { - } - - @Override - public void close() { - } - - @Override - public String getId() { - return PROVIDER_ID; - } - - @Override - public List getConfigMetadata() { - String[] logLevels = Arrays.stream(Level.values()) - .map(Level::name) - .map(String::toLowerCase) - .sorted(Comparator.naturalOrder()) - .toArray(String[]::new); - return ProviderConfigurationBuilder.create() - .property() - .name("success-level") - .type("string") - .helpText("The log level for success messages.") - .options(logLevels) - .defaultValue("debug") - .add() - .property() - .name("error-level") - .type("string") - .helpText("The log level for error messages.") - .options(logLevels) - .defaultValue("warn") - .add() - .build(); - } -} diff --git a/src/main/java/pubsolar/keycloak/events/LastLoginTimeListener.java b/src/main/java/pubsolar/keycloak/events/LastLoginTimeListener.java index 941d291..8322ff5 100644 --- a/src/main/java/pubsolar/keycloak/events/LastLoginTimeListener.java +++ b/src/main/java/pubsolar/keycloak/events/LastLoginTimeListener.java @@ -1,4 +1,4 @@ -package dasniko.keycloak.events; +package pubsolar.keycloak.events; import lombok.RequiredArgsConstructor; import org.keycloak.common.util.Time;