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

139 lines
5.8 KiB
Java

package envoy.server.processors;
import java.io.IOException;
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.ConnectionManager;
import envoy.server.ObjectProcessor;
import envoy.server.data.Message;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectWriteProxy;
/**
* 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&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
private PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager();
@Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
@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) return;
ConnectionManager.getInstance().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.getId(),
user.getContacts().stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
contacts.getContacts().add(user.toCommonUser());
// Complete handshake
System.out.println("Sending user...");
writeProxy.write(socketId, user.toCommonUser());
System.out.println("Sending contacts...");
writeProxy.write(socketId, contacts);
System.out.println("Acquiring pending messages for the client...");
List<Message> pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(user);
for (Message msg : pendingMessages) {
System.out.println("Sending message " + msg.toCommonMessage().toString());
writeProxy.write(socketId, msg.toCommonMessage());
msg.setReceivedDate(new Date());
msg.setStatus(MessageStatus.RECEIVED);
PersistenceManager.getPersistenceManager().updateMessage(msg);
}
}
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 writeProxy the {@link ObjectWriteProxy} to use if login was not
* successful
* @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 {
envoy.server.data.User user;
ConnectionManager connectionManager = ConnectionManager.getInstance();
String userIdentifier = credentials.getIdentifier();
try {
//TODO will need to be replaced with the Identifier once implemented
user = persistenceManager.getUserByName(userIdentifier);
// 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()))
throw new InputMismatchException("User " + credentials.getIdentifier() + "tried logging in using a wrong password");
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}
* @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 {
// Checking that no user already has this identifier TODO change to identifier once implemented
if (PersistenceManager.getPersistenceManager().getUserByName(credentials.getIdentifier()) != null)
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.USER_EXISTS_ALREADY));
// Creation of a new user
envoy.server.data.User user;
user = new envoy.server.data.User();
//TODO needs to be replaced later on
user.setName(credentials.getIdentifier());
user.setLastSeen(new Date());
user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user));
persistenceManager.addUser(user);
return user;
}
}