Implemented method to check for the validity of logins

Warning: will only work once f/advanced_login of envoy-common will be merged into develop
This commit is contained in:
delvh 2020-02-07 23:37:33 +01:00
parent 48e12dcdfe
commit d967c88e5b

View File

@ -3,14 +3,18 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.InputMismatchException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.persistence.NoResultException;
import envoy.data.Contacts; import envoy.data.Contacts;
import envoy.data.LoginCredentials; import envoy.data.LoginCredentials;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.data.User; import envoy.data.User;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import envoy.event.HandshakeRejectionEvent;
import envoy.server.ConnectionManager; import envoy.server.ConnectionManager;
import envoy.server.ObjectProcessor; import envoy.server.ObjectProcessor;
import envoy.server.data.Message; import envoy.server.data.Message;
@ -40,7 +44,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
UserStatusChangeProcessor.setWriteProxy(writeProxy); UserStatusChangeProcessor.setWriteProxy(writeProxy);
System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId)); System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId));
envoy.server.data.User user = getUser(input); envoy.server.data.User user = getUser(input, writeProxy);
// Not logged in successfully // Not logged in successfully
if (user == null) return; if (user == null) return;
@ -72,10 +76,50 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
} }
} }
private envoy.server.data.User getUser(LoginCredentials credentials) { private envoy.server.data.User getUser(LoginCredentials credentials, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.User user; return credentials.isRegistration() ? newUser(credentials) : checkForExistingUser(credentials, writeProxy);
}
if (credentials.isRegistration()) { /**
* @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, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.User user;
ConnectionManager connectionManager = ConnectionManager.getInstance();
long userId = credentials.getId();
long socketId = connectionManager.getSocketId(userId);
try {
// Checking if user is already online
if (connectionManager.isOnline(userId)) {
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE));
return null;
}
user = persistenceManager.getUserById(userId);
// Evaluating the correctness of the password hash
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash()))
throw new InputMismatchException("User " + credentials.getName() + "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.INCORRECT_PASSWORD));
}
return null;
}
/**
* @param credentials the credentials upon which to create the new {@link User}
* @return the newly created {@link User}
* @since Envoy Server Standalone v0.1-alpha
*/
private envoy.server.data.User newUser(LoginCredentials credentials) {
envoy.server.data.User user;
user = new envoy.server.data.User(); user = new envoy.server.data.User();
user.setName(credentials.getName()); user.setName(credentials.getName());
user.setLastSeen(new Date()); user.setLastSeen(new Date());
@ -83,12 +127,6 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
user.setPasswordHash(credentials.getPasswordHash()); user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user)); user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user));
persistenceManager.addUser(user); persistenceManager.addUser(user);
} else {
user = persistenceManager.getUserByName(credentials.getName());
// TODO: Implement error when user does not exist
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) // TODO: Wrong Password Response
return null;
}
return user; return user;
} }
} }