on 409 repsonse of registerDevice-request, show setupFailed with adjusted message

This commit is contained in:
Armin Schrenk 2023-11-17 15:27:46 +01:00
parent 41c22b7840
commit 1199ef40dd
No known key found for this signature in database
GPG Key ID: 8F2992163CBBA7FC
7 changed files with 48 additions and 33 deletions

View File

@ -0,0 +1,6 @@
package org.cryptomator.ui.keyloading.hub;
/**
* Thrown, when Hub registerDevice-Request returns with 409
*/
public class DeviceAlreadyExistsException extends RuntimeException {}

View File

@ -72,6 +72,14 @@ public abstract class HubKeyLoadingModule {
return new CompletableFuture<>(); return new CompletableFuture<>();
} }
@Provides
@KeyLoadingScoped
@Named("registerException")
static AtomicReference<Throwable> provideRegisterException() {
return new AtomicReference<>();
}
@Binds @Binds
@IntoMap @IntoMap
@KeyLoadingScoped @KeyLoadingScoped

View File

@ -40,6 +40,7 @@ import java.text.ParseException;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -55,6 +56,7 @@ public class RegisterDeviceController implements FxController {
private final Stage window; private final Stage window;
private final HubConfig hubConfig; private final HubConfig hubConfig;
private final String bearerToken; private final String bearerToken;
private final AtomicReference<Throwable> registerException;
private final Lazy<Scene> registerSuccessScene; private final Lazy<Scene> registerSuccessScene;
private final Lazy<Scene> registerFailedScene; private final Lazy<Scene> registerFailedScene;
private final String deviceId; private final String deviceId;
@ -62,7 +64,6 @@ public class RegisterDeviceController implements FxController {
private final CompletableFuture<ReceivedKey> result; private final CompletableFuture<ReceivedKey> result;
private final HttpClient httpClient; private final HttpClient httpClient;
private final BooleanProperty deviceNameAlreadyExists = new SimpleBooleanProperty(false);
private final BooleanProperty invalidSetupCode = new SimpleBooleanProperty(false); private final BooleanProperty invalidSetupCode = new SimpleBooleanProperty(false);
private final BooleanProperty workInProgress = new SimpleBooleanProperty(false); private final BooleanProperty workInProgress = new SimpleBooleanProperty(false);
public TextField setupCodeField; public TextField setupCodeField;
@ -70,13 +71,14 @@ public class RegisterDeviceController implements FxController {
public Button registerBtn; public Button registerBtn;
@Inject @Inject
public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture<ReceivedKey> result, @Named("bearerToken") AtomicReference<String> bearerToken, @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) Lazy<Scene> registerSuccessScene, @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) Lazy<Scene> registerFailedScene) { public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture<ReceivedKey> result, @Named("bearerToken") AtomicReference<String> bearerToken, @Named("registerException") AtomicReference<Throwable> registerException, @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) Lazy<Scene> registerSuccessScene, @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) Lazy<Scene> registerFailedScene) {
this.window = window; this.window = window;
this.hubConfig = hubConfig; this.hubConfig = hubConfig;
this.deviceId = deviceId; this.deviceId = deviceId;
this.deviceKeyPair = Objects.requireNonNull(deviceKey.get()); this.deviceKeyPair = Objects.requireNonNull(deviceKey.get());
this.result = result; this.result = result;
this.bearerToken = Objects.requireNonNull(bearerToken.get()); this.bearerToken = Objects.requireNonNull(bearerToken.get());
this.registerException = registerException;
this.registerSuccessScene = registerSuccessScene; this.registerSuccessScene = registerSuccessScene;
this.registerFailedScene = registerFailedScene; this.registerFailedScene = registerFailedScene;
this.window.addEventHandler(WindowEvent.WINDOW_HIDING, this::windowClosed); this.window.addEventHandler(WindowEvent.WINDOW_HIDING, this::windowClosed);
@ -85,7 +87,6 @@ public class RegisterDeviceController implements FxController {
public void initialize() { public void initialize() {
deviceNameField.setText(determineHostname()); deviceNameField.setText(determineHostname());
deviceNameField.textProperty().addListener(observable -> deviceNameAlreadyExists.set(false));
deviceNameField.disableProperty().bind(workInProgress); deviceNameField.disableProperty().bind(workInProgress);
setupCodeField.textProperty().addListener(observable -> invalidSetupCode.set(false)); setupCodeField.textProperty().addListener(observable -> invalidSetupCode.set(false));
setupCodeField.disableProperty().bind(workInProgress); setupCodeField.disableProperty().bind(workInProgress);
@ -146,7 +147,7 @@ public class RegisterDeviceController implements FxController {
return httpClient.sendAsync(putDeviceReq, HttpResponse.BodyHandlers.discarding()); return httpClient.sendAsync(putDeviceReq, HttpResponse.BodyHandlers.discarding());
}).whenCompleteAsync((response, throwable) -> { }).whenCompleteAsync((response, throwable) -> {
if (response != null) { if (response != null) {
this.handleResponse(response); this.handleRegisterDeviceResponse(response);
} else { } else {
this.setupFailed(throwable); this.setupFailed(throwable);
} }
@ -170,12 +171,12 @@ public class RegisterDeviceController implements FxController {
} }
} }
private void handleResponse(HttpResponse<Void> response) { private void handleRegisterDeviceResponse(HttpResponse<Void> response) {
if (response.statusCode() == 201) { if (response.statusCode() == 201) {
LOG.debug("Device registration for hub instance {} successful.", hubConfig.authSuccessUrl); LOG.debug("Device registration for hub instance {} successful.", hubConfig.authSuccessUrl);
window.setScene(registerSuccessScene.get()); window.setScene(registerSuccessScene.get());
} else if (response.statusCode() == 409) { } else if (response.statusCode() == 409) {
deviceNameAlreadyExists.set(true); setupFailed(new DeviceAlreadyExistsException());
} else { } else {
setupFailed(new IllegalStateException("Unexpected http status code " + response.statusCode())); setupFailed(new IllegalStateException("Unexpected http status code " + response.statusCode()));
} }
@ -185,7 +186,12 @@ public class RegisterDeviceController implements FxController {
switch (cause) { switch (cause) {
case CompletionException e when e.getCause() instanceof JWEHelper.InvalidJweKeyException -> invalidSetupCode.set(true); case CompletionException e when e.getCause() instanceof JWEHelper.InvalidJweKeyException -> invalidSetupCode.set(true);
default -> { 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()); window.setScene(registerFailedScene.get());
result.completeExceptionally(cause); result.completeExceptionally(cause);
} }
@ -202,15 +208,6 @@ public class RegisterDeviceController implements FxController {
} }
//--- Getters & Setters //--- Getters & Setters
public BooleanProperty deviceNameAlreadyExistsProperty() {
return deviceNameAlreadyExists;
}
public boolean getDeviceNameAlreadyExists() {
return deviceNameAlreadyExists.get();
}
public BooleanProperty invalidSetupCodeProperty() { public BooleanProperty invalidSetupCodeProperty() {
return invalidSetupCode; return invalidSetupCode;
} }

View File

@ -1,23 +1,27 @@
package org.cryptomator.ui.keyloading.hub; package org.cryptomator.ui.keyloading.hub;
import com.nimbusds.jose.JWEObject;
import org.cryptomator.ui.common.FxController; import org.cryptomator.ui.common.FxController;
import org.cryptomator.ui.keyloading.KeyLoading; import org.cryptomator.ui.keyloading.KeyLoading;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicReference;
public class RegisterFailedController implements FxController { public class RegisterFailedController implements FxController {
private final Stage window; private final Stage window;
private final CompletableFuture<ReceivedKey> result; private final Throwable registerException;
private final SimpleBooleanProperty deviceAlreadyExisting;
@Inject @Inject
public RegisterFailedController(@KeyLoading Stage window, CompletableFuture<ReceivedKey> result) { public RegisterFailedController(@KeyLoading Stage window, @Named("registerException") AtomicReference<Throwable> registerExceptionRef) {
this.window = window; this.window = window;
this.result = result; this.registerException = registerExceptionRef.get();
this.deviceAlreadyExisting = new SimpleBooleanProperty(registerException instanceof DeviceAlreadyExistsException);
} }
@FXML @FXML
@ -25,5 +29,12 @@ public class RegisterFailedController implements FxController {
window.close(); window.close();
} }
public boolean isDeviceAlreadyExisting() {
return deviceAlreadyExisting.get();
}
public boolean isGenericError() {
return !deviceAlreadyExisting.get();
}
} }

View File

@ -57,15 +57,6 @@
<TextField fx:id="deviceNameField" HBox.hgrow="ALWAYS"/> <TextField fx:id="deviceNameField" HBox.hgrow="ALWAYS"/>
</HBox> </HBox>
<HBox alignment="TOP_RIGHT"> <HBox alignment="TOP_RIGHT">
<Label text="%hub.register.occupiedMsg" textAlignment="RIGHT" alignment="CENTER_RIGHT" visible="${controller.deviceNameAlreadyExists}" managed="${controller.deviceNameAlreadyExists}" graphicTextGap="6">
<padding>
<Insets top="6"/>
</padding>
<graphic>
<FontAwesome5IconView glyph="TIMES" styleClass="glyph-icon-red"/>
</graphic>
</Label>
<Label text="%hub.register.invalidAccountKeyLabel" textAlignment="RIGHT" alignment="CENTER_RIGHT" visible="${controller.invalidSetupCode}" managed="${controller.invalidSetupCode}" graphicTextGap="6"> <Label text="%hub.register.invalidAccountKeyLabel" textAlignment="RIGHT" alignment="CENTER_RIGHT" visible="${controller.invalidSetupCode}" managed="${controller.invalidSetupCode}" graphicTextGap="6">
<padding> <padding>
<Insets top="6"/> <Insets top="6"/>

View File

@ -38,7 +38,8 @@
<Insets bottom="6" top="6"/> <Insets bottom="6" top="6"/>
</padding> </padding>
</Label> </Label>
<Label text="%hub.registerFailed.description" wrapText="true"/> <Label text="%hub.registerFailed.description.generic" wrapText="true" visible="${controller.genericError}" managed="${controller.genericError}"/>
<Label text="%hub.registerFailed.description.deviceAlreadyExists" wrapText="true" visible="${controller.deviceAlreadyExisting}" managed="${controller.deviceAlreadyExisting}"/>
<Region VBox.vgrow="ALWAYS" minHeight="18"/> <Region VBox.vgrow="ALWAYS" minHeight="18"/>
<ButtonBar buttonMinWidth="120" buttonOrder="+C"> <ButtonBar buttonMinWidth="120" buttonOrder="+C">

View File

@ -158,14 +158,15 @@ hub.register.message=New Device
hub.register.description=This is the first Hub access from this device. Please authorize it using your Account Key. hub.register.description=This is the first Hub access from this device. Please authorize it using your Account Key.
hub.register.nameLabel=Device Name hub.register.nameLabel=Device Name
hub.register.invalidAccountKeyLabel=Invalid Account Key hub.register.invalidAccountKeyLabel=Invalid Account Key
hub.register.occupiedMsg=Name already in use hub.register.occupiedMsg=Device already registered by another user
hub.register.registerBtn=Confirm hub.register.registerBtn=Confirm
### Registration Success ### Registration Success
hub.registerSuccess.message=Device named hub.registerSuccess.message=Device named
hub.registerSuccess.description=To access the vault, your device needs to be authorized by the vault owner. hub.registerSuccess.description=To access the vault, your device needs to be authorized by the vault owner.
### Registration Failed ### Registration Failed
hub.registerFailed.message=Device naming failed hub.registerFailed.message=Device naming failed
hub.registerFailed.description=An error was thrown in the naming process. For more details, look into the application log. hub.registerFailed.description.generic=An error was thrown in the naming process. For more details, look into the application log.
hub.registerFailed.description.deviceAlreadyExists=This device is already registered for a different user. Try to change the user account or use a different device.
### Unauthorized ### Unauthorized
hub.unauthorized.message=Access denied hub.unauthorized.message=Access denied
hub.unauthorized.description=Your device has not yet been authorized to access this vault. Ask the vault owner to authorize it. hub.unauthorized.description=Your device has not yet been authorized to access this vault. Ask the vault owner to authorize it.