Skip to content

Commit

Permalink
Update date formats and remove deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
rooije committed May 17, 2024
1 parent 265f54b commit 4c6ab9e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void close() {

@Override
public String getDisplayType() {
return "Deltares Email Check";
return "Deltares Email Check (deprecated)";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void close() {

@Override
public String getDisplayType() {
return "Deltares Username Validate ";
return "Deltares Username Validate (deprecated)";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import org.keycloak.models.UserModel;
import org.keycloak.sessions.AuthenticationSessionModel;

import java.time.ZoneId;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Optional;
import java.util.TimeZone;
import java.util.stream.Stream;

import static java.time.LocalDateTime.now;

public class LoginStatsRecordingRequiredActionProvider implements RequiredActionProvider, RequiredActionFactory {

private static final Logger LOG = Logger.getLogger(LoginStatsRecordingRequiredActionProvider.class);
Expand All @@ -31,6 +31,12 @@ public class LoginStatsRecordingRequiredActionProvider implements RequiredActio
private static final String ONE = "1";

private static final LoginStatsRecordingRequiredActionProvider INSTANCE = new LoginStatsRecordingRequiredActionProvider();
private final SimpleDateFormat simpleDateFormat;

public LoginStatsRecordingRequiredActionProvider() {
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}

@Override
public void evaluateTriggers(RequiredActionContext context) {
Expand Down Expand Up @@ -85,15 +91,15 @@ private void recordLoginCount(String referrer, UserModel user) {

private void recordRecentLogin(String referrer, UserModel user) {
String key = referrer == null ? LOGIN_RECENT_LOGIN_DATE : LOGIN_RECENT_LOGIN_DATE + '.' + referrer;
user.setAttribute(key, Collections.singletonList(now(ZoneId.of("GMT")).toString()));
user.setAttribute(key, Collections.singletonList(simpleDateFormat.format(new Date(System.currentTimeMillis()))));
}

private void recordFirstLogin(String clientId, UserModel user) {
String key = clientId == null ? LOGIN_FIRST_LOGIN_DATE : LOGIN_FIRST_LOGIN_DATE + '.' + clientId;
Stream<String> stream = user.getAttributeStream(key);
final Optional<String> first = stream.findFirst();
if (first.isEmpty()) {
user.setAttribute(key, Collections.singletonList(now(ZoneId.of("GMT")).toString()));
user.setAttribute(key, Collections.singletonList(simpleDateFormat.format(new Date(System.currentTimeMillis()))));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
import org.keycloak.authentication.RequiredActionContext;
import org.keycloak.authentication.RequiredActionFactory;
import org.keycloak.authentication.RequiredActionProvider;
import org.keycloak.common.util.Time;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class TermsAndPrivacy implements RequiredActionProvider, RequiredActionFactory {

final SimpleDateFormat simpleDateFormat;

public TermsAndPrivacy() {
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}

public RequiredActionProvider create(KeycloakSession session) {
Expand Down Expand Up @@ -48,7 +55,7 @@ public void processAction(RequiredActionContext context) {
context.getUser().removeAttribute("terms_and_conditions");
context.failure();
} else {
context.getUser().setAttribute("terms_and_conditions", Collections.singletonList(Integer.toString(Time.currentTime())));
context.getUser().setAttribute("terms_and_conditions", Collections.singletonList(simpleDateFormat.format(new Date(System.currentTimeMillis()))));
context.success();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package nl.deltares.keycloak.provider.user;

import org.apache.commons.text.StringEscapeUtils;
import org.keycloak.events.EventType;
import org.jboss.logging.Logger;
import org.keycloak.events.Event;
import org.keycloak.events.EventListenerProvider;
Expand All @@ -21,7 +19,7 @@ public class UserEventListenerProvider implements EventListenerProvider {
private static final Logger logger = Logger.getLogger(UserEventListenerProvider.class);
private final KeycloakSession session;
private final RealmProvider model;
private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

public UserEventListenerProvider(KeycloakSession session) {
this.session = session;
Expand All @@ -31,11 +29,6 @@ public UserEventListenerProvider(KeycloakSession session) {

@Override
public void onEvent(Event event) {
if (event.getType() != EventType.REGISTER) {
if (event.getType() == EventType.UPDATE_PROFILE) {
escapeHtmlProfile(event);
}
} //escapeHtmlRegistration(event); does not work

}

Expand All @@ -62,37 +55,6 @@ private void removeUserFromGroups(UserModel updatedUser) {

}

private void escapeHtmlProfile(Event event) {

final Map<String, String> details = event.getDetails();

final UserModel updatedUser = session.users().getUserById(model.getRealm(event.getRealmId()), event.getUserId());

for (String key : details.keySet()) {
if (!key.startsWith("updated_")) continue;
final String updatedValue = details.get(key);

switch (key){
case "updated_first_name":
updatedUser.setFirstName(StringEscapeUtils.escapeHtml4(updatedValue));
break;
case "updated_last_name":
updatedUser.setLastName(StringEscapeUtils.escapeHtml4(updatedValue));
break;
default:
final String attributeKey = key.substring("updated_".length());
final List<String> escapedValue = Collections.singletonList(StringEscapeUtils.escapeHtml4(updatedValue));
updatedUser.setAttribute(attributeKey, escapedValue);
break;
}
}

if (updatedUser.getEmail() == null && details.containsKey("previous_email")){
//apparently needs to be reset when changes take place
updatedUser.setEmail(details.get("previous_email"));
}
}

private UserModel getUpdatedUser(AdminEvent event) {

RealmModel realm = model.getRealm(event.getRealmId());
Expand Down

0 comments on commit 4c6ab9e

Please sign in to comment.