]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java
Update libsignal-service-java
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / RegistrationManager.java
index e95cb458b74578249610cab2d610e3728a190cc5..653f9cb403271cb73ace2c46246c2104893a11fd 100644 (file)
@@ -22,6 +22,8 @@ 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.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,8 +33,6 @@ 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.push.LockedException;
 import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
@@ -45,6 +45,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;
@@ -64,7 +66,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 +78,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(),
@@ -107,14 +107,14 @@ public class RegistrationManager implements Closeable {
             return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent);
         }
 
-        var account = SignalAccount.load(pathConfig.getDataPath(), username);
+        var account = SignalAccount.load(pathConfig.getDataPath(), username, true);
 
         return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent);
     }
 
     public void register(boolean voiceVerification, String captcha) throws IOException {
         if (voiceVerification) {
-            accountManager.requestVoiceVerificationCode(Locale.getDefault(),
+            accountManager.requestVoiceVerificationCode(getDefaultLocale(),
                     Optional.fromNullable(captcha),
                     Optional.absent());
         } else {
@@ -122,6 +122,18 @@ public class RegistrationManager implements Closeable {
         }
     }
 
+    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(
             String verificationCode, String pin
     ) throws IOException, LockedException, KeyBackupSystemNoDataException, KeyBackupServicePinException {
@@ -129,9 +141,10 @@ public class RegistrationManager implements Closeable {
         VerifyAccountResponse response;
         MasterKey masterKey;
         try {
-            response = verifyAccountWithCode(verificationCode, pin, null);
+            response = verifyAccountWithCode(verificationCode, null, null);
 
             masterKey = null;
+            pin = null;
         } catch (LockedException e) {
             if (pin == null) {
                 throw e;
@@ -139,16 +152,17 @@ public class RegistrationManager implements Closeable {
 
             var registrationLockData = pinHelper.getRegistrationLockData(pin, e);
             if (registrationLockData == null) {
-                throw e;
-            }
-
-            var registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock();
-            try {
-                response = verifyAccountWithCode(verificationCode, null, registrationLock);
-            } catch (LockedException _e) {
-                throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
+                response = verifyAccountWithCode(verificationCode, pin, null);
+                masterKey = null;
+            } else {
+                var registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock();
+                try {
+                    response = verifyAccountWithCode(verificationCode, null, registrationLock);
+                } catch (LockedException _e) {
+                    throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
+                }
+                masterKey = registrationLockData.getMasterKey();
             }
-            masterKey = registrationLockData.getMasterKey();
         }
 
         // TODO response.isStorageCapable()
@@ -161,6 +175,8 @@ public class RegistrationManager implements Closeable {
             account = null;
 
             m.refreshPreKeys();
+            // Set an initial empty profile so user can be added to groups
+            m.setProfile(null, null, null, null, null);
 
             final var result = m;
             m = null;