Check if account file already exists before actually completing device link

This commit is contained in:
AsamK 2021-05-08 15:19:49 +02:00
parent e188121733
commit a4e34e600e
3 changed files with 15 additions and 28 deletions

View file

@ -14,7 +14,7 @@ repositories {
} }
dependencies { dependencies {
api("com.github.turasa:signal-service-java:2.15.3_unofficial_19") api("com.github.turasa:signal-service-java:2.15.3_unofficial_20")
implementation("com.google.protobuf:protobuf-javalite:3.10.0") implementation("com.google.protobuf:protobuf-javalite:3.10.0")
implementation("org.bouncycastle:bcprov-jdk15on:1.68") implementation("org.bouncycastle:bcprov-jdk15on:1.68")
implementation("org.slf4j:slf4j-api:1.7.30") implementation("org.slf4j:slf4j-api:1.7.30")

View file

@ -21,12 +21,9 @@ import org.asamk.signal.manager.config.ServiceEnvironment;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.storage.SignalAccount; import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.util.KeyUtils; import org.asamk.signal.manager.util.KeyUtils;
import org.signal.zkgroup.InvalidInputException;
import org.signal.zkgroup.profiles.ProfileKey;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.IdentityKeyPair; import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.InvalidKeyException;
import org.whispersystems.libsignal.util.KeyHelper; import org.whispersystems.libsignal.util.KeyHelper;
import org.whispersystems.signalservice.api.SignalServiceAccountManager; import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations; import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations;
@ -93,35 +90,30 @@ public class ProvisioningManager {
return new DeviceLinkInfo(deviceUuid, identityKey.getPublicKey().getPublicKey()).createDeviceLinkUri(); return new DeviceLinkInfo(deviceUuid, identityKey.getPublicKey().getPublicKey()).createDeviceLinkUri();
} }
public Manager finishDeviceLink(String deviceName) throws IOException, InvalidKeyException, TimeoutException, UserAlreadyExists { public Manager finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExists {
var ret = accountManager.finishNewDeviceRegistration(identityKey, false, true, registrationId, deviceName); var ret = accountManager.getNewDeviceRegistration(identityKey);
var number = ret.getNumber();
var username = ret.getNumber(); if (SignalAccount.userExists(pathConfig.getDataPath(), number)) {
// TODO do this check before actually registering throw new UserAlreadyExists(number, SignalAccount.getFileName(pathConfig.getDataPath(), number));
if (SignalAccount.userExists(pathConfig.getDataPath(), username)) {
throw new UserAlreadyExists(username, SignalAccount.getFileName(pathConfig.getDataPath(), username));
} }
var deviceId = accountManager.finishNewDeviceRegistration(ret.getProvisioningCode(),
false,
true,
registrationId,
deviceName);
// Create new account with the synced identity // Create new account with the synced identity
var profileKeyBytes = ret.getProfileKey(); var profileKey = ret.getProfileKey() == null ? KeyUtils.createProfileKey() : ret.getProfileKey();
ProfileKey profileKey;
if (profileKeyBytes == null) {
profileKey = KeyUtils.createProfileKey();
} else {
try {
profileKey = new ProfileKey(profileKeyBytes);
} catch (InvalidInputException e) {
throw new IOException("Received invalid profileKey", e);
}
}
SignalAccount account = null; SignalAccount account = null;
try { try {
account = SignalAccount.createLinkedAccount(pathConfig.getDataPath(), account = SignalAccount.createLinkedAccount(pathConfig.getDataPath(),
username, number,
ret.getUuid(), ret.getUuid(),
password, password,
ret.getDeviceId(), deviceId,
ret.getIdentity(), ret.getIdentity(),
registrationId, registrationId,
profileKey); profileKey);

View file

@ -6,13 +6,11 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
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.ProvisioningManager; import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.UserAlreadyExists; import org.asamk.signal.manager.UserAlreadyExists;
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.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -43,9 +41,6 @@ public class LinkCommand implements ProvisioningCommand {
throw new UserErrorException("Link request timed out, please try again."); throw new UserErrorException("Link request timed out, please try again.");
} catch (IOException e) { } catch (IOException e) {
throw new IOErrorException("Link request error: " + e.getMessage()); throw new IOErrorException("Link request error: " + e.getMessage());
} catch (InvalidKeyException e) {
logger.debug("Finish device link failed", e);
throw new UnexpectedErrorException("Invalid key: " + e.getMessage());
} catch (UserAlreadyExists e) { } catch (UserAlreadyExists e) {
throw new UserErrorException("The user " throw new UserErrorException("The user "
+ e.getUsername() + e.getUsername()