This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/server/processors/LoginCredentialProcessor.java

182 lines
6.3 KiB
Java
Raw Normal View History

package envoy.server.processors;
import static envoy.data.User.UserStatus.ONLINE;
import static envoy.event.HandshakeRejection.*;
import java.io.IOException;
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 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;
import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/**
2019-12-30 15:15:25 +01:00
* This {@link ObjectProcessor} handles {@link LoginCredentials}.<br>
* <br>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>LoginCredentialProcessor.java</strong><br>
* Created: <strong>30.12.2019</strong><br>
2019-12-30 15:15:25 +01:00
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
2019-12-30 15:15:25 +01:00
* @since Envoy Server Standalone v0.1-alpha
*/
public final class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
2020-02-12 07:10:33 +01:00
private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
2019-12-30 15:15:25 +01:00
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 {
// Cache this write proxy for user-independant notifications
UserStatusChangeProcessor.setWriteProxy(writeProxy);
// TODO: Verify compatibility
if (!verifyCompatibility(credentials.getClientVersion())) {
logger.info("The client has the wrong version.");
writeProxy.write(socketID, new HandshakeRejection("Wrong version"));
return;
}
// Acquire a user object (or reject the handshake if that's impossible)
User user = null;
if (!credentials.isRegistration()) {
try {
user = persistenceManager.getUserByName(credentials.getIdentifier());
// Checking if user is already online
if (connectionManager.isOnline(user.getID())) {
logger.warning(user + " is already online!");
writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR));
return;
}
// Evaluating the correctness of the password hash
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) {
logger.info(user + " has entered the wrong password.");
writeProxy.write(socketID, new HandshakeRejection(WRONG_PASSWORD_OR_USER));
return;
}
} catch (NoResultException e) {
logger.info("The requested user does not exist.");
writeProxy.write(socketID, new HandshakeRejection(WRONG_PASSWORD_OR_USER));
return;
}
} else {
try {
// Checking that no user already has this identifier
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
// This code only gets executed if this user already exists
logger.info("The requested user already exists.");
writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR));
return;
} catch (NoResultException e) {
// Creation of a new user
user = new User();
user.setName(credentials.getIdentifier());
user.setLastSeen(LocalDateTime.now());
user.setStatus(ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(new HashSet<>());
persistenceManager.addContact(user);
logger.info("Registered new " + user);
}
2020-02-12 07:10:33 +01:00
}
logger.info(user + " successfully authenticated.");
connectionManager.registerUser(user.getID(), socketID);
// Change status and notify contacts about it
user.setStatus(ONLINE);
UserStatusChangeProcessor.updateUserStatus(user);
// Complete the handshake
writeProxy.write(socketID, user.toCommon());
final var pendingMessages = PersistenceManager.getInstance().getPendingMessages(user);
logger.fine("Sending " + pendingMessages.size() + " pending messages to " + user + "...");
for (var msg : pendingMessages) {
final var msgCommon = msg.toCommon();
if (msg.getStatus() == MessageStatus.SENT) {
writeProxy.write(socketID, msgCommon);
msg.received();
if (connectionManager.isOnline(msg.getSender().getID()))
writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), new MessageStatusChange(msgCommon));
PersistenceManager.getInstance().updateMessage(msg);
} else writeProxy.write(socketID, new MessageStatusChange(msgCommon));
}
}
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;
}
}
2020-02-12 07:10:33 +01:00
@Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
}