Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,19 +16,14 @@
package org.craftercms.commons.git.utils;

/**
* Constants for git authentication types
* Git authentication types
*
* @author joseross
* @since 4.0.0
*/
public interface AuthenticationType {

String NONE = "none";

String BASIC = "basic";

String TOKEN = "token";

String PRIVATE_KEY = "key";

public enum AuthenticationType {
none,
basic,
token,
private_key
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -37,32 +37,32 @@ 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;
}

@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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,38 +27,38 @@
*/
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);
}

@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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Enum<?>> enumClass = (Class<Enum<?>>) 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);
}
}