added methods to signal an invalid login

additionally added a failsafe to set all users to offline in case of
shutdown of the server
This commit is contained in:
delvh
2020-02-09 16:37:53 +01:00
parent 65b016b9b7
commit 163141e3a3
4 changed files with 42 additions and 14 deletions

View File

@ -44,7 +44,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
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, writeProxy);
envoy.server.data.User user = getUser(input, socketId, writeProxy);
// Not logged in successfully
if (user == null) return;
@ -76,8 +76,8 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
}
}
private envoy.server.data.User getUser(LoginCredentials credentials, ObjectWriteProxy writeProxy) throws IOException {
return credentials.isRegistration() ? newUser(credentials) : checkForExistingUser(credentials, writeProxy);
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);
}
/**
@ -87,28 +87,28 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
* @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 {
private envoy.server.data.User checkForExistingUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.User user;
ConnectionManager connectionManager = ConnectionManager.getInstance();
long userId = credentials.getId();
long socketId = connectionManager.getSocketId(userId);
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(userId)) {
if (connectionManager.isOnline(user.getId())) {
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");
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.INCORRECT_PASSWORD));
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD));
}
return null;
}
@ -116,12 +116,18 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
/**
* @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) {
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();
user.setName(credentials.getName());
//TODO needs to be replaced later on
user.setName(credentials.getIdentifier());
user.setLastSeen(new Date());
user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(credentials.getPasswordHash());