mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Add InvalidDeviceLinkException exception
This commit is contained in:
parent
d783df510a
commit
3cf7721cd7
7 changed files with 54 additions and 25 deletions
|
@ -1,5 +1,6 @@
|
||||||
package org.asamk.signal.manager;
|
package org.asamk.signal.manager;
|
||||||
|
|
||||||
|
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
|
||||||
import org.whispersystems.libsignal.InvalidKeyException;
|
import org.whispersystems.libsignal.InvalidKeyException;
|
||||||
import org.whispersystems.libsignal.ecc.Curve;
|
import org.whispersystems.libsignal.ecc.Curve;
|
||||||
import org.whispersystems.libsignal.ecc.ECPublicKey;
|
import org.whispersystems.libsignal.ecc.ECPublicKey;
|
||||||
|
@ -17,7 +18,7 @@ import static org.whispersystems.signalservice.internal.util.Util.isEmpty;
|
||||||
|
|
||||||
public record DeviceLinkInfo(String deviceIdentifier, ECPublicKey deviceKey) {
|
public record DeviceLinkInfo(String deviceIdentifier, ECPublicKey deviceKey) {
|
||||||
|
|
||||||
public static DeviceLinkInfo parseDeviceLinkUri(URI linkUri) throws InvalidKeyException {
|
public static DeviceLinkInfo parseDeviceLinkUri(URI linkUri) throws InvalidDeviceLinkException {
|
||||||
final var rawQuery = linkUri.getRawQuery();
|
final var rawQuery = linkUri.getRawQuery();
|
||||||
if (isEmpty(rawQuery)) {
|
if (isEmpty(rawQuery)) {
|
||||||
throw new RuntimeException("Invalid device link uri");
|
throw new RuntimeException("Invalid device link uri");
|
||||||
|
@ -28,16 +29,21 @@ public record DeviceLinkInfo(String deviceIdentifier, ECPublicKey deviceKey) {
|
||||||
var publicKeyEncoded = query.get("pub_key");
|
var publicKeyEncoded = query.get("pub_key");
|
||||||
|
|
||||||
if (isEmpty(deviceIdentifier) || isEmpty(publicKeyEncoded)) {
|
if (isEmpty(deviceIdentifier) || isEmpty(publicKeyEncoded)) {
|
||||||
throw new RuntimeException("Invalid device link uri");
|
throw new InvalidDeviceLinkException("Invalid device link uri");
|
||||||
}
|
}
|
||||||
|
|
||||||
final byte[] publicKeyBytes;
|
final byte[] publicKeyBytes;
|
||||||
try {
|
try {
|
||||||
publicKeyBytes = Base64.getDecoder().decode(publicKeyEncoded);
|
publicKeyBytes = Base64.getDecoder().decode(publicKeyEncoded);
|
||||||
} catch (IllegalArgumentException e) {
|
} 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);
|
return new DeviceLinkInfo(deviceIdentifier, deviceKey);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.asamk.signal.manager;
|
||||||
import org.asamk.signal.manager.api.Device;
|
import org.asamk.signal.manager.api.Device;
|
||||||
import org.asamk.signal.manager.api.Group;
|
import org.asamk.signal.manager.api.Group;
|
||||||
import org.asamk.signal.manager.api.Identity;
|
import org.asamk.signal.manager.api.Identity;
|
||||||
|
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
|
||||||
import org.asamk.signal.manager.api.Message;
|
import org.asamk.signal.manager.api.Message;
|
||||||
import org.asamk.signal.manager.api.Pair;
|
import org.asamk.signal.manager.api.Pair;
|
||||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||||
|
@ -23,7 +24,6 @@ import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
|
||||||
import org.asamk.signal.manager.storage.recipients.Contact;
|
import org.asamk.signal.manager.storage.recipients.Contact;
|
||||||
import org.asamk.signal.manager.storage.recipients.Profile;
|
import org.asamk.signal.manager.storage.recipients.Profile;
|
||||||
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
||||||
import org.whispersystems.libsignal.InvalidKeyException;
|
|
||||||
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
|
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
|
||||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
|
||||||
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
|
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
|
||||||
|
@ -117,7 +117,7 @@ public interface Manager extends Closeable {
|
||||||
|
|
||||||
void removeLinkedDevices(long deviceId) throws IOException;
|
void removeLinkedDevices(long deviceId) throws IOException;
|
||||||
|
|
||||||
void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException;
|
void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException;
|
||||||
|
|
||||||
void setRegistrationLockPin(Optional<String> pin) throws IOException, UnauthenticatedResponseException;
|
void setRegistrationLockPin(Optional<String> pin) throws IOException, UnauthenticatedResponseException;
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import org.asamk.signal.manager.actions.HandleAction;
|
||||||
import org.asamk.signal.manager.api.Device;
|
import org.asamk.signal.manager.api.Device;
|
||||||
import org.asamk.signal.manager.api.Group;
|
import org.asamk.signal.manager.api.Group;
|
||||||
import org.asamk.signal.manager.api.Identity;
|
import org.asamk.signal.manager.api.Identity;
|
||||||
|
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
|
||||||
import org.asamk.signal.manager.api.Message;
|
import org.asamk.signal.manager.api.Message;
|
||||||
import org.asamk.signal.manager.api.Pair;
|
import org.asamk.signal.manager.api.Pair;
|
||||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||||
|
@ -427,22 +428,28 @@ public class ManagerImpl implements Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException {
|
public void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException {
|
||||||
var info = DeviceLinkInfo.parseDeviceLinkUri(linkUri);
|
var info = DeviceLinkInfo.parseDeviceLinkUri(linkUri);
|
||||||
|
|
||||||
addDevice(info.deviceIdentifier(), info.deviceKey());
|
addDevice(info.deviceIdentifier(), info.deviceKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
|
private void addDevice(
|
||||||
|
String deviceIdentifier, ECPublicKey deviceKey
|
||||||
|
) throws IOException, InvalidDeviceLinkException {
|
||||||
var identityKeyPair = account.getIdentityKeyPair();
|
var identityKeyPair = account.getIdentityKeyPair();
|
||||||
var verificationCode = dependencies.getAccountManager().getNewDeviceVerificationCode();
|
var verificationCode = dependencies.getAccountManager().getNewDeviceVerificationCode();
|
||||||
|
|
||||||
dependencies.getAccountManager()
|
try {
|
||||||
.addDevice(deviceIdentifier,
|
dependencies.getAccountManager()
|
||||||
deviceKey,
|
.addDevice(deviceIdentifier,
|
||||||
identityKeyPair,
|
deviceKey,
|
||||||
Optional.of(account.getProfileKey().serialize()),
|
identityKeyPair,
|
||||||
verificationCode);
|
Optional.of(account.getProfileKey().serialize()),
|
||||||
|
verificationCode);
|
||||||
|
} catch (InvalidKeyException e) {
|
||||||
|
throw new InvalidDeviceLinkException("Invalid device link", e);
|
||||||
|
}
|
||||||
account.setMultiDevice(true);
|
account.setMultiDevice(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package org.asamk.signal.manager.api;
|
||||||
|
|
||||||
|
public class InvalidDeviceLinkException extends Exception {
|
||||||
|
|
||||||
|
public InvalidDeviceLinkException(final String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidDeviceLinkException(final String message, final Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,12 +6,11 @@ import net.sourceforge.argparse4j.inf.Subparser;
|
||||||
import org.asamk.signal.OutputWriter;
|
import org.asamk.signal.OutputWriter;
|
||||||
import org.asamk.signal.commands.exceptions.CommandException;
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||||
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
|
||||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
|
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.libsignal.InvalidKeyException;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
@ -38,16 +37,21 @@ public class AddDeviceCommand implements JsonRpcLocalCommand {
|
||||||
public void handleCommand(
|
public void handleCommand(
|
||||||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
|
final URI linkUri;
|
||||||
try {
|
try {
|
||||||
m.addDeviceLink(new URI(ns.getString("uri")));
|
linkUri = new URI(ns.getString("uri"));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw new UserErrorException("Device link uri has invalid format: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
m.addDeviceLink(linkUri);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("Add device link failed", e);
|
logger.error("Add device link failed", e);
|
||||||
throw new IOErrorException("Add device link failed", e);
|
throw new IOErrorException("Add device link failed", e);
|
||||||
} catch (URISyntaxException e) {
|
} catch (InvalidDeviceLinkException e) {
|
||||||
throw new UserErrorException("Device link uri has invalid format: " + e.getMessage());
|
|
||||||
} catch (InvalidKeyException e) {
|
|
||||||
logger.error("Add device link failed", e);
|
logger.error("Add device link failed", e);
|
||||||
throw new UnexpectedErrorException("Add device link failed.", e);
|
throw new UserErrorException("Add device link failed.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.asamk.signal.manager.UntrustedIdentityException;
|
||||||
import org.asamk.signal.manager.api.Device;
|
import org.asamk.signal.manager.api.Device;
|
||||||
import org.asamk.signal.manager.api.Group;
|
import org.asamk.signal.manager.api.Group;
|
||||||
import org.asamk.signal.manager.api.Identity;
|
import org.asamk.signal.manager.api.Identity;
|
||||||
|
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
|
||||||
import org.asamk.signal.manager.api.Message;
|
import org.asamk.signal.manager.api.Message;
|
||||||
import org.asamk.signal.manager.api.Pair;
|
import org.asamk.signal.manager.api.Pair;
|
||||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||||
|
@ -31,7 +32,6 @@ import org.freedesktop.dbus.DBusPath;
|
||||||
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
||||||
import org.freedesktop.dbus.exceptions.DBusException;
|
import org.freedesktop.dbus.exceptions.DBusException;
|
||||||
import org.freedesktop.dbus.interfaces.DBusInterface;
|
import org.freedesktop.dbus.interfaces.DBusInterface;
|
||||||
import org.whispersystems.libsignal.InvalidKeyException;
|
|
||||||
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
|
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
|
||||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
|
||||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||||
|
@ -162,7 +162,7 @@ public class DbusManagerImpl implements Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addDeviceLink(final URI linkUri) throws IOException, InvalidKeyException {
|
public void addDeviceLink(final URI linkUri) throws IOException, InvalidDeviceLinkException {
|
||||||
signal.addDevice(linkUri.toString());
|
signal.addDevice(linkUri.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import org.asamk.signal.manager.NotMasterDeviceException;
|
||||||
import org.asamk.signal.manager.StickerPackInvalidException;
|
import org.asamk.signal.manager.StickerPackInvalidException;
|
||||||
import org.asamk.signal.manager.UntrustedIdentityException;
|
import org.asamk.signal.manager.UntrustedIdentityException;
|
||||||
import org.asamk.signal.manager.api.Identity;
|
import org.asamk.signal.manager.api.Identity;
|
||||||
|
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
|
||||||
import org.asamk.signal.manager.api.Message;
|
import org.asamk.signal.manager.api.Message;
|
||||||
import org.asamk.signal.manager.api.Pair;
|
import org.asamk.signal.manager.api.Pair;
|
||||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||||
|
@ -30,7 +31,6 @@ import org.freedesktop.dbus.connections.impl.DBusConnection;
|
||||||
import org.freedesktop.dbus.exceptions.DBusException;
|
import org.freedesktop.dbus.exceptions.DBusException;
|
||||||
import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
||||||
import org.freedesktop.dbus.types.Variant;
|
import org.freedesktop.dbus.types.Variant;
|
||||||
import org.whispersystems.libsignal.InvalidKeyException;
|
|
||||||
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
|
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
|
||||||
import org.whispersystems.signalservice.api.messages.SendMessageResult;
|
import org.whispersystems.signalservice.api.messages.SendMessageResult;
|
||||||
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
||||||
|
@ -107,7 +107,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
public void addDevice(String uri) {
|
public void addDevice(String uri) {
|
||||||
try {
|
try {
|
||||||
m.addDeviceLink(new URI(uri));
|
m.addDeviceLink(new URI(uri));
|
||||||
} catch (IOException | InvalidKeyException e) {
|
} catch (IOException | InvalidDeviceLinkException e) {
|
||||||
throw new Error.Failure(e.getClass().getSimpleName() + " Add device link failed. " + e.getMessage());
|
throw new Error.Failure(e.getClass().getSimpleName() + " Add device link failed. " + e.getMessage());
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
throw new Error.InvalidUri(e.getClass().getSimpleName()
|
throw new Error.InvalidUri(e.getClass().getSimpleName()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue