From 41c22b7840c9261b29e3fe26207ae96854f3262b Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Fri, 17 Nov 2023 13:30:40 +0100 Subject: [PATCH 01/12] Rename leftovers --- src/main/java/org/cryptomator/ui/common/FxmlFile.java | 2 +- .../cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java | 4 ++-- .../cryptomator/ui/keyloading/hub/ReceiveKeyController.java | 4 +--- .../fxml/{hub_setup_device.fxml => hub_register_device.fxml} | 0 4 files changed, 4 insertions(+), 6 deletions(-) rename src/main/resources/fxml/{hub_setup_device.fxml => hub_register_device.fxml} (100%) diff --git a/src/main/java/org/cryptomator/ui/common/FxmlFile.java b/src/main/java/org/cryptomator/ui/common/FxmlFile.java index 46542ccb9..d5308b5c0 100644 --- a/src/main/java/org/cryptomator/ui/common/FxmlFile.java +++ b/src/main/java/org/cryptomator/ui/common/FxmlFile.java @@ -23,7 +23,7 @@ public enum FxmlFile { HUB_LEGACY_REGISTER_DEVICE("/fxml/hub_legacy_register_device.fxml"), // HUB_REGISTER_SUCCESS("/fxml/hub_register_success.fxml"), // HUB_REGISTER_FAILED("/fxml/hub_register_failed.fxml"), // - HUB_SETUP_DEVICE("/fxml/hub_setup_device.fxml"), // + HUB_REGISTER_DEVICE("/fxml/hub_register_device.fxml"), // HUB_UNAUTHORIZED_DEVICE("/fxml/hub_unauthorized_device.fxml"), // HUB_REQUIRE_ACCOUNT_INIT("/fxml/hub_require_account_init.fxml"), // LOCK_FORCED("/fxml/lock_forced.fxml"), // diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java b/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java index 235fbf639..9f62a0c90 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java @@ -135,10 +135,10 @@ public abstract class HubKeyLoadingModule { } @Provides - @FxmlScene(FxmlFile.HUB_SETUP_DEVICE) + @FxmlScene(FxmlFile.HUB_REGISTER_DEVICE) @KeyLoadingScoped static Scene provideHubRegisterDeviceScene(@KeyLoading FxmlLoaderFactory fxmlLoaders) { - return fxmlLoaders.createScene(FxmlFile.HUB_SETUP_DEVICE); + return fxmlLoaders.createScene(FxmlFile.HUB_REGISTER_DEVICE); } @Provides diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/ReceiveKeyController.java b/src/main/java/org/cryptomator/ui/keyloading/hub/ReceiveKeyController.java index c0681d4bb..d00041640 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/ReceiveKeyController.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/ReceiveKeyController.java @@ -11,7 +11,6 @@ import org.cryptomator.ui.common.FxmlFile; import org.cryptomator.ui.common.FxmlScene; import org.cryptomator.ui.keyloading.KeyLoading; import org.cryptomator.ui.keyloading.KeyLoadingScoped; -import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,7 +31,6 @@ import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.time.Duration; -import java.time.Instant; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -60,7 +58,7 @@ public class ReceiveKeyController implements FxController { private final HttpClient httpClient; @Inject - public ReceiveKeyController(@KeyLoading Vault vault, ExecutorService executor, @KeyLoading Stage window, HubConfig hubConfig, @Named("deviceId") String deviceId, @Named("bearerToken") AtomicReference tokenRef, CompletableFuture result, @FxmlScene(FxmlFile.HUB_SETUP_DEVICE) Lazy setupDeviceScene, @FxmlScene(FxmlFile.HUB_LEGACY_REGISTER_DEVICE) Lazy legacyRegisterDeviceScene, @FxmlScene(FxmlFile.HUB_UNAUTHORIZED_DEVICE) Lazy unauthorizedScene, @FxmlScene(FxmlFile.HUB_REQUIRE_ACCOUNT_INIT) Lazy accountInitializationScene, @FxmlScene(FxmlFile.HUB_INVALID_LICENSE) Lazy invalidLicenseScene) { + public ReceiveKeyController(@KeyLoading Vault vault, ExecutorService executor, @KeyLoading Stage window, HubConfig hubConfig, @Named("deviceId") String deviceId, @Named("bearerToken") AtomicReference tokenRef, CompletableFuture result, @FxmlScene(FxmlFile.HUB_REGISTER_DEVICE) Lazy setupDeviceScene, @FxmlScene(FxmlFile.HUB_LEGACY_REGISTER_DEVICE) Lazy legacyRegisterDeviceScene, @FxmlScene(FxmlFile.HUB_UNAUTHORIZED_DEVICE) Lazy unauthorizedScene, @FxmlScene(FxmlFile.HUB_REQUIRE_ACCOUNT_INIT) Lazy accountInitializationScene, @FxmlScene(FxmlFile.HUB_INVALID_LICENSE) Lazy invalidLicenseScene) { this.window = window; this.hubConfig = hubConfig; this.deviceId = deviceId; diff --git a/src/main/resources/fxml/hub_setup_device.fxml b/src/main/resources/fxml/hub_register_device.fxml similarity index 100% rename from src/main/resources/fxml/hub_setup_device.fxml rename to src/main/resources/fxml/hub_register_device.fxml From 1199ef40ddcfa33305708caf74f81b554d392aec Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Fri, 17 Nov 2023 15:27:46 +0100 Subject: [PATCH 02/12] on 409 repsonse of registerDevice-request, show setupFailed with adjusted message --- .../hub/DeviceAlreadyExistsException.java | 6 ++++ .../keyloading/hub/HubKeyLoadingModule.java | 8 +++++ .../hub/RegisterDeviceController.java | 29 +++++++++---------- .../hub/RegisterFailedController.java | 21 ++++++++++---- .../resources/fxml/hub_register_device.fxml | 9 ------ .../resources/fxml/hub_register_failed.fxml | 3 +- src/main/resources/i18n/strings.properties | 5 ++-- 7 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 src/main/java/org/cryptomator/ui/keyloading/hub/DeviceAlreadyExistsException.java diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/DeviceAlreadyExistsException.java b/src/main/java/org/cryptomator/ui/keyloading/hub/DeviceAlreadyExistsException.java new file mode 100644 index 000000000..fd7cb4f5d --- /dev/null +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/DeviceAlreadyExistsException.java @@ -0,0 +1,6 @@ +package org.cryptomator.ui.keyloading.hub; + +/** + * Thrown, when Hub registerDevice-Request returns with 409 + */ +public class DeviceAlreadyExistsException extends RuntimeException {} diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java b/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java index 9f62a0c90..92e9af6f0 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java @@ -72,6 +72,14 @@ public abstract class HubKeyLoadingModule { return new CompletableFuture<>(); } + @Provides + @KeyLoadingScoped + @Named("registerException") + static AtomicReference provideRegisterException() { + return new AtomicReference<>(); + } + + @Binds @IntoMap @KeyLoadingScoped diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java index 837dc5032..c51c7a8ab 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java @@ -40,6 +40,7 @@ import java.text.ParseException; import java.time.Duration; import java.time.Instant; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutorService; @@ -55,6 +56,7 @@ public class RegisterDeviceController implements FxController { private final Stage window; private final HubConfig hubConfig; private final String bearerToken; + private final AtomicReference registerException; private final Lazy registerSuccessScene; private final Lazy registerFailedScene; private final String deviceId; @@ -62,7 +64,6 @@ public class RegisterDeviceController implements FxController { private final CompletableFuture result; private final HttpClient httpClient; - private final BooleanProperty deviceNameAlreadyExists = new SimpleBooleanProperty(false); private final BooleanProperty invalidSetupCode = new SimpleBooleanProperty(false); private final BooleanProperty workInProgress = new SimpleBooleanProperty(false); public TextField setupCodeField; @@ -70,13 +71,14 @@ public class RegisterDeviceController implements FxController { public Button registerBtn; @Inject - public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture result, @Named("bearerToken") AtomicReference bearerToken, @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) Lazy registerSuccessScene, @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) Lazy registerFailedScene) { + public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture result, @Named("bearerToken") AtomicReference bearerToken, @Named("registerException") AtomicReference registerException, @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) Lazy registerSuccessScene, @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) Lazy registerFailedScene) { this.window = window; this.hubConfig = hubConfig; this.deviceId = deviceId; this.deviceKeyPair = Objects.requireNonNull(deviceKey.get()); this.result = result; this.bearerToken = Objects.requireNonNull(bearerToken.get()); + this.registerException = registerException; this.registerSuccessScene = registerSuccessScene; this.registerFailedScene = registerFailedScene; this.window.addEventHandler(WindowEvent.WINDOW_HIDING, this::windowClosed); @@ -85,7 +87,6 @@ public class RegisterDeviceController implements FxController { public void initialize() { deviceNameField.setText(determineHostname()); - deviceNameField.textProperty().addListener(observable -> deviceNameAlreadyExists.set(false)); deviceNameField.disableProperty().bind(workInProgress); setupCodeField.textProperty().addListener(observable -> invalidSetupCode.set(false)); setupCodeField.disableProperty().bind(workInProgress); @@ -146,7 +147,7 @@ public class RegisterDeviceController implements FxController { return httpClient.sendAsync(putDeviceReq, HttpResponse.BodyHandlers.discarding()); }).whenCompleteAsync((response, throwable) -> { if (response != null) { - this.handleResponse(response); + this.handleRegisterDeviceResponse(response); } else { this.setupFailed(throwable); } @@ -170,12 +171,12 @@ public class RegisterDeviceController implements FxController { } } - private void handleResponse(HttpResponse response) { + private void handleRegisterDeviceResponse(HttpResponse response) { if (response.statusCode() == 201) { LOG.debug("Device registration for hub instance {} successful.", hubConfig.authSuccessUrl); window.setScene(registerSuccessScene.get()); } else if (response.statusCode() == 409) { - deviceNameAlreadyExists.set(true); + setupFailed(new DeviceAlreadyExistsException()); } else { setupFailed(new IllegalStateException("Unexpected http status code " + response.statusCode())); } @@ -185,7 +186,12 @@ public class RegisterDeviceController implements FxController { switch (cause) { case CompletionException e when e.getCause() instanceof JWEHelper.InvalidJweKeyException -> invalidSetupCode.set(true); default -> { - LOG.warn("Device setup failed.", cause); + if(cause instanceof DeviceAlreadyExistsException) { + LOG.debug("Device already registered in hub instance {} for different user", hubConfig.authSuccessUrl); + } else { + LOG.warn("Device setup failed.", cause); + } + registerException.set(cause); window.setScene(registerFailedScene.get()); result.completeExceptionally(cause); } @@ -202,15 +208,6 @@ public class RegisterDeviceController implements FxController { } //--- Getters & Setters - - public BooleanProperty deviceNameAlreadyExistsProperty() { - return deviceNameAlreadyExists; - } - - public boolean getDeviceNameAlreadyExists() { - return deviceNameAlreadyExists.get(); - } - public BooleanProperty invalidSetupCodeProperty() { return invalidSetupCode; } diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterFailedController.java b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterFailedController.java index 57150390c..495fda7dd 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterFailedController.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterFailedController.java @@ -1,23 +1,27 @@ package org.cryptomator.ui.keyloading.hub; -import com.nimbusds.jose.JWEObject; import org.cryptomator.ui.common.FxController; import org.cryptomator.ui.keyloading.KeyLoading; import javax.inject.Inject; +import javax.inject.Named; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.fxml.FXML; import javafx.stage.Stage; -import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; public class RegisterFailedController implements FxController { private final Stage window; - private final CompletableFuture result; + private final Throwable registerException; + private final SimpleBooleanProperty deviceAlreadyExisting; @Inject - public RegisterFailedController(@KeyLoading Stage window, CompletableFuture result) { + public RegisterFailedController(@KeyLoading Stage window, @Named("registerException") AtomicReference registerExceptionRef) { this.window = window; - this.result = result; + this.registerException = registerExceptionRef.get(); + this.deviceAlreadyExisting = new SimpleBooleanProperty(registerException instanceof DeviceAlreadyExistsException); } @FXML @@ -25,5 +29,12 @@ public class RegisterFailedController implements FxController { window.close(); } + public boolean isDeviceAlreadyExisting() { + return deviceAlreadyExisting.get(); + } + + public boolean isGenericError() { + return !deviceAlreadyExisting.get(); + } } diff --git a/src/main/resources/fxml/hub_register_device.fxml b/src/main/resources/fxml/hub_register_device.fxml index f83ad6a97..e1a1e39ff 100644 --- a/src/main/resources/fxml/hub_register_device.fxml +++ b/src/main/resources/fxml/hub_register_device.fxml @@ -57,15 +57,6 @@ - - -