2020-01-11 18:28:48 +01:00
|
|
|
package envoy.server.processors;
|
2019-12-30 14:53:40 +01:00
|
|
|
|
2020-01-06 13:58:28 +01:00
|
|
|
import java.io.IOException;
|
2020-03-25 16:34:55 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.InputMismatchException;
|
|
|
|
import java.util.List;
|
2020-01-18 13:28:54 +01:00
|
|
|
import java.util.stream.Collectors;
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-02-07 23:37:33 +01:00
|
|
|
import javax.persistence.NoResultException;
|
|
|
|
|
2020-01-06 13:58:28 +01:00
|
|
|
import envoy.data.Contacts;
|
2019-12-30 14:53:40 +01:00
|
|
|
import envoy.data.LoginCredentials;
|
2020-01-11 18:28:48 +01:00
|
|
|
import envoy.data.Message.MessageStatus;
|
2019-12-30 14:53:40 +01:00
|
|
|
import envoy.data.User;
|
2020-02-05 21:32:53 +01:00
|
|
|
import envoy.data.User.UserStatus;
|
2020-02-07 23:37:33 +01:00
|
|
|
import envoy.event.HandshakeRejectionEvent;
|
2020-01-11 18:28:48 +01:00
|
|
|
import envoy.server.data.Message;
|
2020-02-15 22:43:20 +01:00
|
|
|
import envoy.server.data.PersistenceManager;
|
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;
|
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-01-06 13:58:28 +01:00
|
|
|
public 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
|
|
|
|
2019-12-30 14:53:40 +01:00
|
|
|
@Override
|
2020-01-06 13:58:28 +01:00
|
|
|
public void process(LoginCredentials input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
2020-02-02 13:34:28 +01:00
|
|
|
UserStatusChangeProcessor.setWriteProxy(writeProxy);
|
2020-01-03 17:11:38 +01:00
|
|
|
System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId));
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-02-09 16:37:53 +01:00
|
|
|
envoy.server.data.User user = getUser(input, socketId, writeProxy);
|
2020-01-18 23:50:41 +01:00
|
|
|
|
2020-01-25 18:41:44 +01:00
|
|
|
// Not logged in successfully
|
2020-02-12 07:10:33 +01:00
|
|
|
if (user == null) {
|
|
|
|
System.out.println("Rejecting handshake on socket " + socketId);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-25 16:34:55 +01:00
|
|
|
connectionManager.registerUser(user.getID(), socketId);
|
2020-02-04 21:09:59 +01:00
|
|
|
|
2020-02-12 07:10:33 +01:00
|
|
|
// Notifies contacts of this users online-going and updates his status in the
|
2020-02-01 23:37:44 +01:00
|
|
|
// database
|
2020-02-05 21:32:53 +01:00
|
|
|
user.setStatus(UserStatus.ONLINE);
|
|
|
|
UserStatusChangeProcessor.updateUserStatus(user);
|
2020-01-25 18:41:44 +01:00
|
|
|
|
2020-01-06 13:58:28 +01:00
|
|
|
// Create contacts
|
2020-03-25 16:34:55 +01:00
|
|
|
Contacts contacts = new Contacts(user.getContacts().stream().map(envoy.server.data.User::toCommon).collect(Collectors.toList()));
|
|
|
|
contacts.getContacts().add(user.toCommon());
|
2020-01-06 13:58:28 +01:00
|
|
|
|
|
|
|
// Complete handshake
|
|
|
|
System.out.println("Sending user...");
|
2020-03-25 16:34:55 +01:00
|
|
|
writeProxy.write(socketId, user.toCommon());
|
2020-01-06 13:58:28 +01:00
|
|
|
System.out.println("Sending contacts...");
|
|
|
|
writeProxy.write(socketId, contacts);
|
2020-01-18 09:55:12 +01:00
|
|
|
System.out.println("Acquiring pending messages for the client...");
|
2020-02-12 07:10:33 +01:00
|
|
|
List<Message> pendingMessages = PersistenceManager.getInstance().getUnreadMessages(user);
|
2020-01-18 09:55:12 +01:00
|
|
|
for (Message msg : pendingMessages) {
|
2020-02-12 07:10:33 +01:00
|
|
|
System.out.println("Sending message " + msg.toCommonMessage());
|
2020-02-03 07:06:52 +01:00
|
|
|
writeProxy.write(socketId, msg.toCommonMessage());
|
2020-01-11 18:28:48 +01:00
|
|
|
msg.setReceivedDate(new Date());
|
|
|
|
msg.setStatus(MessageStatus.RECEIVED);
|
2020-02-12 07:10:33 +01:00
|
|
|
PersistenceManager.getInstance().updateMessage(msg);
|
2020-01-18 09:55:12 +01:00
|
|
|
}
|
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; }
|
|
|
|
|
2020-02-09 16:37:53 +01:00
|
|
|
private envoy.server.data.User getUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
|
|
|
return credentials.isRegistration() ? newUser(credentials, socketId, writeProxy) : checkForExistingUser(credentials, socketId, writeProxy);
|
2020-02-07 23:37:33 +01:00
|
|
|
}
|
2020-02-01 23:37:44 +01:00
|
|
|
|
2020-02-07 23:37:33 +01:00
|
|
|
/**
|
|
|
|
* @param credentials the input to evaluate
|
2020-02-12 07:10:33 +01:00
|
|
|
* @param socketId the socket ID at which the client performing the handshake
|
|
|
|
* is connected
|
2020-02-07 23:37:33 +01:00
|
|
|
* @param writeProxy the {@link ObjectWriteProxy} to use if login was not
|
|
|
|
* successful
|
2020-02-12 07:10:33 +01:00
|
|
|
* @return the database user matching the login credentials
|
2020-02-07 23:37:33 +01:00
|
|
|
* @throws IOException if sending the failed login back to the client failed
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-09 16:37:53 +01:00
|
|
|
private envoy.server.data.User checkForExistingUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
2020-02-07 23:37:33 +01:00
|
|
|
try {
|
2020-02-12 07:10:33 +01:00
|
|
|
envoy.server.data.User user = persistenceManager.getUserByName(credentials.getIdentifier());
|
|
|
|
|
2020-02-07 23:37:33 +01:00
|
|
|
// Checking if user is already online
|
2020-03-25 16:34:55 +01:00
|
|
|
if (connectionManager.isOnline(user.getID())) {
|
2020-02-07 23:37:33 +01:00
|
|
|
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE));
|
2020-01-25 18:41:44 +01:00
|
|
|
return null;
|
2020-02-07 23:37:33 +01:00
|
|
|
}
|
|
|
|
// Evaluating the correctness of the password hash
|
2020-02-12 07:10:33 +01:00
|
|
|
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) {
|
|
|
|
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD));
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-07 23:37:33 +01:00
|
|
|
return user;
|
|
|
|
} catch (NoResultException e) {
|
|
|
|
// Checking if user exists
|
|
|
|
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.USER_DOES_NOT_EXIST));
|
|
|
|
} catch (InputMismatchException e) {
|
|
|
|
// Checking if the given password hash is correct
|
2020-02-09 16:37:53 +01:00
|
|
|
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD));
|
2020-01-25 18:41:44 +01:00
|
|
|
}
|
2020-02-07 23:37:33 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param credentials the credentials upon which to create the new {@link User}
|
2020-02-12 07:10:33 +01:00
|
|
|
* @param socketId the socketID at which the client performing the handshake
|
|
|
|
* is connected
|
|
|
|
* @param writeProxy the write proxy used to notify the client about handshake
|
|
|
|
* rejection
|
2020-02-07 23:37:33 +01:00
|
|
|
* @return the newly created {@link User}
|
2020-02-09 16:37:53 +01:00
|
|
|
* @throws IOException if sending the failed login back to the client failed
|
2020-02-07 23:37:33 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-09 16:37:53 +01:00
|
|
|
private envoy.server.data.User newUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
2020-02-09 20:41:29 +01:00
|
|
|
try {
|
|
|
|
// Checking that no user already has this identifier
|
2020-02-12 07:10:33 +01:00
|
|
|
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
|
2020-02-09 20:41:29 +01:00
|
|
|
// this code only gets executed if this user already exists
|
2020-02-09 16:37:53 +01:00
|
|
|
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.USER_EXISTS_ALREADY));
|
2020-02-09 20:41:29 +01:00
|
|
|
return null;
|
|
|
|
} catch (NoResultException e) {
|
|
|
|
// Creation of a new user
|
|
|
|
envoy.server.data.User user;
|
|
|
|
user = new envoy.server.data.User();
|
|
|
|
user.setName(credentials.getIdentifier());
|
|
|
|
user.setLastSeen(new Date());
|
|
|
|
user.setStatus(User.UserStatus.ONLINE);
|
|
|
|
user.setPasswordHash(credentials.getPasswordHash());
|
2020-02-12 07:10:33 +01:00
|
|
|
user.setContacts(new ArrayList<>());
|
2020-03-25 16:34:55 +01:00
|
|
|
persistenceManager.addContact(user);
|
2020-02-09 20:41:29 +01:00
|
|
|
return user;
|
|
|
|
}
|
2020-01-25 18:41:44 +01:00
|
|
|
}
|
2019-12-30 14:53:40 +01:00
|
|
|
}
|