X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/de273586b4106171a3940ce5ec2c2b3712430c8c..3cf7721cd7112c4997c35ac95dca7fad305dfd12:/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java diff --git a/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java b/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java index 82d8a37f..ca6e305c 100644 --- a/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java +++ b/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java @@ -1,10 +1,12 @@ package org.asamk.signal.manager; +import org.asamk.signal.manager.api.InvalidDeviceLinkException; import org.whispersystems.libsignal.InvalidKeyException; import org.whispersystems.libsignal.ecc.Curve; import org.whispersystems.libsignal.ecc.ECPublicKey; import java.net.URI; +import java.net.URISyntaxException; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -14,12 +16,9 @@ import java.util.Map; import static org.whispersystems.signalservice.internal.util.Util.isEmpty; -public class DeviceLinkInfo { +public record DeviceLinkInfo(String deviceIdentifier, ECPublicKey deviceKey) { - final String deviceIdentifier; - final ECPublicKey deviceKey; - - public static DeviceLinkInfo parseDeviceLinkUri(URI linkUri) throws InvalidKeyException { + public static DeviceLinkInfo parseDeviceLinkUri(URI linkUri) throws InvalidDeviceLinkException { final var rawQuery = linkUri.getRawQuery(); if (isEmpty(rawQuery)) { throw new RuntimeException("Invalid device link uri"); @@ -30,16 +29,21 @@ public class DeviceLinkInfo { var publicKeyEncoded = query.get("pub_key"); if (isEmpty(deviceIdentifier) || isEmpty(publicKeyEncoded)) { - throw new RuntimeException("Invalid device link uri"); + throw new InvalidDeviceLinkException("Invalid device link uri"); } final byte[] publicKeyBytes; try { publicKeyBytes = Base64.getDecoder().decode(publicKeyEncoded); } catch (IllegalArgumentException e) { - throw new RuntimeException("Invalid device link uri", e); + throw new InvalidDeviceLinkException("Invalid device link uri", e); + } + ECPublicKey deviceKey; + try { + deviceKey = Curve.decodePoint(publicKeyBytes, 0); + } catch (InvalidKeyException e) { + throw new InvalidDeviceLinkException("Invalid device link", e); } - var deviceKey = Curve.decodePoint(publicKeyBytes, 0); return new DeviceLinkInfo(deviceIdentifier, deviceKey); } @@ -56,16 +60,15 @@ public class DeviceLinkInfo { return map; } - public DeviceLinkInfo(final String deviceIdentifier, final ECPublicKey deviceKey) { - this.deviceIdentifier = deviceIdentifier; - this.deviceKey = deviceKey; - } - - public String createDeviceLinkUri() { + public URI createDeviceLinkUri() { final var deviceKeyString = Base64.getEncoder().encodeToString(deviceKey.serialize()).replace("=", ""); - return "tsdevice:/?uuid=" - + URLEncoder.encode(deviceIdentifier, StandardCharsets.UTF_8) - + "&pub_key=" - + URLEncoder.encode(deviceKeyString, StandardCharsets.UTF_8); + try { + return new URI("sgnl://linkdevice?uuid=" + + URLEncoder.encode(deviceIdentifier, StandardCharsets.UTF_8) + + "&pub_key=" + + URLEncoder.encode(deviceKeyString, StandardCharsets.UTF_8)); + } catch (URISyntaxException e) { + throw new AssertionError(e); + } } }