Add event listener to separate repo

This commit is contained in:
Thore 2021-01-12 18:48:42 +01:00
commit 4fd7a56a4e
7 changed files with 230 additions and 0 deletions

52
.gitignore vendored Normal file
View file

@ -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

9
README.md Normal file
View file

@ -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.

65
pom.xml Normal file
View file

@ -0,0 +1,65 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.scimeda.keycloak</groupId>
<artifactId>last-login-event-listener</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>last-login-event-listener</name>
<description>Keycloak event listener to store last login date</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- https://mvnrepository.com/artifact/org.keycloak/keycloak-parent -->
<keycloak.version>12.0.1</keycloak.version>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<!-- https://mvnrepository.com/artifact/org.wildfly.plugins/wildfly-maven-plugin -->
<maven-wildfly-plugin.version>2.0.2.Final</maven-wildfly-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
<version>${keycloak.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi-private</artifactId>
<version>${keycloak.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services</artifactId>
<version>${keycloak.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${maven-wildfly-plugin.version}</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -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
}
}

View file

@ -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";
}
}

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="org.keycloak.keycloak-core" export="true" />
<module name="org.keycloak.keycloak-server-spi" export="true" />
<module name="org.keycloak.keycloak-server-spi-private" export="true" />
<module name="org.keycloak.keycloak-services" export="true" />
<module name="org.jboss.logging" export="true" />
</dependencies>
</deployment>
</jboss-deployment-structure>

View file

@ -0,0 +1 @@
de.scimeda.keycloak.events.logging.LastLoginEventListenerProviderFactory