134 lines
4.7 KiB
Java
Executable File
134 lines
4.7 KiB
Java
Executable File
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 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.data.PersistenceManager;
|
|
import envoy.server.data.User;
|
|
import envoy.server.net.ConnectionManager;
|
|
import envoy.server.net.ObjectWriteProxy;
|
|
import envoy.util.EnvoyLog;
|
|
|
|
/**
|
|
* 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>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @author Maximilian Käfer
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public final class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
|
|
|
|
private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
|
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
|
|
|
private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class);
|
|
|
|
@Override
|
|
public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException {
|
|
|
|
// Cache this write proxy for user-independant notifications
|
|
UserStatusChangeProcessor.setWriteProxy(writeProxy);
|
|
|
|
if (!VersionUtils.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(USERNAME_TAKEN));
|
|
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);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
|
|
// Send the message
|
|
writeProxy.write(socketID, msgCommon);
|
|
msg.received();
|
|
PersistenceManager.getInstance().updateMessage(msg);
|
|
|
|
// Notify the sender about the delivery
|
|
if (connectionManager.isOnline(msg.getSender().getID())) {
|
|
msgCommon.nextStatus();
|
|
writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), new MessageStatusChange(msgCommon));
|
|
}
|
|
} else writeProxy.write(socketID, new MessageStatusChange(msgCommon));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
|
|
}
|