Remove external Collection utility class for WebAuthn

Closes #10034
This commit is contained in:
Martin Bartoš 2022-02-07 12:48:43 +01:00 committed by Marek Posolda
parent 82300ba731
commit 75c7491b85
5 changed files with 43 additions and 6 deletions

View file

@ -55,4 +55,12 @@ public class CollectionUtil {
return true; return true;
} }
public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}
public static boolean isNotEmpty(Collection<?> collection) {
return !isEmpty(collection);
}
} }

View file

@ -4,6 +4,12 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class CollectionUtilTest { public class CollectionUtilTest {
@ -39,4 +45,27 @@ public class CollectionUtilTest {
final String retval = CollectionUtil.join(strings, ","); final String retval = CollectionUtil.join(strings, ",");
Assert.assertEquals("foo,bar", retval); Assert.assertEquals("foo,bar", retval);
} }
@Test
public void testEmptyCollection() {
List<String> list = new ArrayList<>();
assertThat(CollectionUtil.isEmpty(list), is(true));
assertThat(CollectionUtil.isNotEmpty(list), is(false));
list.add("something");
assertThat(CollectionUtil.isEmpty(list), is(false));
assertThat(CollectionUtil.isNotEmpty(list), is(true));
Set<Object> set = new HashSet<>();
assertThat(CollectionUtil.isEmpty(set), is(true));
assertThat(CollectionUtil.isNotEmpty(set), is(false));
set.add("something");
assertThat(CollectionUtil.isEmpty(set), is(false));
assertThat(CollectionUtil.isNotEmpty(set), is(true));
}
} }

View file

@ -31,7 +31,6 @@ import javax.ws.rs.core.Response;
import com.webauthn4j.WebAuthnRegistrationManager; import com.webauthn4j.WebAuthnRegistrationManager;
import com.webauthn4j.data.AuthenticatorTransport; import com.webauthn4j.data.AuthenticatorTransport;
import org.apache.commons.collections4.CollectionUtils;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.jboss.resteasy.spi.HttpRequest; import org.jboss.resteasy.spi.HttpRequest;
import org.keycloak.WebAuthnConstants; import org.keycloak.WebAuthnConstants;
@ -40,6 +39,7 @@ import org.keycloak.authentication.InitiatedActionSupport;
import org.keycloak.authentication.RequiredActionContext; import org.keycloak.authentication.RequiredActionContext;
import org.keycloak.authentication.RequiredActionProvider; import org.keycloak.authentication.RequiredActionProvider;
import org.keycloak.common.util.Base64Url; import org.keycloak.common.util.Base64Url;
import org.keycloak.common.util.CollectionUtil;
import org.keycloak.common.util.UriUtils; import org.keycloak.common.util.UriUtils;
import org.keycloak.credential.CredentialProvider; import org.keycloak.credential.CredentialProvider;
import org.keycloak.credential.WebAuthnCredentialModelInput; import org.keycloak.credential.WebAuthnCredentialModelInput;
@ -327,7 +327,7 @@ public class WebAuthnRegister implements RequiredActionProvider, CredentialRegis
logger.debugv("aaguid = {0}", attestedCredentialData.getAaguid().toString()); logger.debugv("aaguid = {0}", attestedCredentialData.getAaguid().toString());
logger.debugv("attestation format = {0}", attestationStatement.getFormat()); logger.debugv("attestation format = {0}", attestationStatement.getFormat());
if (CollectionUtils.isNotEmpty(transports)) { if (CollectionUtil.isNotEmpty(transports)) {
logger.debugv("transports = [{0}]", transports.stream() logger.debugv("transports = [{0}]", transports.stream()
.map(AuthenticatorTransport::getValue) .map(AuthenticatorTransport::getValue)
.collect(Collectors.joining(","))); .collect(Collectors.joining(",")));

View file

@ -16,7 +16,6 @@
package org.keycloak.credential; package org.keycloak.credential;
import org.apache.commons.collections4.CollectionUtils;
import org.keycloak.common.util.Base64; import org.keycloak.common.util.Base64;
import com.webauthn4j.data.AuthenticationParameters; import com.webauthn4j.data.AuthenticationParameters;
@ -25,6 +24,7 @@ import com.webauthn4j.data.AuthenticatorTransport;
import com.webauthn4j.data.attestation.authenticator.AttestedCredentialData; import com.webauthn4j.data.attestation.authenticator.AttestedCredentialData;
import com.webauthn4j.data.attestation.authenticator.COSEKey; import com.webauthn4j.data.attestation.authenticator.COSEKey;
import com.webauthn4j.data.attestation.statement.AttestationStatement; import com.webauthn4j.data.attestation.statement.AttestationStatement;
import org.keycloak.common.util.CollectionUtil;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
@ -171,7 +171,7 @@ public class WebAuthnCredentialModelInput implements CredentialInput {
.append(Base64.encodeBytes(authenticationRequest.getCredentialId())) .append(Base64.encodeBytes(authenticationRequest.getCredentialId()))
.append(","); .append(",");
} }
if (CollectionUtils.isNotEmpty(getTransports())) { if (CollectionUtil.isNotEmpty(getTransports())) {
final String transportsString = getTransports().stream() final String transportsString = getTransports().stream()
.map(AuthenticatorTransport::getValue) .map(AuthenticatorTransport::getValue)
.collect(Collectors.joining(",")); .collect(Collectors.joining(","));

View file

@ -16,8 +16,8 @@
package org.keycloak.forms.login.freemarker.model; package org.keycloak.forms.login.freemarker.model;
import com.webauthn4j.data.AuthenticatorTransport; import com.webauthn4j.data.AuthenticatorTransport;
import org.apache.commons.collections4.CollectionUtils;
import org.keycloak.common.util.Base64Url; import org.keycloak.common.util.Base64Url;
import org.keycloak.common.util.CollectionUtil;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
@ -117,7 +117,7 @@ public class WebAuthnAuthenticatorsBean {
* @return TransportBean * @return TransportBean
*/ */
public static TransportsBean convertFromSet(Set<String> transports) { public static TransportsBean convertFromSet(Set<String> transports) {
if (CollectionUtils.isEmpty(transports)) { if (CollectionUtil.isEmpty(transports)) {
return new TransportsBean(Transport.UNKNOWN); return new TransportsBean(Transport.UNKNOWN);
} }