Apply suggestions from code review

This commit is contained in:
2020-06-23 09:08:57 +02:00
parent e9610b00ce
commit 0d7cb38b6d
8 changed files with 133 additions and 79 deletions

View File

@ -8,15 +8,14 @@ import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.persistence.NoResultException;
import enovy.server.util.VersionUtils;
import envoy.data.LoginCredentials;
import envoy.data.Message.MessageStatus;
import envoy.event.HandshakeRejection;
import envoy.event.MessageStatusChange;
import envoy.server.Startup;
import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.net.ConnectionManager;
@ -40,7 +39,6 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class);
private static final Pattern versionPattern = Pattern.compile("(?<major>\\d).(?<minor>\\d)(?:-(?<suffix>\\w+))?");
@Override
public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException {
@ -48,10 +46,9 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
// Cache this write proxy for user-independant notifications
UserStatusChangeProcessor.setWriteProxy(writeProxy);
// TODO: Verify compatibility
if (!verifyCompatibility(credentials.getClientVersion())) {
if (!VersionUtils.verifyCompatibility(credentials.getClientVersion())) {
logger.info("The client has the wrong version.");
writeProxy.write(socketID, new HandshakeRejection("Wrong version"));
writeProxy.write(socketID, new HandshakeRejection(WRONG_VERSION));
return;
}
@ -85,7 +82,7 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
// This code only gets executed if this user already exists
logger.info("The requested user already exists.");
writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR));
writeProxy.write(socketID, new HandshakeRejection(USERNAME_TAKEN));
return;
} catch (NoResultException e) {
// Creation of a new user
@ -101,7 +98,6 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
}
logger.info(user + " successfully authenticated.");
connectionManager.registerUser(user.getID(), socketID);
// Change status and notify contacts about it
@ -125,57 +121,6 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
}
}
private boolean verifyCompatibility(String version) {
final var currentMatcher = versionPattern.matcher(version);
if (!currentMatcher.matches()) return false;
final var minMatcher = versionPattern.matcher(Startup.MIN_CLIENT_VERSION);
final var maxMatcher = versionPattern.matcher(Startup.MAX_CLIENT_VERSION);
if (!minMatcher.matches() || !maxMatcher.matches()) throw new RuntimeException("Invalid min or max client version configured!");
// Compare suffixes
{
final var currentSuffix = convertSuffix(currentMatcher.group("suffix"));
final var minSuffix = convertSuffix(minMatcher.group("suffix"));
final var maxSuffix = convertSuffix(maxMatcher.group("suffix"));
if (currentSuffix < minSuffix || currentSuffix > maxSuffix) return false;
}
// Compare major
{
final var currentMajor = Integer.parseInt(currentMatcher.group("major"));
final var minMajor = Integer.parseInt(minMatcher.group("major"));
final var maxMajor = Integer.parseInt(maxMatcher.group("major"));
if (currentMajor < minMajor || currentMajor > maxMajor) return false;
}
// Compare minor
{
final var currentMinor = Integer.parseInt(currentMatcher.group("minor"));
final var minMinor = Integer.parseInt(minMatcher.group("minor"));
final var maxMinor = Integer.parseInt(maxMatcher.group("minor"));
if (currentMinor < minMinor || currentMinor > maxMinor) return false;
}
return true;
}
private int convertSuffix(String suffix) {
switch (suffix == null ? "" : suffix) {
case "alpha":
return 0;
case "beta":
return 1;
default:
return 2;
}
}
@Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
}