From 4fd7a56a4e723abdef0346d804bbe2d37b97e856 Mon Sep 17 00:00:00 2001 From: Thore Date: Tue, 12 Jan 2021 18:48:42 +0100 Subject: [PATCH] Add event listener to separate repo --- .gitignore | 52 +++++++++++++++ README.md | 9 +++ pom.xml | 65 +++++++++++++++++++ .../LastLoginEventListenerProvider.java | 55 ++++++++++++++++ ...LastLoginEventListenerProviderFactory.java | 36 ++++++++++ .../META-INF/jboss-deployment-structure.xml | 12 ++++ ...ycloak.events.EventListenerProviderFactory | 1 + 7 files changed, 230 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java create mode 100644 src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProviderFactory.java create mode 100644 src/main/resources/META-INF/jboss-deployment-structure.xml create mode 100644 src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1aec543 --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ +# Customize with https://github.com/github/gitignore + +# compiled output +/dist +/tmp +/out-tsc +/node +/build +/out +target/ +dist/ + +.gradle + +# dependencies +node_modules +.vertx + +# IDEs and editors +*.iml +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +# misc +/.sass-cache +/connect.lock +/coverage +/libpeerconnection.log +npm-debug.log +testem.log +/typings + +# e2e +/e2e/*.js +/e2e/*.map + +# System Files +.DS_Store +Thumbs.db + diff --git a/README.md b/README.md new file mode 100644 index 0000000..b567939 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Keycloak Last Login Event Listener + +Sets a last login attribute on the user model when they login. + +This is based on the templates provided by [zonaut](https://github.com/zonaut/keycloak-extensions). + +Building can be done via `mvn clean install`. Please take a look at the aforementioned repository for a far more detailed introduction. + +This repository will most likely not keep up with the keycloak release cycle. You have been warned. \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..0bc31b9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + 4.0.0 + + de.scimeda.keycloak + last-login-event-listener + 0.0.1-SNAPSHOT + jar + + last-login-event-listener + Keycloak event listener to store last login date + + UTF-8 + UTF-8 + + 1.8 + 1.8 + + + 12.0.1 + + 3.8.1 + + 2.0.2.Final + + + + + org.keycloak + keycloak-server-spi + ${keycloak.version} + provided + + + org.keycloak + keycloak-server-spi-private + ${keycloak.version} + provided + + + org.keycloak + keycloak-services + ${keycloak.version} + provided + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + + org.wildfly.plugins + wildfly-maven-plugin + ${maven-wildfly-plugin.version} + + false + + + + + diff --git a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java new file mode 100644 index 0000000..933f3c5 --- /dev/null +++ b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProvider.java @@ -0,0 +1,55 @@ +package de.scimeda.keycloak.events.logging; + +import org.jboss.logging.Logger; +import org.keycloak.events.Event; +import org.keycloak.events.EventListenerProvider; +import org.keycloak.events.EventType; +import org.keycloak.events.admin.AdminEvent; +import org.keycloak.models.KeycloakSession; +import org.keycloak.models.RealmModel; +import org.keycloak.models.RealmProvider; +import org.keycloak.models.UserModel; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class LastLoginEventListenerProvider implements EventListenerProvider { + + private static final Logger log = Logger.getLogger(LastLoginEventListenerProvider.class); + + private final KeycloakSession session; + private final RealmProvider model; + + public LastLoginEventListenerProvider(KeycloakSession session) { + this.session = session; + this.model = session.realms(); + } + + @Override + public void onEvent(Event event) { + // log.infof("## NEW %s EVENT", event.getType()); + if (EventType.LOGIN.equals(event.getType())) { + RealmModel realm = this.model.getRealm(event.getRealmId()); + UserModel user = this.session.users().getUserById(event.getUserId(), realm); + + if (user != null) { + log.info("Updating last login status for user: " + event.getUserId()); + + // Use current server time for login event + LocalDateTime loginTime = LocalDateTime.now(); + String loginTimeS = DateTimeFormatter.ISO_DATE_TIME.format(loginTime); + user.setSingleAttribute("last-login", loginTimeS); + } + } + } + + @Override + public void onEvent(AdminEvent adminEvent, boolean b) { + } + + @Override + public void close() { + // Nothing to close + } + +} \ No newline at end of file diff --git a/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProviderFactory.java b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProviderFactory.java new file mode 100644 index 0000000..0cefd0a --- /dev/null +++ b/src/main/java/de/scimeda/keycloak/events/logging/LastLoginEventListenerProviderFactory.java @@ -0,0 +1,36 @@ +package de.scimeda.keycloak.events.logging; + +import org.keycloak.Config; +import org.keycloak.events.EventListenerProviderFactory; +import org.keycloak.models.KeycloakSession; +import org.keycloak.models.KeycloakSessionFactory; + + +public class LastLoginEventListenerProviderFactory implements EventListenerProviderFactory { + + @Override + public LastLoginEventListenerProvider create(KeycloakSession keycloakSession) { + return new LastLoginEventListenerProvider(keycloakSession); + } + + @Override + public void init(Config.Scope scope) { + // + } + + @Override + public void postInit(KeycloakSessionFactory keycloakSessionFactory) { + // + } + + @Override + public void close() { + // + } + + @Override + public String getId() { + return "last_login"; + } + +} diff --git a/src/main/resources/META-INF/jboss-deployment-structure.xml b/src/main/resources/META-INF/jboss-deployment-structure.xml new file mode 100644 index 0000000..b3bf009 --- /dev/null +++ b/src/main/resources/META-INF/jboss-deployment-structure.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory b/src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory new file mode 100644 index 0000000..32a3563 --- /dev/null +++ b/src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory @@ -0,0 +1 @@ +de.scimeda.keycloak.events.logging.LastLoginEventListenerProviderFactory