From 3e594c1fbd39748246d4241b42ba03812ab743f9 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 19 Sep 2020 13:33:18 +0200 Subject: [PATCH] Handle handshake rejections on invalid token, reuse not expired tokens --- .../src/main/java/envoy/client/ui/Startup.java | 18 +++++++++++++----- .../processors/LoginCredentialProcessor.java | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/client/src/main/java/envoy/client/ui/Startup.java b/client/src/main/java/envoy/client/ui/Startup.java index c3dea29..0b94d8a 100644 --- a/client/src/main/java/envoy/client/ui/Startup.java +++ b/client/src/main/java/envoy/client/ui/Startup.java @@ -93,8 +93,9 @@ public final class Startup extends Application { logger.info("Attempting authentication with token..."); localDB.initializeUserStorage(); localDB.loadUserData(); - performHandshake(LoginCredentials.loginWithToken(localDB.getUser().getName(), localDB.getAuthToken(), VERSION, localDB.getLastSync())); - // TODO: handle unsuccessful handshake + if (!performHandshake( + LoginCredentials.loginWithToken(localDB.getUser().getName(), localDB.getAuthToken(), VERSION, localDB.getLastSync()))) + sceneContext.load(SceneInfo.LOGIN_SCENE); } else { // Load login scene @@ -106,9 +107,10 @@ public final class Startup extends Application { * Tries to perform a Handshake with the server. * * @param credentials the credentials to use for the handshake + * @return whether the handshake was successful or offline mode could be entered * @since Envoy Client v0.2-beta */ - public static void performHandshake(LoginCredentials credentials) { + public static boolean performHandshake(LoginCredentials credentials) { final var cacheMap = new CacheMap(); cacheMap.put(Message.class, new Cache()); cacheMap.put(GroupMessage.class, new Cache()); @@ -120,10 +122,13 @@ public final class Startup extends Application { if (client.isOnline()) { loadChatScene(); client.initReceiver(localDB, cacheMap); + return true; + } else { + return false; } } catch (IOException | InterruptedException | TimeoutException e) { logger.log(Level.INFO, "Could not connect to server. Entering offline mode..."); - attemptOfflineMode(credentials.getIdentifier()); + return attemptOfflineMode(credentials.getIdentifier()); } } @@ -132,9 +137,10 @@ public final class Startup extends Application { * for a given user. * * @param identifier the identifier of the user - currently his username + * @return whether the offline mode could be entered * @since Envoy Client v0.2-beta */ - public static void attemptOfflineMode(String identifier) { + public static boolean attemptOfflineMode(String identifier) { try { // Try entering offline mode localDB.loadUsers(); @@ -142,10 +148,12 @@ public final class Startup extends Application { if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); client.setSender(clientUser); loadChatScene(); + return true; } catch (final Exception e) { new Alert(AlertType.ERROR, "Client error: " + e).showAndWait(); logger.log(Level.SEVERE, "Offline mode could not be loaded: ", e); System.exit(1); + return false; } } diff --git a/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 5f5cf01..b14346f 100755 --- a/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -122,10 +122,20 @@ public final class LoginCredentialProcessor implements ObjectProcessor