mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-04-20 09:36:55 -04:00
deduplicate code
This commit is contained in:
@@ -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 <code>/api/</code> base resource.
|
||||
*
|
||||
* @return <code>/api/</code> URI
|
||||
* @apiNote URI is guaranteed to end on <code>/</code>
|
||||
* @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 <code>/api/</code> endpoint of this Hub instance.
|
||||
*/
|
||||
public final StringTemplate.Processor<URI, RuntimeException> API = template -> {
|
||||
var path = template.interpolate();
|
||||
var relPath = path.startsWith("/") ? path.substring(1) : path;
|
||||
return getApiBaseUrl().resolve(relPath);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,6 @@ public class ReceiveKeyController implements FxController {
|
||||
private final Lazy<Scene> accountInitializationScene;
|
||||
private final Lazy<Scene> invalidLicenseScene;
|
||||
private final HttpClient httpClient;
|
||||
private final StringTemplate.Processor<URI, RuntimeException> API_BASE = this::resolveRelativeToApiBase;
|
||||
|
||||
@Inject
|
||||
public ReceiveKeyController(@KeyLoading Vault vault, ExecutorService executor, @KeyLoading Stage window, HubConfig hubConfig, @Named("deviceId") String deviceId, @Named("bearerToken") AtomicReference<String> tokenRef, CompletableFuture<ReceivedKey> result, @FxmlScene(FxmlFile.HUB_REGISTER_DEVICE) Lazy<Scene> registerDeviceScene, @FxmlScene(FxmlFile.HUB_LEGACY_REGISTER_DEVICE) Lazy<Scene> legacyRegisterDeviceScene, @FxmlScene(FxmlFile.HUB_UNAUTHORIZED_DEVICE) Lazy<Scene> unauthorizedScene, @FxmlScene(FxmlFile.HUB_REQUIRE_ACCOUNT_INIT) Lazy<Scene> accountInitializationScene, @FxmlScene(FxmlFile.HUB_INVALID_LICENSE) Lazy<Scene> 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();
|
||||
|
||||
@@ -63,7 +63,6 @@ public class RegisterDeviceController implements FxController {
|
||||
private final P384KeyPair deviceKeyPair;
|
||||
private final CompletableFuture<ReceivedKey> result;
|
||||
private final HttpClient httpClient;
|
||||
private final StringTemplate.Processor<URI, RuntimeException> 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;
|
||||
|
||||
Reference in New Issue
Block a user