From d2a28bdaedc0183e9799358edf5e81bc62b4079c Mon Sep 17 00:00:00 2001 From: ljacqu Date: Tue, 22 Nov 2016 22:02:34 +0100 Subject: [PATCH] #850 Fix export of legacy hashes property - Ugly workaround due to #1014: need to have EnumSetProperty extend from StringListProperty type so that it is exported in a proper manner (as a string list). To get an enum Set we need to call a dedicated method on EnumSetProperty for the time being. --- .../authme/security/PasswordSecurity.java | 4 +- .../authme/settings/EnumSetProperty.java | 37 ++++++++++--------- .../settings/properties/SecuritySettings.java | 5 +-- .../authme/security/PasswordSecurityTest.java | 9 +++-- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/main/java/fr/xephi/authme/security/PasswordSecurity.java b/src/main/java/fr/xephi/authme/security/PasswordSecurity.java index 8549b388b..246070586 100644 --- a/src/main/java/fr/xephi/authme/security/PasswordSecurity.java +++ b/src/main/java/fr/xephi/authme/security/PasswordSecurity.java @@ -6,6 +6,7 @@ import fr.xephi.authme.events.PasswordEncryptionEvent; import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.security.crypts.EncryptionMethod; import fr.xephi.authme.security.crypts.HashedPassword; +import fr.xephi.authme.settings.EnumSetProperty; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.SecuritySettings; import org.bukkit.plugin.PluginManager; @@ -41,7 +42,8 @@ public class PasswordSecurity implements Reloadable { @Override public void reload() { this.algorithm = settings.getProperty(SecuritySettings.PASSWORD_HASH); - this.legacyAlgorithms = settings.getProperty(SecuritySettings.LEGACY_HASHES); + // TODO #1014: Need to cast to specific type because ConfigMe ignores fields of child Property types + this.legacyAlgorithms = ((EnumSetProperty) SecuritySettings.LEGACY_HASHES).asEnumSet(settings); } /** diff --git a/src/main/java/fr/xephi/authme/settings/EnumSetProperty.java b/src/main/java/fr/xephi/authme/settings/EnumSetProperty.java index 4ca284d72..8b1fa7893 100644 --- a/src/main/java/fr/xephi/authme/settings/EnumSetProperty.java +++ b/src/main/java/fr/xephi/authme/settings/EnumSetProperty.java @@ -1,35 +1,38 @@ package fr.xephi.authme.settings; -import com.github.authme.configme.properties.Property; -import com.github.authme.configme.resource.PropertyResource; +import com.github.authme.configme.SettingsManager; +import com.github.authme.configme.properties.StringListProperty; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; /** * Property whose value is a set of entries of a given enum. */ -// TODO https://github.com/AuthMe/ConfigMe/issues/27: Should be a Property of Set type -public class EnumSetProperty> extends Property> { +// TODO #1014: This property type currently extends StringListProperty with a dedicated method to convert the values +// into a Set of the selected enum due to multiple issues on ConfigMe's side. +public class EnumSetProperty> extends StringListProperty { private final Class enumClass; - public EnumSetProperty(Class enumClass, String path, List defaultValue) { - super(path, defaultValue); + public EnumSetProperty(Class enumClass, String path, String... values) { + super(path, values); this.enumClass = enumClass; } - @Override - protected List getFromResource(PropertyResource resource) { - List elements = resource.getList(getPath()); - if (elements != null) { - return elements.stream() - .map(val -> toEnum(String.valueOf(val))) - .filter(e -> e != null) - .distinct() - .collect(Collectors.toList()); - } - return null; + /** + * Returns the value as a set of enum entries. + * + * @param settings the settings manager to look up the raw value with + * @return the property's value as mapped enum entries + */ + public Set asEnumSet(SettingsManager settings) { + List entries = settings.getProperty(this); + return entries.stream() + .map(str -> toEnum(str)) + .filter(e -> e != null) + .collect(Collectors.toSet()); } private E toEnum(String str) { diff --git a/src/main/java/fr/xephi/authme/settings/properties/SecuritySettings.java b/src/main/java/fr/xephi/authme/settings/properties/SecuritySettings.java index e8035491b..3711715d0 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/SecuritySettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/SecuritySettings.java @@ -6,7 +6,6 @@ import com.github.authme.configme.properties.Property; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.settings.EnumSetProperty; -import java.util.Collections; import java.util.List; import static com.github.authme.configme.properties.PropertyInitializer.newLowercaseListProperty; @@ -83,8 +82,8 @@ public class SecuritySettings implements SettingsHolder { "legacyHashes:", "- 'SHA1'" }) - public static final Property> LEGACY_HASHES = - new EnumSetProperty<>(HashAlgorithm.class, "settings.security.legacyHashes", Collections.emptyList()); + public static final Property> LEGACY_HASHES = + new EnumSetProperty<>(HashAlgorithm.class, "settings.security.legacyHashes"); @Comment({"Prevent unsafe passwords from being used; put them in lowercase!", "You should always set 'help' as unsafePassword due to possible conflicts.", diff --git a/src/test/java/fr/xephi/authme/security/PasswordSecurityTest.java b/src/test/java/fr/xephi/authme/security/PasswordSecurityTest.java index ec206c869..e1c30f43b 100644 --- a/src/test/java/fr/xephi/authme/security/PasswordSecurityTest.java +++ b/src/test/java/fr/xephi/authme/security/PasswordSecurityTest.java @@ -26,8 +26,10 @@ import org.mockito.stubbing.Answer; import java.util.Collections; import java.util.List; +import java.util.Set; import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Sets.newHashSet; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalToIgnoringCase; import static org.junit.Assert.assertThat; @@ -173,7 +175,7 @@ public class PasswordSecurityTest { given(method.comparePassword(clearTextPass, password, playerLowerCase)).willReturn(false); given(method.computeHash(clearTextPass, playerLowerCase)).willReturn(newPassword); initSettings(HashAlgorithm.MD5); - given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(newArrayList(HashAlgorithm.BCRYPT)); + given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(newArrayList(HashAlgorithm.BCRYPT.name())); PasswordSecurity security = newPasswordSecurity(); // when @@ -258,7 +260,7 @@ public class PasswordSecurityTest { initSettings(HashAlgorithm.BCRYPT); PasswordSecurity passwordSecurity = newPasswordSecurity(); given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.MD5); - List legacyHashes = newArrayList(HashAlgorithm.CUSTOM, HashAlgorithm.BCRYPT); + List legacyHashes = newArrayList(HashAlgorithm.CUSTOM.name(), HashAlgorithm.BCRYPT.name()); given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(legacyHashes); // when @@ -267,8 +269,9 @@ public class PasswordSecurityTest { // then assertThat(ReflectionTestUtils.getFieldValue(PasswordSecurity.class, passwordSecurity, "algorithm"), equalTo(HashAlgorithm.MD5)); + Set legacyHashesSet = newHashSet(HashAlgorithm.CUSTOM, HashAlgorithm.BCRYPT); assertThat(ReflectionTestUtils.getFieldValue(PasswordSecurity.class, passwordSecurity, "legacyAlgorithms"), - equalTo(legacyHashes)); + equalTo(legacyHashesSet)); } private PasswordSecurity newPasswordSecurity() {