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:
		| @@ -3,14 +3,18 @@ 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; | ||||
| @@ -40,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); | ||||
| 		envoy.server.data.User user = getUser(input, writeProxy); | ||||
|  | ||||
| 		// Not logged in successfully | ||||
| 		if (user == null) return; | ||||
| @@ -72,23 +76,57 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	private envoy.server.data.User getUser(LoginCredentials credentials) { | ||||
| 		envoy.server.data.User user; | ||||
| 	private envoy.server.data.User getUser(LoginCredentials credentials, ObjectWriteProxy writeProxy) throws IOException { | ||||
| 		return credentials.isRegistration() ? newUser(credentials) : checkForExistingUser(credentials, writeProxy); | ||||
| 	} | ||||
|  | ||||
| 		if (credentials.isRegistration()) { | ||||
| 			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); | ||||
| 		} 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 | ||||
| 	/** | ||||
| 	 * @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.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; | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 delvh
					delvh