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:
parent
48e12dcdfe
commit
d967c88e5b
@ -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,23 +76,57 @@ 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()) {
|
/**
|
||||||
user = new envoy.server.data.User();
|
* @param credentials the input to evaluate
|
||||||
user.setName(credentials.getName());
|
* @param writeProxy the {@link ObjectWriteProxy} to use if login was not
|
||||||
user.setLastSeen(new Date());
|
* successful
|
||||||
user.setStatus(User.UserStatus.ONLINE);
|
* @throws IOException if sending the failed login back to the client failed
|
||||||
user.setPasswordHash(credentials.getPasswordHash());
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user));
|
*/
|
||||||
persistenceManager.addUser(user);
|
private envoy.server.data.User checkForExistingUser(LoginCredentials credentials, ObjectWriteProxy writeProxy) throws IOException {
|
||||||
} else {
|
envoy.server.data.User user;
|
||||||
user = persistenceManager.getUserByName(credentials.getName());
|
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||||
// TODO: Implement error when user does not exist
|
long userId = credentials.getId();
|
||||||
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) // TODO: Wrong Password Response
|
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;
|
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.setName(credentials.getName());
|
||||||
|
user.setLastSeen(new Date());
|
||||||
|
user.setStatus(User.UserStatus.ONLINE);
|
||||||
|
user.setPasswordHash(credentials.getPasswordHash());
|
||||||
|
user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user));
|
||||||
|
persistenceManager.addUser(user);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user