package envoy.server.processors; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.InputMismatchException; import java.util.List; import java.util.stream.Collectors; import javax.persistence.NoResultException; import envoy.data.Contacts; import envoy.data.LoginCredentials; import envoy.data.Message.MessageStatus; import envoy.data.User; import envoy.data.User.UserStatus; import envoy.event.HandshakeRejectionEvent; import envoy.server.data.Message; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; /** * This {@link ObjectProcessor} handles {@link LoginCredentials}.
*
* Project: envoy-server-standalone
* File: LoginCredentialProcessor.java
* Created: 30.12.2019
* * @author Kai S. K. Engelbart * @author Maximilian Käfer * @since Envoy Server Standalone v0.1-alpha */ public class LoginCredentialProcessor implements ObjectProcessor { private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance(); @Override public void process(LoginCredentials input, long socketId, ObjectWriteProxy writeProxy) throws IOException { UserStatusChangeProcessor.setWriteProxy(writeProxy); System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId)); envoy.server.data.User user = getUser(input, socketId, writeProxy); // Not logged in successfully if (user == null) { System.out.println("Rejecting handshake on socket " + socketId); return; } connectionManager.registerUser(user.getID(), socketId); // Notifies contacts of this users online-going and updates his status in the // database user.setStatus(UserStatus.ONLINE); UserStatusChangeProcessor.updateUserStatus(user); // Create contacts Contacts contacts = new Contacts(user.getContacts().stream().map(envoy.server.data.User::toCommon).collect(Collectors.toList())); contacts.getContacts().add(user.toCommon()); // Complete handshake System.out.println("Sending user..."); writeProxy.write(socketId, user.toCommon()); System.out.println("Sending contacts..."); writeProxy.write(socketId, contacts); System.out.println("Acquiring pending messages for the client..."); List pendingMessages = PersistenceManager.getInstance().getUnreadMessages(user); for (Message msg : pendingMessages) { System.out.println("Sending message " + msg.toCommonMessage()); writeProxy.write(socketId, msg.toCommonMessage()); msg.setReceivedDate(new Date()); msg.setStatus(MessageStatus.RECEIVED); PersistenceManager.getInstance().updateMessage(msg); } } @Override public Class getInputClass() { return LoginCredentials.class; } 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); } /** * @param credentials the input to evaluate * @param socketId the socket ID at which the client performing the handshake * is connected * @param writeProxy the {@link ObjectWriteProxy} to use if login was not * successful * @return the database user matching the login credentials * @throws IOException if sending the failed login back to the client failed * @since Envoy Server Standalone v0.1-alpha */ private envoy.server.data.User checkForExistingUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException { try { envoy.server.data.User user = persistenceManager.getUserByName(credentials.getIdentifier()); // Checking if user is already online if (connectionManager.isOnline(user.getID())) { writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE)); return null; } // Evaluating the correctness of the password hash if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) { writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD)); return null; } 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 writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD)); } return null; } /** * @param credentials the credentials upon which to create the new {@link User} * @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 * @return the newly created {@link User} * @throws IOException if sending the failed login back to the client failed * @since Envoy Server Standalone v0.1-alpha */ private envoy.server.data.User newUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException { try { // Checking that no user already has this identifier PersistenceManager.getInstance().getUserByName(credentials.getIdentifier()); // this code only gets executed if this user already exists writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.USER_EXISTS_ALREADY)); 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()); user.setContacts(new ArrayList<>()); persistenceManager.addContact(user); return user; } } }