diff --git a/src/main/java/org/cryptomator/ui/common/FxmlFile.java b/src/main/java/org/cryptomator/ui/common/FxmlFile.java index b9e6a990e..0ab7375d7 100644 --- a/src/main/java/org/cryptomator/ui/common/FxmlFile.java +++ b/src/main/java/org/cryptomator/ui/common/FxmlFile.java @@ -16,6 +16,8 @@ public enum FxmlFile { HUB_AUTH_FLOW("/fxml/hub_auth_flow.fxml"), // HUB_RECEIVE_KEY("/fxml/hub_receive_key.fxml"), // HUB_REGISTER_DEVICE("/fxml/hub_register_device.fxml"), // + HUB_REGISTER_SUCCESS("/fxml/hub_register_success.fxml"), // + HUB_REGISTER_FAILED("/fxml/hub_register_failed.fxml"), HUB_UNAUTHORIZED_DEVICE("/fxml/hub_unauthorized_device.fxml"), // LOCK_FORCED("/fxml/lock_forced.fxml"), // LOCK_FAILED("/fxml/lock_failed.fxml"), // diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/AuthFlowController.java b/src/main/java/org/cryptomator/ui/keyloading/hub/AuthFlowController.java index 23ecfff91..5765f56e0 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/AuthFlowController.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/AuthFlowController.java @@ -38,7 +38,6 @@ public class AuthFlowController implements FxController { private final CompletableFuture result; private final Lazy receiveKeyScene; private final ObjectProperty authUri; - private final StringBinding authHost; private AuthFlowTask task; @Inject @@ -52,7 +51,6 @@ public class AuthFlowController implements FxController { this.result = result; this.receiveKeyScene = receiveKeyScene; this.authUri = new SimpleObjectProperty<>(); - this.authHost = Bindings.createStringBinding(this::getAuthHost, authUri); this.window.addEventHandler(WindowEvent.WINDOW_HIDING, this::windowClosed); } @@ -100,19 +98,4 @@ public class AuthFlowController implements FxController { result.completeExceptionally(exception); } - /* Getter/Setter */ - - public StringBinding authHostProperty() { - return authHost; - } - - public String getAuthHost() { - var uri = authUri.get(); - if (uri == null) { - return ""; - } else { - return uri.getAuthority().toString(); - } - } - } 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 4d4d810a3..587810021 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingModule.java @@ -43,6 +43,14 @@ public abstract class HubKeyLoadingModule { } } + @Provides + @KeyLoadingScoped + @Named("windowTitle") + static String provideWindowTitle(@KeyLoading Vault vault, ResourceBundle resourceBundle) { + return String.format(resourceBundle.getString("unlock.title"), vault.getDisplayName()); + } + + @Provides @KeyLoadingScoped @Named("deviceId") @@ -98,6 +106,20 @@ public abstract class HubKeyLoadingModule { return fxmlLoaders.createScene(FxmlFile.HUB_REGISTER_DEVICE); } + @Provides + @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) + @KeyLoadingScoped + static Scene provideHubRegisterSuccessScene(@KeyLoading FxmlLoaderFactory fxmlLoaders) { + return fxmlLoaders.createScene(FxmlFile.HUB_REGISTER_SUCCESS); + } + + @Provides + @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) + @KeyLoadingScoped + static Scene provideHubRegisterFailedScene(@KeyLoading FxmlLoaderFactory fxmlLoaders) { + return fxmlLoaders.createScene(FxmlFile.HUB_REGISTER_FAILED); + } + @Provides @FxmlScene(FxmlFile.HUB_UNAUTHORIZED_DEVICE) @KeyLoadingScoped @@ -127,6 +149,16 @@ public abstract class HubKeyLoadingModule { @FxControllerKey(RegisterDeviceController.class) abstract FxController bindRegisterDeviceController(RegisterDeviceController controller); + @Binds + @IntoMap + @FxControllerKey(RegisterSuccessController.class) + abstract FxController bindRegisterSuccessController(RegisterSuccessController controller); + + @Binds + @IntoMap + @FxControllerKey(RegisterFailedController.class) + abstract FxController bindRegisterFailedController(RegisterFailedController controller); + @Binds @IntoMap @FxControllerKey(UnauthorizedDeviceController.class) diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingStrategy.java b/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingStrategy.java index 10d1399e2..dcb5722d2 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingStrategy.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/HubKeyLoadingStrategy.java @@ -13,6 +13,7 @@ import org.cryptomator.ui.keyloading.KeyLoadingStrategy; import org.cryptomator.ui.unlock.UnlockCancelledException; import javax.inject.Inject; +import javax.inject.Named; import javafx.application.Platform; import javafx.scene.Scene; import javafx.stage.Stage; @@ -35,8 +36,9 @@ public class HubKeyLoadingStrategy implements KeyLoadingStrategy { private final DeviceKey deviceKey; @Inject - public HubKeyLoadingStrategy(@KeyLoading Stage window, @FxmlScene(FxmlFile.HUB_AUTH_FLOW) Lazy authFlowScene, CompletableFuture result, DeviceKey deviceKey) { + public HubKeyLoadingStrategy(@KeyLoading Stage window, @FxmlScene(FxmlFile.HUB_AUTH_FLOW) Lazy authFlowScene, CompletableFuture result, DeviceKey deviceKey, @Named("windowTitle") String windowTitle) { this.window = window; + window.setTitle(windowTitle); this.authFlowScene = authFlowScene; this.result = result; this.deviceKey = deviceKey; 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 2bdb772a6..85af2c66d 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/ReceiveKeyController.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/ReceiveKeyController.java @@ -35,7 +35,6 @@ import java.util.concurrent.atomic.AtomicReference; @KeyLoadingScoped public class ReceiveKeyController implements FxController { - private static final Logger LOG = LoggerFactory.getLogger(ReceiveKeyController.class); private static final String SCHEME_PREFIX = "hub+"; private final Stage window; 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 2c346d977..b63a8676a 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java @@ -6,9 +6,12 @@ import com.google.common.io.BaseEncoding; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.nimbusds.jose.JWEObject; +import dagger.Lazy; import org.cryptomator.common.settings.DeviceKey; import org.cryptomator.cryptolib.common.P384KeyPair; import org.cryptomator.ui.common.FxController; +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.slf4j.Logger; @@ -18,9 +21,12 @@ import javax.inject.Inject; import javax.inject.Named; import javafx.application.Platform; import javafx.fxml.FXML; +import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.stage.Stage; import javafx.stage.WindowEvent; +import java.io.IOException; +import java.net.InetAddress; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -40,6 +46,8 @@ public class RegisterDeviceController implements FxController { private final Stage window; private final HubConfig hubConfig; private final String bearerToken; + private final Lazy registerSuccessScene; + private final Lazy registerFailedScene; private final String deviceId; private final P384KeyPair keyPair; private final CompletableFuture result; @@ -49,18 +57,33 @@ public class RegisterDeviceController implements FxController { public TextField deviceNameField; @Inject - public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture result, @Named("bearerToken") AtomicReference bearerToken) { + 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) { this.window = window; this.hubConfig = hubConfig; this.deviceId = deviceId; this.keyPair = Objects.requireNonNull(deviceKey.get()); this.result = result; this.bearerToken = Objects.requireNonNull(bearerToken.get()); + this.registerSuccessScene = registerSuccessScene; + this.registerFailedScene = registerFailedScene; this.jwt = JWT.decode(this.bearerToken); this.window.addEventHandler(WindowEvent.WINDOW_HIDING, this::windowClosed); this.httpClient = HttpClient.newBuilder().executor(executor).build(); } + public void initialize() { + deviceNameField.setText(determineHostname()); + } + + private String determineHostname() { + try { + var hostName = InetAddress.getLocalHost().getHostName(); + return Objects.requireNonNullElse(hostName, ""); + } catch (IOException e) { + return ""; + } + } + @FXML public void register() { var keyUri = URI.create(hubConfig.devicesResourceUrl + deviceId); @@ -75,18 +98,25 @@ public class RegisterDeviceController implements FxController { .header("Content-Type", "application/json").PUT(HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8)) // .build(); httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding()) // - .thenAcceptAsync(this::registrationSucceeded, Platform::runLater) // - .exceptionally(this::registrationFailed); + .handleAsync((response, throwable) -> { + if (response != null) { + this.registrationSucceeded(response); + } else { + this.registrationFailed(throwable); + } + return null; + }, Platform::runLater); } private void registrationSucceeded(HttpResponse voidHttpResponse) { - LOG.info("Registered!"); - window.close(); // TODO: show visual feedback "please wait for device authorization" + LOG.debug("Device registration for hub instance {} successful.", hubConfig.authSuccessUrl); + window.setScene(registerSuccessScene.get()); } - private Void registrationFailed(Throwable cause) { + private void registrationFailed(Throwable cause) { + LOG.warn("Device registration failed.", cause); + window.setScene(registerFailedScene.get()); result.completeExceptionally(cause); - return null; } @FXML diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterFailedController.java b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterFailedController.java new file mode 100644 index 000000000..8a4278d72 --- /dev/null +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterFailedController.java @@ -0,0 +1,29 @@ +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 javafx.fxml.FXML; +import javafx.stage.Stage; +import java.util.concurrent.CompletableFuture; + +public class RegisterFailedController implements FxController { + + private final Stage window; + private final CompletableFuture result; + + @Inject + public RegisterFailedController(@KeyLoading Stage window, CompletableFuture result) { + this.window = window; + this.result = result; + } + + @FXML + public void close() { + window.close(); + } + + +} diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterSuccessController.java b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterSuccessController.java new file mode 100644 index 000000000..bba13516c --- /dev/null +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterSuccessController.java @@ -0,0 +1,24 @@ +package org.cryptomator.ui.keyloading.hub; + +import org.cryptomator.ui.common.FxController; +import org.cryptomator.ui.keyloading.KeyLoading; + +import javax.inject.Inject; +import javafx.fxml.FXML; +import javafx.stage.Stage; + +public class RegisterSuccessController implements FxController { + + private final Stage window; + + @Inject + public RegisterSuccessController(@KeyLoading Stage window) { + this.window = window; + } + + @FXML + public void close() { + window.close(); + } + +} diff --git a/src/main/resources/fxml/hub_auth_flow.fxml b/src/main/resources/fxml/hub_auth_flow.fxml index a6d086ea3..40bb9986a 100644 --- a/src/main/resources/fxml/hub_auth_flow.fxml +++ b/src/main/resources/fxml/hub_auth_flow.fxml @@ -1,37 +1,58 @@ + - - + + + + - + - + + spacing="12" + alignment="TOP_LEFT"> - - - - - - - - - + + + + + + + + + - + + +