From 5e54eee7241195b8d114ffbc4f0f40a8f79c8b0f Mon Sep 17 00:00:00 2001 From: Jonathan Mendez Date: Mon, 30 Mar 2026 12:39:49 -0600 Subject: [PATCH 1/3] Make AuthenticationType an enum --- .../git/utils/AuthConfiguratorFactory.java | 4 ++-- .../commons/git/utils/AuthenticationType.java | 23 +++++++++++-------- .../TypeBasedAuthConfiguratorBuilder.java | 6 ++--- .../TypeBasedAuthConfiguratorBuilderTest.java | 4 ++-- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/git/src/main/java/org/craftercms/commons/git/utils/AuthConfiguratorFactory.java b/git/src/main/java/org/craftercms/commons/git/utils/AuthConfiguratorFactory.java index edfa3722..33bae904 100644 --- a/git/src/main/java/org/craftercms/commons/git/utils/AuthConfiguratorFactory.java +++ b/git/src/main/java/org/craftercms/commons/git/utils/AuthConfiguratorFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. + * Copyright (C) 2007-2026 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by @@ -47,7 +47,7 @@ public UrlBasedAuthConfiguratorBuilder forUrl(String url) { * @param type the auth type * @return the builder */ - public TypeBasedAuthConfiguratorBuilder forType(String type) { + public TypeBasedAuthConfiguratorBuilder forType(AuthenticationType type) { return new TypeBasedAuthConfiguratorBuilder(sshConfig, type); } diff --git a/git/src/main/java/org/craftercms/commons/git/utils/AuthenticationType.java b/git/src/main/java/org/craftercms/commons/git/utils/AuthenticationType.java index b05993d5..36146752 100644 --- a/git/src/main/java/org/craftercms/commons/git/utils/AuthenticationType.java +++ b/git/src/main/java/org/craftercms/commons/git/utils/AuthenticationType.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. + * Copyright (C) 2007-2026 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by @@ -16,19 +16,24 @@ package org.craftercms.commons.git.utils; /** - * Constants for git authentication types + * Git authentication types * * @author joseross * @since 4.0.0 */ -public interface AuthenticationType { +public enum AuthenticationType { + NONE("none"), + BASIC("basic"), + TOKEN("token"), + PRIVATE_KEY("key"); - String NONE = "none"; + private final String value; - String BASIC = "basic"; - - String TOKEN = "token"; - - String PRIVATE_KEY = "key"; + AuthenticationType(String value) { + this.value = value; + } + public String getValue() { + return value; + } } diff --git a/git/src/main/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilder.java b/git/src/main/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilder.java index 15fde939..536b813b 100644 --- a/git/src/main/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilder.java +++ b/git/src/main/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. + * Copyright (C) 2007-2026 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by @@ -37,9 +37,9 @@ public class TypeBasedAuthConfiguratorBuilder extends AbstractAuthConfiguratorBu /** * The authentication type to use */ - protected String authType; + protected AuthenticationType authType; - public TypeBasedAuthConfiguratorBuilder(File sshConfig, String authType) { + public TypeBasedAuthConfiguratorBuilder(File sshConfig, AuthenticationType authType) { super(sshConfig); this.authType = authType; } diff --git a/git/src/test/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilderTest.java b/git/src/test/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilderTest.java index 18ad6f0f..05f5001e 100644 --- a/git/src/test/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilderTest.java +++ b/git/src/test/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. + * Copyright (C) 2007-2026 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by @@ -27,7 +27,7 @@ */ public class TypeBasedAuthConfiguratorBuilderTest extends AbstractAuthConfiguratorBuilderTest { - protected TypeBasedAuthConfiguratorBuilder createBuilder(String type) { + protected TypeBasedAuthConfiguratorBuilder createBuilder(AuthenticationType type) { // The config path is not important for this test return new TypeBasedAuthConfiguratorBuilder(new File("."), type); } From bb3ea885565e06e52db11167e2073fcb938e6cc3 Mon Sep 17 00:00:00 2001 From: Jonathan Mendez Date: Mon, 30 Mar 2026 14:13:27 -0600 Subject: [PATCH 2/3] CaseInsensitiveEnumDeserializer --- .../CaseInsensitiveEnumDeserializer.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 utilities/src/main/java/org/craftercms/commons/jackson/CaseInsensitiveEnumDeserializer.java diff --git a/utilities/src/main/java/org/craftercms/commons/jackson/CaseInsensitiveEnumDeserializer.java b/utilities/src/main/java/org/craftercms/commons/jackson/CaseInsensitiveEnumDeserializer.java new file mode 100644 index 00000000..77fd4f18 --- /dev/null +++ b/utilities/src/main/java/org/craftercms/commons/jackson/CaseInsensitiveEnumDeserializer.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2007-2026 Crafter Software Corporation. All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.craftercms.commons.jackson; + +import com.fasterxml.jackson.core.JacksonException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.deser.ContextualDeserializer; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; + +import java.io.IOException; + +import static java.lang.String.format; + +/** + * Custom Jackson deserializer for enums that allows case-insensitive deserialization. + * This is useful for cases where the JSON input may not match the exact case of the enum constants. + */ +public class CaseInsensitiveEnumDeserializer extends JsonDeserializer implements ContextualDeserializer { + private JavaType fieldType; + + public CaseInsensitiveEnumDeserializer() { + } + + private CaseInsensitiveEnumDeserializer(JavaType fieldType) { + this.fieldType = fieldType; + } + + @Override + public JsonDeserializer createContextual(DeserializationContext ctxt, BeanProperty property) + throws JsonMappingException { + // 'property' can be null if the deserializer is for a root-level value, not a bean property. + if (property != null) { + // Get the declared type of the field + JavaType type = property.getType(); + return new CaseInsensitiveEnumDeserializer(type); + } + return this; + } + + @Override + public Enum deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException { + String value = p.getText(); + Class> enumClass = (Class>) fieldType.getRawClass(); + for (Enum enumConstant : enumClass.getEnumConstants()) { + if (enumConstant.name().equalsIgnoreCase(value)) { + return enumConstant; + } + } + throw new InvalidFormatException(p, format("Unable to deserialize value '%s' to enum %s", value, + fieldType.getRawClass().getName()), value, enumClass); + } +} From 01f0826ad70758181c021c39f6b3f011561040f1 Mon Sep 17 00:00:00 2001 From: Jonathan Mendez Date: Wed, 1 Apr 2026 12:15:40 -0600 Subject: [PATCH 3/3] AuthenticationType lowercase --- .../commons/git/utils/AuthenticationType.java | 18 ++++-------------- .../TypeBasedAuthConfiguratorBuilder.java | 8 ++++---- .../TypeBasedAuthConfiguratorBuilderTest.java | 10 +++++----- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/git/src/main/java/org/craftercms/commons/git/utils/AuthenticationType.java b/git/src/main/java/org/craftercms/commons/git/utils/AuthenticationType.java index 36146752..71a97fab 100644 --- a/git/src/main/java/org/craftercms/commons/git/utils/AuthenticationType.java +++ b/git/src/main/java/org/craftercms/commons/git/utils/AuthenticationType.java @@ -22,18 +22,8 @@ * @since 4.0.0 */ public enum AuthenticationType { - NONE("none"), - BASIC("basic"), - TOKEN("token"), - PRIVATE_KEY("key"); - - private final String value; - - AuthenticationType(String value) { - this.value = value; - } - - public String getValue() { - return value; - } + none, + basic, + token, + private_key } diff --git a/git/src/main/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilder.java b/git/src/main/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilder.java index 536b813b..2abf3997 100644 --- a/git/src/main/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilder.java +++ b/git/src/main/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilder.java @@ -47,22 +47,22 @@ public TypeBasedAuthConfiguratorBuilder(File sshConfig, AuthenticationType authT @Override public GitAuthenticationConfigurator build() { switch (authType) { - case AuthenticationType.NONE: + case AuthenticationType.none: logger.debug("No authentication will be used"); return new NoopAuthConfigurator(); - case AuthenticationType.BASIC: + case AuthenticationType.basic: if (isEmpty(username) && isEmpty(password)) { throw new IllegalStateException("basic auth requires a username or password"); } logger.debug("Username/password authentication will be used"); return new BasicUsernamePasswordAuthConfigurator(username, password); - case AuthenticationType.TOKEN: + case AuthenticationType.token: if (isEmpty(username)) { throw new IllegalStateException("token auth requires a username"); } logger.debug("Token authentication will be used"); return new BasicUsernamePasswordAuthConfigurator(username, StringUtils.EMPTY); - case AuthenticationType.PRIVATE_KEY: + case AuthenticationType.private_key: logger.debug("SSH private key authentication will be used"); return new SshPrivateKeyAuthConfigurator(sshConfig, privateKeyPath, privateKeyPassphrase); default: diff --git a/git/src/test/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilderTest.java b/git/src/test/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilderTest.java index 05f5001e..4bf7b64a 100644 --- a/git/src/test/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilderTest.java +++ b/git/src/test/java/org/craftercms/commons/git/utils/TypeBasedAuthConfiguratorBuilderTest.java @@ -34,31 +34,31 @@ protected TypeBasedAuthConfiguratorBuilder createBuilder(AuthenticationType type @Test public void testNoAuth() { - verify(createBuilder(AuthenticationType.NONE), NoopAuthConfigurator.class); + verify(createBuilder(AuthenticationType.none), NoopAuthConfigurator.class); } @Test public void testBasicAuth() { - verify(createBuilder(AuthenticationType.BASIC) + verify(createBuilder(AuthenticationType.basic) .withUsername("joe") .withPassword("secret"), BasicUsernamePasswordAuthConfigurator.class); } @Test public void testTokenAuth() { - verify(createBuilder(AuthenticationType.TOKEN) + verify(createBuilder(AuthenticationType.token) .withUsername("token"), BasicUsernamePasswordAuthConfigurator.class); } @Test public void testPrivateKeyAuth() { - verify(createBuilder(AuthenticationType.PRIVATE_KEY) + verify(createBuilder(AuthenticationType.private_key) .withPrivateKeyPath("/some/key"), SshPrivateKeyAuthConfigurator.class); } @Test public void testPrivateKeyAuthWithPass() { - verify(createBuilder(AuthenticationType.PRIVATE_KEY) + verify(createBuilder(AuthenticationType.private_key) .withPrivateKeyPath("/some/key") .withPrivateKeyPassphrase("secret"), SshPrivateKeyAuthConfigurator.class); }