Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Lowercase cleanup #1706

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
10 changes: 4 additions & 6 deletions src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ private Long getLastLoginMillis(String playerName) {
* @return true if player is registered, false otherwise
*/
public boolean isRegistered(String playerName) {
String player = playerName.toLowerCase();
return dataSource.isAuthAvailable(player);
return dataSource.isAuthAvailable(playerName);
}

/**
Expand All @@ -227,13 +226,12 @@ public boolean checkPassword(String playerName, String passwordToCheck) {
* @return true if the player was registered successfully
*/
public boolean registerPlayer(String playerName, String password) {
String name = playerName.toLowerCase();
if (isRegistered(name)) {
if (isRegistered(playerName)) {
return false;
}
HashedPassword result = passwordSecurity.computeHash(password, name);
HashedPassword result = passwordSecurity.computeHash(password, playerName);
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.name(playerName)
.password(result)
.realName(playerName)
.registrationDate(System.currentTimeMillis())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,23 @@ public void run() {
}
});
} else {
bukkitService.runTaskAsynchronously(new Runnable() {
@Override
public void run() {
PlayerAuth auth = dataSource.getAuth(playerName.toLowerCase());
if (auth == null) {
commonService.send(sender, MessageKey.UNKNOWN_USER);
return;
} else if (auth.getLastIp() == null) {
sender.sendMessage("No known last IP address for player");
return;
}
bukkitService.runTaskAsynchronously(() -> {
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commonService.send(sender, MessageKey.UNKNOWN_USER);
return;
} else if (auth.getLastIp() == null) {
sender.sendMessage("No known last IP address for player");
return;
}

List<String> accountList = dataSource.getAllAuthsByIp(auth.getLastIp());
if (accountList.isEmpty()) {
commonService.send(sender, MessageKey.UNKNOWN_USER);
} else if (accountList.size() == 1) {
sender.sendMessage("[AuthMe] " + playerName + " is a single account player");
} else {
outputAccountsList(sender, playerName, accountList);
}
List<String> accountList = dataSource.getAllAuthsByIp(auth.getLastIp());
if (accountList.isEmpty()) {
commonService.send(sender, MessageKey.UNKNOWN_USER);
} else if (accountList.size() == 1) {
sender.sendMessage("[AuthMe] " + playerName + " is a single account player");
} else {
outputAccountsList(sender, playerName, accountList);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void executeCommand(CommandSender sender, List<String> arguments) {
Set<OfflinePlayer> bannedPlayers = bukkitService.getBannedPlayers();
Set<String> namedBanned = new HashSet<>(bannedPlayers.size());
for (OfflinePlayer offlinePlayer : bannedPlayers) {
namedBanned.add(offlinePlayer.getName().toLowerCase());
namedBanned.add(offlinePlayer.getName());
}

purgeService.purgePlayers(sender, namedBanned, bannedPlayers.toArray(new OfflinePlayer[bannedPlayers.size()]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public void executeCommand(final CommandSender sender, List<String> arguments) {
// Get the player name and password
final String playerName = arguments.get(0);
final String playerPass = arguments.get(1);
final String playerNameLowerCase = playerName.toLowerCase();

// Command logic
ValidationResult passwordValidation = validationService.validatePassword(playerPass, playerName);
Expand All @@ -52,13 +51,13 @@ public void executeCommand(final CommandSender sender, List<String> arguments) {
}

bukkitService.runTaskOptionallyAsync(() -> {
if (dataSource.isAuthAvailable(playerNameLowerCase)) {
if (dataSource.isAuthAvailable(playerName)) {
commonService.send(sender, MessageKey.NAME_ALREADY_REGISTERED);
return;
}
HashedPassword hashedPassword = passwordSecurity.computeHash(playerPass, playerNameLowerCase);
HashedPassword hashedPassword = passwordSecurity.computeHash(playerPass, playerName);
PlayerAuth auth = PlayerAuth.builder()
.name(playerNameLowerCase)
.name(playerName)
.realName(playerName)
Copy link
Member

@ljacqu ljacqu Dec 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

     .name(playerName)
     .realName(playerName)

This has bit of a funny feel now. It's absolutely 100% correct. I'm just wondering if it'd make sense to have a single name(String) function, since obviously from the realname we immediately know the all-lowercase name? This is maybe something we can look at after this pull request (for the sake of merging this soon). I expect that in some cases we do only want to set the name only (without a realname), but in most cases it would be nice if we just had to pass the name once and could tell the PlayerAuth builder "hey you, do what you need to do" 😄

Edit: So to be clear, my suggestion is to do nothing about it for now but we should keep it in the back of our heads.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we can enhance it in a future cleanup

.password(hashedPassword)
.registrationDate(System.currentTimeMillis())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ChangePasswordCommand extends PlayerCommand {

@Override
public void runCommand(Player player, List<String> arguments) {
String name = player.getName().toLowerCase();
String name = player.getName();

if (!playerCache.isAuthenticated(name)) {
commonService.send(player, MessageKey.NOT_LOGGED_IN);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/fr/xephi/authme/data/TempbanManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void increaseCount(String address, String name) {
if (isEnabled) {
TimedCounter<String> countsByName = ipLoginFailureCounts.computeIfAbsent(
address, k -> new TimedCounter<>(resetThreshold, TimeUnit.MINUTES));
countsByName.increment(name);
countsByName.increment(name.toLowerCase());
}
}

Expand All @@ -66,7 +66,7 @@ public void resetCount(String address, String name) {
if (isEnabled) {
TimedCounter<String> counter = ipLoginFailureCounts.get(address);
if (counter != null) {
counter.remove(name);
counter.remove(name.toLowerCase());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/xephi/authme/data/auth/PlayerCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class PlayerCache {
* @param auth the player auth object to save
*/
public void updatePlayer(PlayerAuth auth) {
cache.put(auth.getNickname().toLowerCase(), auth);
cache.put(auth.getNickname(), auth);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about this one? Aha, nickname is lowercase by definition. OK.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ private String generateCode(String name) {
* @return true if the code matches, false otherwise
*/
public boolean checkCode(String name, String code) {
String nameLowerCase = name.toLowerCase();
String savedCode = captchaCodes.get(nameLowerCase);
String lowerCaseName = name.toLowerCase();
String savedCode = captchaCodes.get(lowerCaseName);
if (savedCode != null && savedCode.equalsIgnoreCase(code)) {
captchaCodes.remove(nameLowerCase);
captchaCodes.remove(lowerCaseName);
return true;
} else {
generateCode(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public String getCaptchaCodeOrGenerateNew(String name) {

@Override
public boolean checkCode(Player player, String code) {
String nameLower = player.getName().toLowerCase();
boolean isCodeCorrect = captchaCodeStorage.checkCode(nameLower, code);
String name = player.getName();
boolean isCodeCorrect = captchaCodeStorage.checkCode(name, code);
if (isCodeCorrect) {
verifiedNamesForRegistration.add(nameLower);
verifiedNamesForRegistration.add(name.toLowerCase());
}
return isCodeCorrect;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/fr/xephi/authme/data/limbo/LimboService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ public class LimboService {
* @param isRegistered whether or not the player is registered
*/
public void createLimboPlayer(Player player, boolean isRegistered) {
final String name = player.getName().toLowerCase();
final String lowerCaseName = player.getName().toLowerCase();

LimboPlayer limboFromDisk = persistence.getLimboPlayer(player);
if (limboFromDisk != null) {
ConsoleLogger.debug("LimboPlayer for `{0}` already exists on disk", name);
ConsoleLogger.debug("LimboPlayer for `{0}` already exists on disk", lowerCaseName);
}

LimboPlayer existingLimbo = entries.remove(name);
LimboPlayer existingLimbo = entries.remove(lowerCaseName);
if (existingLimbo != null) {
existingLimbo.clearTasks();
ConsoleLogger.debug("LimboPlayer for `{0}` already present in memory", name);
ConsoleLogger.debug("LimboPlayer for `{0}` already present in memory", lowerCaseName);
}

Location location = spawnLoader.getPlayerLocationOrSpawn(player);
Expand All @@ -75,7 +75,7 @@ public void createLimboPlayer(Player player, boolean isRegistered) {
helper.revokeLimboStates(player);
authGroupHandler.setGroup(player, limboPlayer,
isRegistered ? AuthGroupType.REGISTERED_UNAUTHENTICATED : AuthGroupType.UNREGISTERED);
entries.put(name, limboPlayer);
entries.put(lowerCaseName, limboPlayer);
persistence.saveLimboPlayer(player, limboPlayer);
}

Expand Down
16 changes: 6 additions & 10 deletions src/main/java/fr/xephi/authme/datasource/CacheDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public boolean isAuthAvailable(String user) {

@Override
public HashedPassword getPassword(String user) {
user = user.toLowerCase();
Optional<PlayerAuth> pAuthOpt = cachedAuths.getIfPresent(user);
Optional<PlayerAuth> pAuthOpt = cachedAuths.getIfPresent(user.toLowerCase());
if (pAuthOpt != null && pAuthOpt.isPresent()) {
return pAuthOpt.get().getPassword();
}
Expand All @@ -93,8 +92,7 @@ public HashedPassword getPassword(String user) {

@Override
public PlayerAuth getAuth(String user) {
user = user.toLowerCase();
return cachedAuths.getUnchecked(user).orElse(null);
return cachedAuths.getUnchecked(user.toLowerCase()).orElse(null);
}

@Override
Expand All @@ -117,10 +115,9 @@ public boolean updatePassword(PlayerAuth auth) {

@Override
public boolean updatePassword(String user, HashedPassword password) {
user = user.toLowerCase();
boolean result = source.updatePassword(user, password);
if (result) {
cachedAuths.refresh(user);
cachedAuths.refresh(user.toLowerCase());
}
return result;
}
Expand Down Expand Up @@ -150,10 +147,9 @@ public Set<String> getRecordsToPurge(long until) {

@Override
public boolean removeAuth(String name) {
name = name.toLowerCase();
boolean result = source.removeAuth(name);
if (result) {
cachedAuths.invalidate(name);
cachedAuths.invalidate(name.toLowerCase());
}
return result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!

}
Expand Down Expand Up @@ -207,12 +203,12 @@ public boolean isLogged(String user) {

@Override
public void setLogged(final String user) {
source.setLogged(user.toLowerCase());
source.setLogged(user);
}

@Override
public void setUnlogged(final String user) {
source.setUnlogged(user.toLowerCase());
source.setUnlogged(user);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void migrateAccount(String line) {
String password = args[1];
if (password != null) {
PlayerAuth auth = PlayerAuth.builder()
.name(playerName.toLowerCase())
.name(playerName)
.realName(playerName)
.password(password, null)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class RoyalAuthConverter implements Converter {
public void execute(CommandSender sender) {
for (OfflinePlayer player : plugin.getServer().getOfflinePlayers()) {
try {
String name = player.getName().toLowerCase();
File file = new File(makePath(".", "plugins", "RoyalAuth", "userdata", name + ".yml"));
String name = player.getName();
File file = new File(makePath(".", "plugins", "RoyalAuth", "userdata", name.toLowerCase() + ".yml"));

if (dataSource.isAuthAvailable(name) || !file.exists()) {
continue;
Expand All @@ -42,7 +42,7 @@ public void execute(CommandSender sender) {
.name(name)
.password(configuration.getString(PASSWORD_PATH), null)
.lastLogin(configuration.getLong(LAST_LOGIN_PATH))
.realName(player.getName())
.realName(name)
.build();

dataSource.saveAuth(auth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public void execute(CommandSender sender) {
continue;
}
auth = PlayerAuth.builder()
.name(pname.toLowerCase())
.name(pname)
.realName(pname)
.password(password, null).build();
} else {
auth = PlayerAuth.builder()
.name(name.toLowerCase())
.name(name)
.realName(name)
.password(password, null).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void convert(CommandSender sender) {
String psw = getPassword(id);
if (psw != null && !psw.isEmpty() && pl != null) {
PlayerAuth auth = PlayerAuth.builder()
.name(pl.toLowerCase())
.name(pl)
.realName(pl)
.password(psw, null).build();
database.saveAuth(auth);
Expand All @@ -89,7 +89,7 @@ private String getIdPlayer(int id) {
if (!rs.next()) {
return null;
}
realPass = rs.getString("playername").toLowerCase();
realPass = rs.getString("playername");
} catch (SQLException e) {
xAuthLog.severe("Failed to retrieve name for account: " + id, e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void saveAllPlayers() {
}

private void savePlayer(Player player) {
final String name = player.getName().toLowerCase();
final String name = player.getName();
if (PlayerUtils.isNpc(player) || validationService.isUnrestricted(name)) {
return;
}
Expand All @@ -68,7 +68,7 @@ private void saveLoggedinPlayer(Player player) {
if (settings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
Location loc = spawnLoader.getPlayerLocationOrSpawn(player);
final PlayerAuth auth = PlayerAuth.builder()
.name(player.getName().toLowerCase())
.name(player.getName())
.realName(player.getName())
.location(loc).build();
dataSource.updateQuitLoc(auth);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void checkNameCasing(String connectingName, PlayerAuth auth) throws Faile
String realName = auth.getRealName(); // might be null or "Player"

if (StringUtils.isEmpty(realName) || "Player".equals(realName)) {
dataSource.updateRealName(connectingName.toLowerCase(), connectingName);
dataSource.updateRealName(connectingName, connectingName);
} else if (!realName.equals(connectingName)) {
throw new FailedVerificationException(MessageKey.INVALID_NAME_CASE, realName, connectingName);
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/fr/xephi/authme/listener/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,12 @@ public void onJoinMessage(PlayerJoinEvent event) {
return;
}

String name = player.getName().toLowerCase();
String joinMsg = event.getJoinMessage();

// Remove the join message while the player isn't logging in
if (joinMsg != null) {
event.setJoinMessage(null);
joinMessageService.putMessage(name, joinMsg);
joinMessageService.putMessage(player.getName().toLowerCase(), joinMsg);
}
}

Expand Down Expand Up @@ -469,12 +468,12 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {
return;
}
Player player = event.getPlayer();
String name = player.getName().toLowerCase();
String name = player.getName();
Location spawn = spawnLoader.getSpawnLocation(player);
if (settings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION) && dataSource.isAuthAvailable(name)) {
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.realName(player.getName())
.realName(name)
.location(spawn)
.build();
dataSource.updateQuitLoc(auth);
Expand Down
Loading