X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/c79860b493f8689e7aa8c066b4ef80e1a0f75add..d72b838560b1a4186ac121c7d605773b49fcdf46:/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java diff --git a/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java b/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java index e95cb458..ff94c19b 100644 --- a/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java +++ b/lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java @@ -21,7 +21,10 @@ import org.asamk.signal.manager.config.ServiceEnvironment; import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.helper.PinHelper; import org.asamk.signal.manager.storage.SignalAccount; +import org.asamk.signal.manager.storage.identities.TrustNewIdentity; import org.asamk.signal.manager.util.KeyUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.whispersystems.libsignal.util.KeyHelper; import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.signalservice.api.KeyBackupServicePinException; @@ -31,10 +34,10 @@ import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations; import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations; import org.whispersystems.signalservice.api.kbs.MasterKey; import org.whispersystems.signalservice.api.push.SignalServiceAddress; -import org.whispersystems.signalservice.api.util.SleepTimer; -import org.whispersystems.signalservice.api.util.UptimeSleepTimer; import org.whispersystems.signalservice.api.util.UuidUtil; +import org.whispersystems.signalservice.internal.ServiceResponse; import org.whispersystems.signalservice.internal.push.LockedException; +import org.whispersystems.signalservice.internal.push.RequestVerificationCodeResponse; import org.whispersystems.signalservice.internal.push.VerifyAccountResponse; import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider; @@ -45,6 +48,8 @@ import java.util.Locale; public class RegistrationManager implements Closeable { + private final static Logger logger = LoggerFactory.getLogger(RegistrationManager.class); + private SignalAccount account; private final PathConfig pathConfig; private final ServiceEnvironmentConfig serviceEnvironmentConfig; @@ -53,7 +58,7 @@ public class RegistrationManager implements Closeable { private final SignalServiceAccountManager accountManager; private final PinHelper pinHelper; - public RegistrationManager( + private RegistrationManager( SignalAccount account, PathConfig pathConfig, ServiceEnvironmentConfig serviceEnvironmentConfig, @@ -64,7 +69,6 @@ public class RegistrationManager implements Closeable { this.serviceEnvironmentConfig = serviceEnvironmentConfig; this.userAgent = userAgent; - final SleepTimer timer = new UptimeSleepTimer(); GroupsV2Operations groupsV2Operations; try { groupsV2Operations = new GroupsV2Operations(ClientZkOperations.create(serviceEnvironmentConfig.getSignalServiceConfiguration())); @@ -77,8 +81,7 @@ public class RegistrationManager implements Closeable { null, account.getUsername(), account.getPassword(), SignalServiceAddress.DEFAULT_DEVICE_ID), userAgent, groupsV2Operations, - ServiceConfig.AUTOMATIC_NETWORK_RETRY, - timer); + ServiceConfig.AUTOMATIC_NETWORK_RETRY); final var keyBackupService = accountManager.getKeyBackupService(ServiceConfig.getIasKeyStore(), serviceEnvironmentConfig.getKeyBackupConfig().getEnclaveName(), serviceEnvironmentConfig.getKeyBackupConfig().getServiceId(), @@ -88,38 +91,57 @@ public class RegistrationManager implements Closeable { } public static RegistrationManager init( - String username, File settingsPath, ServiceEnvironment serviceEnvironment, String userAgent + String number, File settingsPath, ServiceEnvironment serviceEnvironment, String userAgent ) throws IOException { var pathConfig = PathConfig.createDefault(settingsPath); final var serviceConfiguration = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent); - if (!SignalAccount.userExists(pathConfig.getDataPath(), username)) { + if (!SignalAccount.userExists(pathConfig.getDataPath(), number)) { var identityKey = KeyUtils.generateIdentityKeyPair(); var registrationId = KeyHelper.generateRegistrationId(false); var profileKey = KeyUtils.createProfileKey(); var account = SignalAccount.create(pathConfig.getDataPath(), - username, + number, identityKey, registrationId, - profileKey); + profileKey, + TrustNewIdentity.ON_FIRST_USE); return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent); } - var account = SignalAccount.load(pathConfig.getDataPath(), username); + var account = SignalAccount.load(pathConfig.getDataPath(), number, true, TrustNewIdentity.ON_FIRST_USE); return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent); } public void register(boolean voiceVerification, String captcha) throws IOException { + final ServiceResponse response; if (voiceVerification) { - accountManager.requestVoiceVerificationCode(Locale.getDefault(), + response = accountManager.requestVoiceVerificationCode(getDefaultLocale(), Optional.fromNullable(captcha), + Optional.absent(), Optional.absent()); } else { - accountManager.requestSmsVerificationCode(false, Optional.fromNullable(captcha), Optional.absent()); + response = accountManager.requestSmsVerificationCode(false, + Optional.fromNullable(captcha), + Optional.absent(), + Optional.absent()); } + handleResponseException(response); + } + + private Locale getDefaultLocale() { + final var locale = Locale.getDefault(); + try { + Locale.LanguageRange.parse(locale.getLanguage() + "-" + locale.getCountry()); + } catch (IllegalArgumentException e) { + logger.debug("Invalid locale, ignoring: {}", locale); + return null; + } + + return locale; } public Manager verifyAccount( @@ -129,9 +151,10 @@ public class RegistrationManager implements Closeable { VerifyAccountResponse response; MasterKey masterKey; try { - response = verifyAccountWithCode(verificationCode, pin, null); + response = verifyAccountWithCode(verificationCode, null); masterKey = null; + pin = null; } catch (LockedException e) { if (pin == null) { throw e; @@ -144,23 +167,31 @@ public class RegistrationManager implements Closeable { var registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock(); try { - response = verifyAccountWithCode(verificationCode, null, registrationLock); + response = verifyAccountWithCode(verificationCode, registrationLock); } catch (LockedException _e) { throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!"); } masterKey = registrationLockData.getMasterKey(); } - // TODO response.isStorageCapable() //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID))); account.finishRegistration(UuidUtil.parseOrNull(response.getUuid()), masterKey, pin); - Manager m = null; + ManagerImpl m = null; try { - m = new Manager(account, pathConfig, serviceEnvironmentConfig, userAgent); + m = new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent); account = null; m.refreshPreKeys(); + // Set an initial empty profile so user can be added to groups + try { + m.setProfile(null, null, null, null, null); + } catch (NoClassDefFoundError e) { + logger.warn("Failed to set default profile: {}", e.getMessage()); + } + if (response.isStorageCapable()) { + m.retrieveRemoteStorage(); + } final var result = m; m = null; @@ -174,18 +205,29 @@ public class RegistrationManager implements Closeable { } private VerifyAccountResponse verifyAccountWithCode( - final String verificationCode, final String legacyPin, final String registrationLock + final String verificationCode, final String registrationLock ) throws IOException { - return accountManager.verifyAccountWithCode(verificationCode, - null, - account.getLocalRegistrationId(), - true, - legacyPin, - registrationLock, - account.getSelfUnidentifiedAccessKey(), - account.isUnrestrictedUnidentifiedAccess(), - ServiceConfig.capabilities, - account.isDiscoverableByPhoneNumber()); + final ServiceResponse response; + if (registrationLock == null) { + response = accountManager.verifyAccount(verificationCode, + account.getLocalRegistrationId(), + true, + account.getSelfUnidentifiedAccessKey(), + account.isUnrestrictedUnidentifiedAccess(), + ServiceConfig.capabilities, + account.isDiscoverableByPhoneNumber()); + } else { + response = accountManager.verifyAccountWithRegistrationLockPin(verificationCode, + account.getLocalRegistrationId(), + true, + registrationLock, + account.getSelfUnidentifiedAccessKey(), + account.isUnrestrictedUnidentifiedAccess(), + ServiceConfig.capabilities, + account.isDiscoverableByPhoneNumber()); + } + handleResponseException(response); + return response.getResult().get(); } @Override @@ -195,4 +237,15 @@ public class RegistrationManager implements Closeable { account = null; } } + + private void handleResponseException(final ServiceResponse response) throws IOException { + final var throwableOptional = response.getExecutionError().or(response.getApplicationError()); + if (throwableOptional.isPresent()) { + if (throwableOptional.get() instanceof IOException) { + throw (IOException) throwableOptional.get(); + } else { + throw new IOException(throwableOptional.get()); + } + } + } }