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:
parent
65b016b9b7
commit
163141e3a3
@ -1,8 +1,10 @@
|
|||||||
package envoy.server;
|
package envoy.server;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -98,4 +100,14 @@ public class ConnectionManager implements ISocketIdListener {
|
|||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public boolean isOnline(long userId) { return sockets.containsKey(userId); }
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
package envoy.server.database;
|
package envoy.server.database;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityTransaction;
|
import javax.persistence.EntityTransaction;
|
||||||
import javax.persistence.Persistence;
|
import javax.persistence.Persistence;
|
||||||
|
|
||||||
|
import envoy.data.User.UserStatus;
|
||||||
|
import envoy.server.ConnectionManager;
|
||||||
import envoy.server.data.ConfigItem;
|
import envoy.server.data.ConfigItem;
|
||||||
import envoy.server.data.Message;
|
import envoy.server.data.Message;
|
||||||
import envoy.server.data.User;
|
import envoy.server.data.User;
|
||||||
@ -32,7 +35,14 @@ public class PersistenceManager {
|
|||||||
*/
|
*/
|
||||||
private PersistenceManager() {
|
private PersistenceManager() {
|
||||||
transaction.begin();
|
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();
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,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, writeProxy);
|
envoy.server.data.User user = getUser(input, socketId, writeProxy);
|
||||||
|
|
||||||
// Not logged in successfully
|
// Not logged in successfully
|
||||||
if (user == null) return;
|
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 {
|
private envoy.server.data.User getUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||||
return credentials.isRegistration() ? newUser(credentials) : checkForExistingUser(credentials, writeProxy);
|
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
|
* @throws IOException if sending the failed login back to the client failed
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @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;
|
envoy.server.data.User user;
|
||||||
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||||
long userId = credentials.getId();
|
String userIdentifier = credentials.getIdentifier();
|
||||||
long socketId = connectionManager.getSocketId(userId);
|
|
||||||
try {
|
try {
|
||||||
|
//TODO will need to be replaced with the Identifier once implemented
|
||||||
|
user = persistenceManager.getUserByName(userIdentifier);
|
||||||
// Checking if user is already online
|
// Checking if user is already online
|
||||||
if (connectionManager.isOnline(userId)) {
|
if (connectionManager.isOnline(user.getId())) {
|
||||||
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE));
|
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
user = persistenceManager.getUserById(userId);
|
|
||||||
// Evaluating the correctness of the password hash
|
// Evaluating the correctness of the password hash
|
||||||
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash()))
|
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;
|
return user;
|
||||||
} catch (NoResultException e) {
|
} catch (NoResultException e) {
|
||||||
// Checking if user exists
|
// Checking if user exists
|
||||||
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.USER_DOES_NOT_EXIST));
|
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.USER_DOES_NOT_EXIST));
|
||||||
} catch (InputMismatchException e) {
|
} catch (InputMismatchException e) {
|
||||||
// Checking if the given password hash is correct
|
// 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;
|
return null;
|
||||||
}
|
}
|
||||||
@ -116,12 +116,18 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
|||||||
/**
|
/**
|
||||||
* @param credentials the credentials upon which to create the new {@link User}
|
* @param credentials the credentials upon which to create the new {@link User}
|
||||||
* @return the newly created {@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
|
* @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;
|
envoy.server.data.User user;
|
||||||
user = new envoy.server.data.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.setLastSeen(new Date());
|
||||||
user.setStatus(User.UserStatus.ONLINE);
|
user.setStatus(User.UserStatus.ONLINE);
|
||||||
user.setPasswordHash(credentials.getPasswordHash());
|
user.setPasswordHash(credentials.getPasswordHash());
|
||||||
|
@ -37,7 +37,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
|
|||||||
// Update the message status to RECEIVED
|
// Update the message status to RECEIVED
|
||||||
message.setReceivedDate(new Date());
|
message.setReceivedDate(new Date());
|
||||||
message.nextStatus();
|
message.nextStatus();
|
||||||
writeProxy.write(connectionManager.getSocketId(message.getSenderId()), new MessageStatusChangeEvent(message));
|
writeProxy.write(socketId, new MessageStatusChangeEvent(message));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Recipient online. Failed to send message" + message.getId());
|
System.err.println("Recipient online. Failed to send message" + message.getId());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Reference in New Issue
Block a user