X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/d27a12a6cfcf9afca362a8d359ee808b1507e0b3..f7f882e834f33702e4cc36d9a20e0a89ed76dec8:/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java diff --git a/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java b/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java index 7519950e..544032da 100644 --- a/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java +++ b/lib/src/main/java/org/asamk/signal/manager/ProvisioningManagerImpl.java @@ -21,7 +21,6 @@ import org.asamk.signal.manager.config.ServiceConfig; import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.storage.SignalAccount; import org.asamk.signal.manager.storage.accounts.AccountsStore; -import org.asamk.signal.manager.storage.identities.TrustNewIdentity; import org.asamk.signal.manager.util.KeyUtils; import org.signal.libsignal.protocol.IdentityKeyPair; import org.signal.libsignal.protocol.util.KeyHelper; @@ -33,6 +32,7 @@ import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations; import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException; import org.whispersystems.signalservice.api.util.DeviceNameUtil; +import org.whispersystems.signalservice.internal.push.ConfirmCodeMessage; import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider; import java.io.IOException; @@ -41,6 +41,8 @@ import java.nio.channels.OverlappingFileLockException; import java.util.concurrent.TimeoutException; import java.util.function.Consumer; +import static org.asamk.signal.manager.config.ServiceConfig.getCapabilities; + class ProvisioningManagerImpl implements ProvisioningManager { private final static Logger logger = LoggerFactory.getLogger(ProvisioningManagerImpl.class); @@ -54,6 +56,7 @@ class ProvisioningManagerImpl implements ProvisioningManager { private final SignalServiceAccountManager accountManager; private final IdentityKeyPair tempIdentityKey; private final int registrationId; + private final int pniRegistrationId; private final String password; ProvisioningManagerImpl( @@ -71,6 +74,7 @@ class ProvisioningManagerImpl implements ProvisioningManager { tempIdentityKey = KeyUtils.generateIdentityKeyPair(); registrationId = KeyHelper.generateRegistrationId(false); + pniRegistrationId = KeyHelper.generateRegistrationId(false); password = KeyUtils.createPassword(); GroupsV2Operations groupsV2Operations; try { @@ -123,10 +127,12 @@ class ProvisioningManagerImpl implements ProvisioningManager { logger.debug("Finishing new device registration"); var deviceId = accountManager.finishNewDeviceRegistration(ret.getProvisioningCode(), - false, - true, - registrationId, - encryptedDeviceName); + new ConfirmCodeMessage(false, + true, + registrationId, + pniRegistrationId, + encryptedDeviceName, + getCapabilities(false))); // Create new account with the synced identity var profileKey = ret.getProfileKey() == null ? KeyUtils.createProfileKey() : ret.getProfileKey(); @@ -136,6 +142,7 @@ class ProvisioningManagerImpl implements ProvisioningManager { account = SignalAccount.createOrUpdateLinkedAccount(pathConfig.dataPath(), accountPath, number, + serviceEnvironmentConfig.getType(), aci, pni, password, @@ -144,15 +151,16 @@ class ProvisioningManagerImpl implements ProvisioningManager { ret.getAciIdentity(), ret.getPniIdentity(), registrationId, + pniRegistrationId, profileKey, - TrustNewIdentity.ON_FIRST_USE); + Settings.DEFAULT); + account.getConfigurationStore().setReadReceipts(ret.isReadReceipts()); ManagerImpl m = null; try { - final var accountPathFinal = accountPath; m = new ManagerImpl(account, pathConfig, - (newNumber, newAci) -> accountsStore.updateAccount(accountPathFinal, newNumber, newAci), + new AccountFileUpdaterImpl(accountsStore, accountPath), serviceEnvironmentConfig, userAgent); account = null; @@ -161,7 +169,7 @@ class ProvisioningManagerImpl implements ProvisioningManager { try { m.refreshPreKeys(); } catch (Exception e) { - logger.error("Failed to refresh pre keys."); + logger.error("Failed to refresh pre keys.", e); } logger.debug("Requesting sync data"); @@ -169,7 +177,8 @@ class ProvisioningManagerImpl implements ProvisioningManager { m.requestAllSyncData(); } catch (Exception e) { logger.error( - "Failed to request sync messages from linked device, data can be requested again with `sendSyncRequest`."); + "Failed to request sync messages from linked device, data can be requested again with `sendSyncRequest`.", + e); } if (newManagerListener != null) { @@ -192,10 +201,7 @@ class ProvisioningManagerImpl implements ProvisioningManager { private boolean canRelinkExistingAccount(final String accountPath) throws IOException { final SignalAccount signalAccount; try { - signalAccount = SignalAccount.load(pathConfig.dataPath(), - accountPath, - false, - TrustNewIdentity.ON_FIRST_USE); + signalAccount = SignalAccount.load(pathConfig.dataPath(), accountPath, false, Settings.DEFAULT); } catch (IOException e) { logger.debug("Account in use or failed to load.", e); return false; @@ -205,14 +211,21 @@ class ProvisioningManagerImpl implements ProvisioningManager { } try (signalAccount) { - if (signalAccount.isMasterDevice()) { - logger.debug("Account is a master device."); + if (signalAccount.isPrimaryDevice()) { + logger.debug("Account is a primary device."); + return false; + } + if (signalAccount.isRegistered() + && signalAccount.getServiceEnvironment() != null + && signalAccount.getServiceEnvironment() != serviceEnvironmentConfig.getType()) { + logger.debug("Account is registered in another environment: {}.", + signalAccount.getServiceEnvironment()); return false; } final var m = new ManagerImpl(signalAccount, pathConfig, - (newNumber, newAci) -> accountsStore.updateAccount(accountPath, newNumber, newAci), + new AccountFileUpdaterImpl(accountsStore, accountPath), serviceEnvironmentConfig, userAgent); try (m) {