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

152 lines
6.1 KiB
Java
Raw Normal View History

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;
/**
2019-12-30 15:15:25 +01:00
* 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>
2019-12-30 15:15:25 +01:00
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
2019-12-30 15:15:25 +01:00
* @since Envoy Server Standalone v0.1-alpha
*/
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
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
@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
2020-02-12 07:10:33 +01:00
if (user == null) {
System.out.println("Rejecting handshake on socket " + socketId);
return;
}
connectionManager.registerUser(user.getID(), socketId);
2020-02-12 07:10:33 +01:00
// 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...");
2020-02-12 07:10:33 +01:00
List<Message> pendingMessages = PersistenceManager.getInstance().getUnreadMessages(user);
for (Message msg : pendingMessages) {
2020-02-12 07:10:33 +01:00
System.out.println("Sending message " + msg.toCommonMessage());
writeProxy.write(socketId, msg.toCommonMessage());
msg.setReceivedDate(new Date());
msg.setStatus(MessageStatus.RECEIVED);
2020-02-12 07:10:33 +01:00
PersistenceManager.getInstance().updateMessage(msg);
}
}
2020-02-12 07:10:33 +01:00
@Override
public Class<LoginCredentials> 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
2020-02-12 07:10:33 +01:00
* @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
2020-02-12 07:10:33 +01:00
* @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 {
2020-02-12 07:10:33 +01:00
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
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;
}
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}
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
* @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
2020-02-12 07:10:33 +01:00
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());
2020-02-12 07:10:33 +01:00
user.setContacts(new ArrayList<>());
persistenceManager.addContact(user);
return user;
}
}
}