From cd48ce67553e65c9b6bcdbdad6cb4c96d48543d7 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 26 Jun 2020 22:04:40 +0200 Subject: [PATCH] Validate user name during registration --- .../processors/LoginCredentialProcessor.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index ad7addd..0a248c9 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -20,6 +20,7 @@ import envoy.server.data.PersistenceManager; import envoy.server.data.User; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; +import envoy.util.Bounds; import envoy.util.EnvoyLog; /** @@ -38,7 +39,7 @@ public final class LoginCredentialProcessor implements ObjectProcessor()); - persistenceManager.addContact(user); - logger.info("Registered new " + user); } + try { + // Checking that no user already has this identifier + PersistenceManager.getInstance().getUserByName(credentials.getIdentifier()); + + // This code only gets executed if this user already exists + logger.info("The requested user already exists."); + writeProxy.write(socketID, new HandshakeRejection(USERNAME_TAKEN)); + return; + } catch (NoResultException e) { + // Creation of a new user + user = new User(); + user.setName(credentials.getIdentifier()); + user.setLastSeen(LocalDateTime.now()); + user.setStatus(ONLINE); + user.setPasswordHash(credentials.getPasswordHash()); + user.setContacts(new HashSet<>()); + persistenceManager.addContact(user); + logger.info("Registered new " + user); + } } logger.info(user + " successfully authenticated.");