Always specify Locale on toLowerCase and toUpperCase usages, fixes AuthMe not working correctly on machines with turkish locale. ('I'.toLowerCase() => 'ı')

This commit is contained in:
Gabriele C
2022-08-20 04:41:04 +02:00
parent c38e2aba28
commit 75b3a571e1
67 changed files with 217 additions and 143 deletions
@@ -13,6 +13,7 @@ import org.bukkit.plugin.PluginManager;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.Collection;
import java.util.Locale;
/**
* Manager class for password-related operations.
@@ -54,7 +55,7 @@ public class PasswordSecurity implements Reloadable {
* @return The password hash
*/
public HashedPassword computeHash(String password, String playerName) {
String playerLowerCase = playerName.toLowerCase();
String playerLowerCase = playerName.toLowerCase(Locale.ROOT);
return encryptionMethod.computeHash(password, playerLowerCase);
}
@@ -81,7 +82,7 @@ public class PasswordSecurity implements Reloadable {
* @return True if the password matches, false otherwise
*/
public boolean comparePassword(String password, HashedPassword hashedPassword, String playerName) {
String playerLowerCase = playerName.toLowerCase();
String playerLowerCase = playerName.toLowerCase(Locale.ROOT);
return methodMatches(encryptionMethod, password, hashedPassword, playerLowerCase)
|| compareWithLegacyHashes(password, hashedPassword, playerLowerCase);
}
@@ -7,6 +7,8 @@ import fr.xephi.authme.security.crypts.description.SaltType;
import fr.xephi.authme.security.crypts.description.Usage;
import fr.xephi.authme.util.RandomStringUtils;
import java.util.Locale;
import static fr.xephi.authme.security.HashUtils.isEqual;
/**
@@ -29,7 +31,7 @@ public class Smf implements EncryptionMethod {
@Override
public String computeHash(String password, String salt, String name) {
return HashUtils.sha1(name.toLowerCase() + password);
return HashUtils.sha1(name.toLowerCase(Locale.ROOT) + password);
}
@Override
@@ -3,6 +3,8 @@ package fr.xephi.authme.security.crypts;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.Usage;
import java.util.Locale;
import static fr.xephi.authme.security.HashUtils.isEqual;
@Recommendation(Usage.RECOMMENDED)
@@ -19,7 +21,7 @@ public class XAuth extends HexSaltedMethod {
@Override
public String computeHash(String password, String salt, String name) {
String hash = getWhirlpool(salt + password).toLowerCase();
String hash = getWhirlpool(salt + password).toLowerCase(Locale.ROOT);
int saltPos = password.length() >= hash.length() ? hash.length() - 1 : password.length();
return hash.substring(0, saltPos) + salt + hash.substring(saltPos);
}
@@ -6,6 +6,7 @@ import fr.xephi.authme.util.expiring.ExpiringMap;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
/**
@@ -32,7 +33,7 @@ public class GenerateTotpService implements HasCleanup {
*/
public TotpGenerationResult generateTotpKey(Player player) {
TotpGenerationResult credentials = totpAuthenticator.generateTotpKey(player);
totpKeys.put(player.getName().toLowerCase(), credentials);
totpKeys.put(player.getName().toLowerCase(Locale.ROOT), credentials);
return credentials;
}
@@ -43,11 +44,11 @@ public class GenerateTotpService implements HasCleanup {
* @return TOTP generation result
*/
public TotpGenerationResult getGeneratedTotpKey(Player player) {
return totpKeys.get(player.getName().toLowerCase());
return totpKeys.get(player.getName().toLowerCase(Locale.ROOT));
}
public void removeGenerateTotpKey(Player player) {
totpKeys.remove(player.getName().toLowerCase());
totpKeys.remove(player.getName().toLowerCase(Locale.ROOT));
}
/**
@@ -58,7 +59,7 @@ public class GenerateTotpService implements HasCleanup {
* @return true if the input code is correct, false if the code is invalid or no unexpired totp key is available
*/
public boolean isTotpCodeCorrectForGeneratedTotpKey(Player player, String totpCode) {
TotpGenerationResult totpDetails = totpKeys.get(player.getName().toLowerCase());
TotpGenerationResult totpDetails = totpKeys.get(player.getName().toLowerCase(Locale.ROOT));
return totpDetails != null && totpAuthenticator.checkCode(player.getName(), totpDetails.getTotpKey(), totpCode);
}
@@ -15,6 +15,8 @@ import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.Locale;
import static fr.xephi.authme.util.Utils.MILLIS_PER_MINUTE;
/**
@@ -54,7 +56,7 @@ public class TotpAuthenticator implements HasCleanup {
* @return true if code is valid, false otherwise
*/
public boolean checkCode(String playerName, String totpKey, String inputCode) {
String nameLower = playerName.toLowerCase();
String nameLower = playerName.toLowerCase(Locale.ROOT);
Integer totpCode = Ints.tryParse(inputCode);
if (totpCode != null && !usedCodes.contains(nameLower, totpCode)
&& authenticator.authorize(totpKey, totpCode)) {