Merge pull request #43 from informatik-ag-ngl/f/user_name_validation

Validate user name during registration
This commit is contained in:
Kai S. K. Engelbart 2020-06-27 09:24:24 +00:00 committed by GitHub
commit 97b7b8070f

View File

@ -20,6 +20,7 @@ import envoy.server.data.PersistenceManager;
import envoy.server.data.User; import envoy.server.data.User;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.Bounds;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
@ -38,7 +39,7 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class); private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class);
@Override @Override
public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException {
@ -76,25 +77,31 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
return; return;
} }
} else { } else {
try { // Validate user name
// Checking that no user already has this identifier if (!Bounds.isValidContactName(credentials.getIdentifier())) {
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier()); logger.info("The requested user name is not valid.");
writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR));
// 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; 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);
} }
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."); logger.info(user + " successfully authenticated.");