Upgrade dependencies to keycloak 20.0.2

This commit is contained in:
Thore 2023-01-10 03:25:50 +01:00 committed by Thore
parent 79e5c74897
commit 1095e77006
5 changed files with 28 additions and 38 deletions

View file

@ -8,10 +8,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 11
java-version: 17
distribution: temurin
- uses: gradle/wrapper-validation-action@v1

View file

@ -22,7 +22,7 @@ version = '0.0.1-SNAPSHOT'
description = 'last-login-event-listener'
ext {
keycloakVersion = "17.0.0"
keycloakVersion = "20.0.2"
}
@ -35,10 +35,10 @@ dependencies {
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
languageVersion = JavaLanguageVersion.of(17)
}
}
tasks.withType(JavaCompile) {
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
}

View file

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View file

@ -10,11 +10,11 @@ import org.keycloak.models.RealmModel;
import org.keycloak.models.RealmProvider;
import org.keycloak.models.UserModel;
import java.util.List;
import java.util.Map;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
public class LastLoginEventListenerProvider implements EventListenerProvider {
@ -31,27 +31,29 @@ public class LastLoginEventListenerProvider implements EventListenerProvider {
@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(realm, event.getUserId());
if (!EventType.LOGIN.equals(event.getType())) {
return;
}
RealmModel realm = this.model.getRealm(event.getRealmId());
UserModel user = this.session.users().getUserById(realm, event.getUserId());
if (user != null) {
log.info("Updating last login status for user: " + event.getUserId());
if (user == null) {
return;
}
log.info("Updating last login status for user: " + event.getUserId());
Map<String, List<String>> userAttrs = user.getAttributes();
if (userAttrs.containsKey("last-login")) {
List<String> userLastLogin = userAttrs.get("last-login");
if (userLastLogin != null && !userLastLogin.isEmpty()) {
user.setSingleAttribute("prior-login", userLastLogin.get(0));
}
}
// Use current server time for login event
OffsetDateTime loginTime = OffsetDateTime.now(ZoneOffset.UTC);
String loginTimeS = DateTimeFormatter.ISO_DATE_TIME.format(loginTime);
user.setSingleAttribute("last-login", loginTimeS);
Map<String, List<String>> userAttrs = user.getAttributes();
if (userAttrs.containsKey("last-login")) {
List<String> userLastLogin = userAttrs.get("last-login");
if (userLastLogin != null && !userLastLogin.isEmpty()) {
user.setSingleAttribute("prior-login", userLastLogin.get(0));
}
}
// Use current server time for login event
OffsetDateTime loginTime = OffsetDateTime.now(ZoneOffset.UTC);
String loginTimeS = DateTimeFormatter.ISO_DATE_TIME.format(loginTime);
user.setSingleAttribute("last-login", loginTimeS);
}
@Override

View file

@ -1,12 +0,0 @@
<?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>