2020-01-11 18:28:48 +01:00
|
|
|
package envoy.server.processors;
|
2019-12-30 14:53:40 +01:00
|
|
|
|
2020-07-06 12:34:13 +02:00
|
|
|
import static envoy.data.Message.MessageStatus.*;
|
2020-06-20 09:57:54 +02:00
|
|
|
import static envoy.data.User.UserStatus.ONLINE;
|
2020-07-05 16:53:27 +02:00
|
|
|
import static envoy.event.HandshakeRejection.*;
|
2020-06-20 09:57:54 +02:00
|
|
|
|
2020-07-16 17:04:35 +02:00
|
|
|
import java.time.Instant;
|
2020-07-10 23:25:13 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
2020-06-11 12:06:06 +02:00
|
|
|
import java.util.logging.Logger;
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-02-07 23:37:33 +01:00
|
|
|
import javax.persistence.NoResultException;
|
|
|
|
|
2019-12-30 14:53:40 +01:00
|
|
|
import envoy.data.LoginCredentials;
|
2020-07-03 17:46:37 +02:00
|
|
|
import envoy.event.GroupMessageStatusChange;
|
2020-06-20 09:57:54 +02:00
|
|
|
import envoy.event.HandshakeRejection;
|
|
|
|
import envoy.event.MessageStatusChange;
|
2020-06-27 21:58:53 +02:00
|
|
|
import envoy.server.data.GroupMessage;
|
2020-02-15 22:43:20 +01:00
|
|
|
import envoy.server.data.PersistenceManager;
|
2020-06-20 09:57:54 +02:00
|
|
|
import envoy.server.data.User;
|
2020-02-12 22:01:32 +01:00
|
|
|
import envoy.server.net.ConnectionManager;
|
2020-01-06 13:58:28 +01:00
|
|
|
import envoy.server.net.ObjectWriteProxy;
|
2020-07-10 23:25:13 +02:00
|
|
|
import envoy.server.util.PasswordUtil;
|
|
|
|
import envoy.server.util.VersionUtil;
|
2020-06-26 22:04:40 +02:00
|
|
|
import envoy.util.Bounds;
|
2020-06-11 12:06:06 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
2019-12-30 14:53:40 +01:00
|
|
|
|
|
|
|
/**
|
2019-12-30 15:15:25 +01:00
|
|
|
* This {@link ObjectProcessor} handles {@link LoginCredentials}.<br>
|
|
|
|
* <br>
|
2019-12-30 14:53:40 +01:00
|
|
|
* 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
|
|
|
*
|
2019-12-30 14:53:40 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
2020-01-18 23:50:41 +01:00
|
|
|
* @author Maximilian Käfer
|
2019-12-30 15:15:25 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
2019-12-30 14:53:40 +01:00
|
|
|
*/
|
2020-06-20 09:57:54 +02:00
|
|
|
public final class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
|
2019-12-30 14:53:40 +01:00
|
|
|
|
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
|
|
|
|
2020-06-26 22:04:40 +02:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class);
|
2020-06-11 12:06:06 +02:00
|
|
|
|
2019-12-30 14:53:40 +01:00
|
|
|
@Override
|
2020-07-06 11:40:13 +02:00
|
|
|
public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) {
|
2020-06-20 09:57:54 +02:00
|
|
|
|
2020-07-16 17:04:35 +02:00
|
|
|
// Cache this write proxy for user-independent notifications
|
2020-02-02 13:34:28 +01:00
|
|
|
UserStatusChangeProcessor.setWriteProxy(writeProxy);
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-07-10 23:25:13 +02:00
|
|
|
if (!VersionUtil.verifyCompatibility(credentials.getClientVersion())) {
|
2020-06-20 14:38:06 +02:00
|
|
|
logger.info("The client has the wrong version.");
|
2020-06-23 09:08:57 +02:00
|
|
|
writeProxy.write(socketID, new HandshakeRejection(WRONG_VERSION));
|
2020-06-20 14:38:06 +02:00
|
|
|
return;
|
2020-06-20 09:57:54 +02:00
|
|
|
}
|
2020-01-18 23:50:41 +01:00
|
|
|
|
2020-06-20 09:57:54 +02:00
|
|
|
// 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
|
2020-07-10 23:25:13 +02:00
|
|
|
if (!PasswordUtil.validate(credentials.getPassword(), user.getPasswordHash())) {
|
2020-06-20 09:57:54 +02:00
|
|
|
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 {
|
2020-06-26 22:04:40 +02:00
|
|
|
// Validate user name
|
|
|
|
if (!Bounds.isValidContactName(credentials.getIdentifier())) {
|
|
|
|
logger.info("The requested user name is not valid.");
|
|
|
|
writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR));
|
2020-06-20 09:57:54 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-06 11:40:13 +02:00
|
|
|
try {
|
|
|
|
// Checking that no user already has this identifier
|
|
|
|
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
|
2020-06-26 22:04:40 +02:00
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
// 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());
|
2020-07-16 17:04:35 +02:00
|
|
|
user.setLastSeen(Instant.now());
|
2020-07-06 11:40:13 +02:00
|
|
|
user.setStatus(ONLINE);
|
2020-07-10 23:25:13 +02:00
|
|
|
user.setPasswordHash(PasswordUtil.hash(credentials.getPassword()));
|
2020-07-06 11:40:13 +02:00
|
|
|
user.setContacts(new HashSet<>());
|
|
|
|
persistenceManager.addContact(user);
|
|
|
|
logger.info("Registered new " + user);
|
|
|
|
}
|
2020-02-12 07:10:33 +01:00
|
|
|
}
|
2020-06-20 09:57:54 +02:00
|
|
|
|
|
|
|
logger.info(user + " successfully authenticated.");
|
2020-03-26 20:14:09 +01:00
|
|
|
connectionManager.registerUser(user.getID(), socketID);
|
2020-02-04 21:09:59 +01:00
|
|
|
|
2020-06-20 09:57:54 +02:00
|
|
|
// Complete the handshake
|
2020-03-26 20:14:09 +01:00
|
|
|
writeProxy.write(socketID, user.toCommon());
|
2020-06-20 09:57:54 +02:00
|
|
|
|
2020-07-16 17:04:35 +02:00
|
|
|
final var pendingMessages = PersistenceManager.getInstance().getPendingMessages(user, credentials.getLastSync());
|
2020-07-05 16:53:27 +02:00
|
|
|
pendingMessages.removeIf(GroupMessage.class::isInstance);
|
2020-06-20 09:57:54 +02:00
|
|
|
logger.fine("Sending " + pendingMessages.size() + " pending messages to " + user + "...");
|
2020-06-25 17:00:41 +02:00
|
|
|
|
2020-06-20 09:57:54 +02:00
|
|
|
for (var msg : pendingMessages) {
|
|
|
|
final var msgCommon = msg.toCommon();
|
2020-07-16 18:32:40 +02:00
|
|
|
if (msg.getCreationDate().isAfter(credentials.getLastSync())) {
|
|
|
|
|
|
|
|
// Sync without side effects
|
|
|
|
writeProxy.write(socketID, msgCommon);
|
|
|
|
} else if (msg.getStatus() == SENT) {
|
2020-06-25 17:00:41 +02:00
|
|
|
|
|
|
|
// Send the message
|
2020-06-20 09:57:54 +02:00
|
|
|
writeProxy.write(socketID, msgCommon);
|
|
|
|
msg.received();
|
2020-04-02 20:41:05 +02:00
|
|
|
PersistenceManager.getInstance().updateMessage(msg);
|
2020-06-25 17:00:41 +02:00
|
|
|
|
|
|
|
// Notify the sender about the delivery
|
|
|
|
if (connectionManager.isOnline(msg.getSender().getID())) {
|
|
|
|
msgCommon.nextStatus();
|
|
|
|
writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), new MessageStatusChange(msgCommon));
|
|
|
|
}
|
2020-06-20 09:57:54 +02:00
|
|
|
} else writeProxy.write(socketID, new MessageStatusChange(msgCommon));
|
|
|
|
}
|
2020-06-27 21:58:53 +02:00
|
|
|
|
2020-07-16 17:04:35 +02:00
|
|
|
List<GroupMessage> pendingGroupMessages = PersistenceManager.getInstance().getPendingGroupMessages(user, credentials.getLastSync());
|
2020-07-06 11:40:13 +02:00
|
|
|
logger.fine("Sending " + pendingGroupMessages.size() + " pending group messages to " + user + "...");
|
|
|
|
|
|
|
|
for (var gmsg : pendingGroupMessages) {
|
|
|
|
final var gmsgCommon = gmsg.toCommon();
|
|
|
|
|
|
|
|
// Deliver the message to the user if he hasn't received it yet
|
2020-07-06 12:34:13 +02:00
|
|
|
if (gmsg.getMemberMessageStatus().get(user.getID()) == SENT) {
|
2020-07-16 18:32:40 +02:00
|
|
|
if (gmsg.getMemberMessageStatus().replace(user.getID(), RECEIVED) != RECEIVED) {
|
2020-07-08 13:53:10 +02:00
|
|
|
|
2020-07-16 18:32:40 +02:00
|
|
|
gmsg.setLastStatusChangeDate(Instant.now());
|
2020-07-08 13:53:10 +02:00
|
|
|
|
2020-07-16 18:32:40 +02:00
|
|
|
writeProxy.write(socketID, gmsgCommon);
|
2020-07-06 11:40:13 +02:00
|
|
|
|
2020-07-16 18:32:40 +02:00
|
|
|
// Notify all online group members about the status change
|
|
|
|
writeProxy.writeToOnlineContacts(gmsg.getRecipient().getContacts(),
|
|
|
|
new GroupMessageStatusChange(gmsg.getID(), RECEIVED, Instant.now(), connectionManager.getUserIDBySocketID(socketID)));
|
2020-07-06 11:40:13 +02:00
|
|
|
|
2020-07-16 18:32:40 +02:00
|
|
|
if (Collections.min(gmsg.getMemberMessageStatus().values()) == RECEIVED) {
|
|
|
|
gmsg.received();
|
2020-07-06 11:40:13 +02:00
|
|
|
|
2020-07-16 18:32:40 +02:00
|
|
|
// Notify online members about the status change
|
|
|
|
writeProxy.writeToOnlineContacts(gmsg.getRecipient().getContacts(),
|
|
|
|
new MessageStatusChange(gmsg.getID(), gmsg.getStatus(), Instant.now()));
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistenceManager.getInstance().updateMessage(gmsg);
|
|
|
|
} else {
|
2020-07-06 11:40:13 +02:00
|
|
|
|
2020-07-16 18:32:40 +02:00
|
|
|
// Just send the message without updating if it was received in the past
|
|
|
|
writeProxy.write(socketID, gmsgCommon);
|
|
|
|
}
|
2020-06-27 21:58:53 +02:00
|
|
|
} else {
|
2020-07-06 11:40:13 +02:00
|
|
|
|
2020-07-08 13:53:10 +02:00
|
|
|
// Sending group message status changes
|
|
|
|
if (gmsg.getStatus() == SENT && gmsg.getLastStatusChangeDate().isAfter(gmsg.getCreationDate())
|
|
|
|
|| gmsg.getStatus() == RECEIVED && gmsg.getLastStatusChangeDate().isAfter(gmsg.getReceivedDate())) {
|
2020-07-16 18:32:40 +02:00
|
|
|
gmsg.getMemberMessageStatus()
|
|
|
|
.forEach((memberID, memberStatus) -> writeProxy.write(socketID,
|
|
|
|
new GroupMessageStatusChange(gmsg.getID(), memberStatus, gmsg.getLastStatusChangeDate(), memberID)));
|
2020-07-08 13:53:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
// Deliver just a status change instead of the whole message
|
2020-07-08 13:53:10 +02:00
|
|
|
if (gmsg.getStatus() == SENT && user.getLastSeen().isBefore(gmsg.getCreationDate())
|
|
|
|
|| gmsg.getStatus() == RECEIVED && user.getLastSeen().isBefore(gmsg.getReceivedDate()))
|
|
|
|
writeProxy.write(socketID, new MessageStatusChange(gmsgCommon));
|
2020-06-27 21:58:53 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 21:14:37 +02:00
|
|
|
|
|
|
|
// Change status and notify contacts about it
|
|
|
|
user.setStatus(ONLINE);
|
|
|
|
UserStatusChangeProcessor.updateUserStatus(user);
|
2019-12-30 14:53:40 +01:00
|
|
|
}
|
2020-01-25 18:41:44 +01:00
|
|
|
|
2020-02-12 07:10:33 +01:00
|
|
|
@Override
|
|
|
|
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
|
2019-12-30 14:53:40 +01:00
|
|
|
}
|