mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-20 23:57:34 +01:00
#1014 Use ConfigMe improvements to create custom Enum set property
This commit is contained in:
parent
9ce680f56e
commit
d717f75bb4
@ -6,7 +6,6 @@ import fr.xephi.authme.events.PasswordEncryptionEvent;
|
|||||||
import fr.xephi.authme.initialization.Reloadable;
|
import fr.xephi.authme.initialization.Reloadable;
|
||||||
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
||||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||||
import fr.xephi.authme.settings.EnumSetProperty;
|
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
@ -42,8 +41,7 @@ public class PasswordSecurity implements Reloadable {
|
|||||||
@Override
|
@Override
|
||||||
public void reload() {
|
public void reload() {
|
||||||
this.algorithm = settings.getProperty(SecuritySettings.PASSWORD_HASH);
|
this.algorithm = settings.getProperty(SecuritySettings.PASSWORD_HASH);
|
||||||
// TODO #1014: Need to cast to specific type because ConfigMe ignores fields of child Property types
|
this.legacyAlgorithms = settings.getProperty(SecuritySettings.LEGACY_HASHES);
|
||||||
this.legacyAlgorithms = ((EnumSetProperty<HashAlgorithm>) SecuritySettings.LEGACY_HASHES).asEnumSet(settings);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,38 +1,38 @@
|
|||||||
package fr.xephi.authme.settings;
|
package fr.xephi.authme.settings;
|
||||||
|
|
||||||
import ch.jalu.configme.SettingsManager;
|
import ch.jalu.configme.properties.Property;
|
||||||
import ch.jalu.configme.properties.StringListProperty;
|
import ch.jalu.configme.resource.PropertyResource;
|
||||||
|
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static com.google.common.collect.Sets.newHashSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Property whose value is a set of entries of a given enum.
|
* Property whose value is a set of entries of a given enum.
|
||||||
*/
|
*/
|
||||||
// TODO #1014: This property type currently extends StringListProperty with a dedicated method to convert the values
|
public class EnumSetProperty<E extends Enum<E>> extends Property<Set<E>> {
|
||||||
// into a Set of the selected enum due to multiple issues on ConfigMe's side.
|
|
||||||
public class EnumSetProperty<E extends Enum<E>> extends StringListProperty {
|
|
||||||
|
|
||||||
private final Class<E> enumClass;
|
private final Class<E> enumClass;
|
||||||
|
|
||||||
public EnumSetProperty(Class<E> enumClass, String path, String... values) {
|
@SafeVarargs
|
||||||
super(path, values);
|
public EnumSetProperty(Class<E> enumClass, String path, E... values) {
|
||||||
|
super(path, newHashSet(values));
|
||||||
this.enumClass = enumClass;
|
this.enumClass = enumClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Returns the value as a set of enum entries.
|
protected Set<E> getFromResource(PropertyResource resource) {
|
||||||
*
|
List<?> elements = resource.getList(getPath());
|
||||||
* @param settings the settings manager to look up the raw value with
|
if (elements != null) {
|
||||||
* @return the property's value as mapped enum entries
|
return elements.stream()
|
||||||
*/
|
.map(val -> toEnum(String.valueOf(val)))
|
||||||
public Set<E> asEnumSet(SettingsManager settings) {
|
.filter(e -> e != null)
|
||||||
List<String> entries = settings.getProperty(this);
|
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||||
return entries.stream()
|
}
|
||||||
.map(str -> toEnum(str))
|
return null;
|
||||||
.filter(e -> e != null)
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private E toEnum(String str) {
|
private E toEnum(String str) {
|
||||||
|
@ -46,8 +46,7 @@ public final class CommandSettingsHolder implements SettingsHolder {
|
|||||||
"Supported command events: onLogin, onJoin, onRegister"
|
"Supported command events: onLogin, onJoin, onRegister"
|
||||||
};
|
};
|
||||||
Map<String, String[]> commentMap = new HashMap<>();
|
Map<String, String[]> commentMap = new HashMap<>();
|
||||||
// TODO ConfigMe/#25 cannot set comments on the root ("")
|
commentMap.put("", comments);
|
||||||
commentMap.put("onLogin", comments);
|
|
||||||
return commentMap;
|
return commentMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import fr.xephi.authme.security.HashAlgorithm;
|
|||||||
import fr.xephi.authme.settings.EnumSetProperty;
|
import fr.xephi.authme.settings.EnumSetProperty;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseListProperty;
|
import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseListProperty;
|
||||||
import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
|
import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
|
||||||
@ -83,7 +84,7 @@ public class SecuritySettings implements SettingsHolder {
|
|||||||
"legacyHashes:",
|
"legacyHashes:",
|
||||||
"- 'SHA1'"
|
"- 'SHA1'"
|
||||||
})
|
})
|
||||||
public static final Property<List<String>> LEGACY_HASHES =
|
public static final Property<Set<HashAlgorithm>> LEGACY_HASHES =
|
||||||
new EnumSetProperty<>(HashAlgorithm.class, "settings.security.legacyHashes");
|
new EnumSetProperty<>(HashAlgorithm.class, "settings.security.legacyHashes");
|
||||||
|
|
||||||
@Comment("Number of rounds to use if passwordHash is set to PBKDF2. Default is 10000")
|
@Comment("Number of rounds to use if passwordHash is set to PBKDF2. Default is 10000")
|
||||||
|
@ -25,10 +25,8 @@ import org.mockito.junit.MockitoJUnitRunner;
|
|||||||
import org.mockito.stubbing.Answer;
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static com.google.common.collect.Lists.newArrayList;
|
|
||||||
import static com.google.common.collect.Sets.newHashSet;
|
import static com.google.common.collect.Sets.newHashSet;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.equalToIgnoringCase;
|
import static org.hamcrest.Matchers.equalToIgnoringCase;
|
||||||
@ -175,7 +173,7 @@ public class PasswordSecurityTest {
|
|||||||
given(method.comparePassword(clearTextPass, password, playerLowerCase)).willReturn(false);
|
given(method.comparePassword(clearTextPass, password, playerLowerCase)).willReturn(false);
|
||||||
given(method.computeHash(clearTextPass, playerLowerCase)).willReturn(newPassword);
|
given(method.computeHash(clearTextPass, playerLowerCase)).willReturn(newPassword);
|
||||||
initSettings(HashAlgorithm.MD5);
|
initSettings(HashAlgorithm.MD5);
|
||||||
given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(newArrayList(HashAlgorithm.BCRYPT.name()));
|
given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(newHashSet(HashAlgorithm.BCRYPT));
|
||||||
PasswordSecurity security = newPasswordSecurity();
|
PasswordSecurity security = newPasswordSecurity();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
@ -260,8 +258,8 @@ public class PasswordSecurityTest {
|
|||||||
initSettings(HashAlgorithm.BCRYPT);
|
initSettings(HashAlgorithm.BCRYPT);
|
||||||
PasswordSecurity passwordSecurity = newPasswordSecurity();
|
PasswordSecurity passwordSecurity = newPasswordSecurity();
|
||||||
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.MD5);
|
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.MD5);
|
||||||
List<String> legacyHashes = newArrayList(HashAlgorithm.CUSTOM.name(), HashAlgorithm.BCRYPT.name());
|
given(settings.getProperty(SecuritySettings.LEGACY_HASHES))
|
||||||
given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(legacyHashes);
|
.willReturn(newHashSet(HashAlgorithm.CUSTOM, HashAlgorithm.BCRYPT));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
passwordSecurity.reload();
|
passwordSecurity.reload();
|
||||||
@ -286,7 +284,7 @@ public class PasswordSecurityTest {
|
|||||||
|
|
||||||
private void initSettings(HashAlgorithm algorithm) {
|
private void initSettings(HashAlgorithm algorithm) {
|
||||||
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(algorithm);
|
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(algorithm);
|
||||||
given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(Collections.emptyList());
|
given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(Collections.emptySet());
|
||||||
given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8);
|
given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user