Token Based Authentication #30
@ -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<Message>());
|
||||
cacheMap.put(GroupMessage.class, new Cache<GroupMessage>());
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,10 +122,20 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
|
||||
user.setStatus(ONLINE);
|
||||
UserStatusChangeProcessor.updateUserStatus(user);
|
||||
|
||||
// Generate a new token if requested
|
||||
// Process token request
|
||||
if (credentials.requestToken()) {
|
||||
String token = AuthTokenGenerator.nextToken();
|
||||
user.setAuthToken(token);
|
||||
String token;
|
||||
|
||||
if (user.getAuthToken() != null && user.getAuthTokenExpiration().isAfter(Instant.now())) {
|
||||
|
||||
// Reuse existing token and delay expiration date
|
||||
token = user.getAuthToken();
|
||||
} else {
|
||||
|
||||
// Generate new token
|
||||
token = AuthTokenGenerator.nextToken();
|
||||
user.setAuthToken(token);
|
||||
}
|
||||
user.setAuthTokenExpiration(Instant.now().plus(ServerConfig.getInstance().getAuthTokenExpiration().longValue(), ChronoUnit.DAYS));
|
||||
persistenceManager.updateContact(user);
|
||||
writeProxy.write(socketID, new NewAuthToken(token));
|
||||
|
Reference in New Issue
Block a user