From dc5d6e734eae58e6424ec575f2ead001cf57690c Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Fri, 19 Jan 2024 17:55:36 +0100 Subject: [PATCH] deduplicate code --- .../ui/keyloading/hub/HubConfig.java | 21 +++++++++++++++++++ .../keyloading/hub/ReceiveKeyController.java | 15 ++++--------- .../hub/RegisterDeviceController.java | 11 ++-------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/cryptomator/ui/keyloading/hub/HubConfig.java b/src/main/java/org/cryptomator/ui/keyloading/hub/HubConfig.java index 84cac8ed2..eefad55a2 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/HubConfig.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/HubConfig.java @@ -1,5 +1,6 @@ package org.cryptomator.ui.keyloading.hub; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.jetbrains.annotations.Nullable; @@ -18,11 +19,18 @@ public class HubConfig { @Deprecated // use apiBaseUrl + "/devices/" public String devicesResourceUrl; + /** + * A collection of String template processors to construct URIs related to this Hub instance. + */ + @JsonIgnore + public final URIProcessors URIs = new URIProcessors(); + /** * Get the URI pointing to the /api/ base resource. * * @return /api/ URI * @apiNote URI is guaranteed to end on / + * @see #URIs */ public URI getApiBaseUrl() { if (apiBaseUrl != null) { @@ -38,4 +46,17 @@ public class HubConfig { public URI getWebappBaseUrl() { return getApiBaseUrl().resolve("../app/"); } + + public class URIProcessors { + + /** + * Resolves paths relative to the /api/ endpoint of this Hub instance. + */ + public final StringTemplate.Processor API = template -> { + var path = template.interpolate(); + var relPath = path.startsWith("/") ? path.substring(1) : path; + return getApiBaseUrl().resolve(relPath); + }; + + } } 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 11840f989..3bfb4ec8e 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/ReceiveKeyController.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/ReceiveKeyController.java @@ -57,7 +57,6 @@ public class ReceiveKeyController implements FxController { private final Lazy accountInitializationScene; private final Lazy invalidLicenseScene; private final HttpClient httpClient; - private final StringTemplate.Processor API_BASE = this::resolveRelativeToApiBase; @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_REGISTER_DEVICE) Lazy registerDeviceScene, @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) { @@ -89,7 +88,7 @@ public class ReceiveKeyController implements FxController { * STEP 0 (Request): GET /api/config */ private void requestApiConfig() { - var configUri = API_BASE."config"; + var configUri = hubConfig.URIs.API."config"; var request = HttpRequest.newBuilder(configUri) // .GET() // .timeout(REQ_TIMEOUT) // @@ -123,7 +122,7 @@ public class ReceiveKeyController implements FxController { * STEP 1 (Request): GET user key for this device */ private void requestDeviceData() { - var deviceUri = API_BASE."devices/\{deviceId}"; + var deviceUri = hubConfig.URIs.API."devices/\{deviceId}"; var request = HttpRequest.newBuilder(deviceUri) // .header("Authorization", "Bearer " + bearerToken) // .GET() // @@ -163,7 +162,7 @@ public class ReceiveKeyController implements FxController { * STEP 2 (Request): GET vault key for this user */ private void requestVaultMasterkey(String encryptedUserKey) { - var vaultKeyUri = API_BASE."vaults/\{vaultId}/access-token"; + var vaultKeyUri = hubConfig.URIs.API."vaults/\{vaultId}/access-token"; var request = HttpRequest.newBuilder(vaultKeyUri) // .header("Authorization", "Bearer " + bearerToken) // .GET() // @@ -206,7 +205,7 @@ public class ReceiveKeyController implements FxController { */ @Deprecated private void requestLegacyAccessToken() { - var legacyAccessTokenUri = API_BASE."vaults/\{vaultId}/keys/\{deviceId}"; + var legacyAccessTokenUri = hubConfig.URIs.API."vaults/\{vaultId}/keys/\{deviceId}"; var request = HttpRequest.newBuilder(legacyAccessTokenUri) // .header("Authorization", "Bearer " + bearerToken) // .GET() // @@ -288,12 +287,6 @@ public class ReceiveKeyController implements FxController { } } - private URI resolveRelativeToApiBase(StringTemplate template) { - var path = template.interpolate(); - var relPath = path.startsWith("/") ? path.substring(1) : path; - return hubConfig.getApiBaseUrl().resolve(relPath); - } - private static String extractVaultId(URI vaultKeyUri) { assert vaultKeyUri.getScheme().startsWith(SCHEME_PREFIX); var path = vaultKeyUri.getPath(); 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 0db265b3b..f604cd489 100644 --- a/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java +++ b/src/main/java/org/cryptomator/ui/keyloading/hub/RegisterDeviceController.java @@ -63,7 +63,6 @@ public class RegisterDeviceController implements FxController { private final P384KeyPair deviceKeyPair; private final CompletableFuture result; private final HttpClient httpClient; - private final StringTemplate.Processor API_BASE = this::resolveRelativeToApiBase; private final BooleanProperty invalidSetupCode = new SimpleBooleanProperty(false); private final BooleanProperty workInProgress = new SimpleBooleanProperty(false); @@ -111,7 +110,7 @@ public class RegisterDeviceController implements FxController { workInProgress.set(true); - var userReq = HttpRequest.newBuilder(API_BASE."users/me") // + var userReq = HttpRequest.newBuilder(hubConfig.URIs.API."users/me") // .GET() // .timeout(REQ_TIMEOUT) // .header("Authorization", "Bearer " + bearerToken) // @@ -137,7 +136,7 @@ public class RegisterDeviceController implements FxController { var now = Instant.now().toString(); var dto = new CreateDeviceDto(deviceId, deviceNameField.getText(), BaseEncoding.base64().encode(deviceKeyPair.getPublic().getEncoded()), "DESKTOP", jwe.serialize(), now); var json = toJson(dto); - var deviceUri = API_BASE."devices/\{deviceId}"; + var deviceUri = hubConfig.URIs.API."devices/\{deviceId}"; var putDeviceReq = HttpRequest.newBuilder(deviceUri) // .PUT(HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8)) // .timeout(REQ_TIMEOUT) // @@ -205,12 +204,6 @@ public class RegisterDeviceController implements FxController { result.cancel(true); } - private URI resolveRelativeToApiBase(StringTemplate template) { - var path = template.interpolate(); - var relPath = path.startsWith("/") ? path.substring(1) : path; - return hubConfig.getApiBaseUrl().resolve(relPath); - } - //--- Getters & Setters public BooleanProperty invalidSetupCodeProperty() { return invalidSetupCode;