Fix LoginFailureEntity protostream encoding

The field lastIPFailure can be null and needs a proto factory to set it
to null when missing.

Closes #30485

Signed-off-by: Pedro Ruivo <pruivo@redhat.com>
This commit is contained in:
Pedro Ruivo 2024-06-17 08:58:09 +01:00 committed by Alexander Schwartz
parent b5d7931d93
commit 66dd9e65b9
2 changed files with 16 additions and 25 deletions

View file

@ -86,9 +86,7 @@ public class InfinispanUserLoginFailureProvider implements UserLoginFailureProvi
log.tracef("addUserLoginFailure(%s, %s)%s", realm, userId, getShortStackTrace());
LoginFailureKey key = new LoginFailureKey(realm.getId(), userId);
LoginFailureEntity entity = new LoginFailureEntity();
entity.setRealmId(realm.getId());
entity.setUserId(userId);
LoginFailureEntity entity = new LoginFailureEntity(realm.getId(), userId);
SessionUpdateTask<LoginFailureEntity> createLoginFailureTask = Tasks.addIfAbsentSync();
loginFailuresTx.addTask(key, createLoginFailureTask, entity, UserSessionModel.SessionPersistenceState.PERSISTENT);
@ -129,7 +127,7 @@ public class InfinispanUserLoginFailureProvider implements UserLoginFailureProvi
.map(Mappers.loginFailureId())
.forEach(loginFailureKey -> {
// Remove loginFailure from remoteCache too. Use removeAsync for better perf
Future future = localCache.removeAsync(loginFailureKey);
Future<?> future = localCache.removeAsync(loginFailureKey);
futures.addTask(future);
});

View file

@ -17,19 +17,20 @@
package org.keycloak.models.sessions.infinispan.entities;
import java.util.Objects;
import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;
import org.infinispan.protostream.annotations.ProtoTypeId;
import org.keycloak.marshalling.Marshalling;
import java.util.Objects;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@ProtoTypeId(Marshalling.LOGIN_FAILURE_ENTITY)
public class LoginFailureEntity extends SessionEntity {
private String userId;
private final String userId;
private int failedLoginNotBefore;
private int numFailures;
@ -37,17 +38,20 @@ public class LoginFailureEntity extends SessionEntity {
private long lastFailure;
private String lastIPFailure;
public LoginFailureEntity() {
public LoginFailureEntity(String realmId, String userId) {
super(Objects.requireNonNull(realmId));
this.userId = Objects.requireNonNull(userId);
}
private LoginFailureEntity(String realmId, String userId, int failedLoginNotBefore, int numFailures, int numTemporaryLockouts, long lastFailure, String lastIPFailure) {
@ProtoFactory
LoginFailureEntity(String realmId, String userId, int failedLoginNotBefore, int numFailures, int numTemporaryLockouts, long lastFailure, String lastIPFailure) {
super(realmId);
this.userId = userId;
this.failedLoginNotBefore = failedLoginNotBefore;
this.numFailures = numFailures;
this.numTemporaryLockouts = numTemporaryLockouts;
this.lastFailure = lastFailure;
this.lastIPFailure = lastIPFailure;
this.lastIPFailure = Marshalling.emptyStringToNull(lastIPFailure);
}
@ProtoField(2)
@ -55,10 +59,6 @@ public class LoginFailureEntity extends SessionEntity {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@ProtoField(3)
public int getFailedLoginNotBefore() {
return failedLoginNotBefore;
@ -118,17 +118,10 @@ public class LoginFailureEntity extends SessionEntity {
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof LoginFailureEntity that)) {
return false;
}
if (!Objects.equals(userId, that.userId)) {
return false;
}
return getRealmId() != null ? getRealmId().equals(that.getRealmId()) : that.getRealmId() == null;
if (this == o) return true;
if (!(o instanceof LoginFailureEntity that)) return false;
return Objects.equals(userId, that.userId) &&
Objects.equals(getRealmId(), that.getRealmId());
}
@Override