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 d967c88e5b
commit aa52cddf6a
4 changed files with 42 additions and 14 deletions

View File

@ -1,8 +1,10 @@
package envoy.server;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -98,4 +100,14 @@ public class ConnectionManager implements ISocketIdListener {
* @since Envoy Server Standalone v0.1-alpha
*/
public boolean isOnline(long userId) { return sockets.containsKey(userId); }
/**
* @return the userId of all users who are currently online
* @since Envoy Server Standalone v0.1-alpha
*/
public List<Long> getOnlineUsers() {
List<Long> onlineUsers = new ArrayList<>();
sockets.forEach((userId, unimportant) -> onlineUsers.add(userId));
return onlineUsers;
}
}

View File

@ -1,11 +1,14 @@
package envoy.server.database;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import envoy.data.User.UserStatus;
import envoy.server.ConnectionManager;
import envoy.server.data.ConfigItem;
import envoy.server.data.Message;
import envoy.server.data.User;
@ -32,7 +35,14 @@ public class PersistenceManager {
*/
private PersistenceManager() {
transaction.begin();
Runtime.getRuntime().addShutdownHook(new Thread(() -> transaction.commit()));
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
ConnectionManager.getInstance()
.getOnlineUsers()
.stream()
.map(this::getUserById)
.forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); updateUser(user); });
transaction.commit();
}));
}
/**

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());

View File

@ -37,7 +37,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
// Update the message status to RECEIVED
message.setReceivedDate(new Date());
message.nextStatus();
writeProxy.write(connectionManager.getSocketId(message.getSenderId()), new MessageStatusChangeEvent(message));
writeProxy.write(socketId, new MessageStatusChangeEvent(message));
} catch (IOException e) {
System.err.println("Recipient online. Failed to send message" + message.getId());
e.printStackTrace();