]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/helper/PinHelper.java
Improve behavior when pin data doesn't exist on the server
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / PinHelper.java
index d8c801be90009050bf72a9653564721cc9f4ed82..40878647079cdae284d3cac036e104cad7b40ad3 100644 (file)
@@ -3,123 +3,130 @@ package org.asamk.signal.manager.helper;
 import org.asamk.signal.manager.api.IncorrectPinException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.whispersystems.signalservice.api.KeyBackupService;
 import org.whispersystems.signalservice.api.kbs.MasterKey;
 import org.whispersystems.signalservice.api.svr.SecureValueRecovery;
-import org.whispersystems.signalservice.api.svr.SecureValueRecoveryV1;
-import org.whispersystems.signalservice.api.svr.SecureValueRecoveryV2;
 import org.whispersystems.signalservice.internal.push.AuthCredentials;
 import org.whispersystems.signalservice.internal.push.LockedException;
 
 import java.io.IOException;
-import java.util.Collection;
+import java.util.List;
 
 public class PinHelper {
 
-    private final static Logger logger = LoggerFactory.getLogger(PinHelper.class);
+    private static final Logger logger = LoggerFactory.getLogger(PinHelper.class);
 
-    private final SecureValueRecoveryV1 secureValueRecoveryV1;
-    private final SecureValueRecoveryV2 secureValueRecoveryV2;
-    private final Collection<KeyBackupService> fallbackKeyBackupServices;
+    private final List<SecureValueRecovery> secureValueRecoveries;
 
-    public PinHelper(
-            final SecureValueRecoveryV1 secureValueRecoveryV1,
-            final SecureValueRecoveryV2 secureValueRecoveryV2,
-            final Collection<KeyBackupService> fallbackKeyBackupServices
-    ) {
-        this.fallbackKeyBackupServices = fallbackKeyBackupServices;
-        this.secureValueRecoveryV1 = secureValueRecoveryV1;
-        this.secureValueRecoveryV2 = secureValueRecoveryV2;
+    public PinHelper(final List<SecureValueRecovery> secureValueRecoveries) {
+        this.secureValueRecoveries = secureValueRecoveries;
     }
 
-    public void setRegistrationLockPin(
-            String pin, MasterKey masterKey
-    ) throws IOException {
-        secureValueRecoveryV1.setPin(pin, masterKey).execute();
-        final var backupResponse = secureValueRecoveryV2.setPin(pin, masterKey).execute();
-        if (backupResponse instanceof SecureValueRecovery.BackupResponse.Success) {
-        } else if (backupResponse instanceof SecureValueRecovery.BackupResponse.ServerRejected) {
-            logger.warn("Backup svr2 failed: ServerRejected");
-        } else if (backupResponse instanceof SecureValueRecovery.BackupResponse.EnclaveNotFound) {
-            logger.warn("Backup svr2 failed: EnclaveNotFound");
-        } else if (backupResponse instanceof SecureValueRecovery.BackupResponse.ExposeFailure) {
-            logger.warn("Backup svr2 failed: ExposeFailure");
-        } else if (backupResponse instanceof SecureValueRecovery.BackupResponse.ApplicationError error) {
-            throw new IOException(error.getException());
-        } else if (backupResponse instanceof SecureValueRecovery.BackupResponse.NetworkError error) {
-            throw error.getException();
-        } else {
-            throw new AssertionError("Unexpected response");
+    public void setRegistrationLockPin(String pin, MasterKey masterKey) throws IOException {
+        IOException exception = null;
+        for (final var secureValueRecovery : secureValueRecoveries) {
+            try {
+                final var backupResponse = secureValueRecovery.setPin(pin, masterKey).execute();
+                switch (backupResponse) {
+                    case SecureValueRecovery.BackupResponse.Success success -> {
+                    }
+                    case SecureValueRecovery.BackupResponse.ServerRejected serverRejected ->
+                            logger.warn("Backup svr failed: ServerRejected");
+                    case SecureValueRecovery.BackupResponse.EnclaveNotFound enclaveNotFound ->
+                            logger.warn("Backup svr failed: EnclaveNotFound");
+                    case SecureValueRecovery.BackupResponse.ExposeFailure exposeFailure ->
+                            logger.warn("Backup svr failed: ExposeFailure");
+                    case SecureValueRecovery.BackupResponse.ApplicationError error ->
+                            throw new IOException(error.getException());
+                    case SecureValueRecovery.BackupResponse.NetworkError error -> throw error.getException();
+                    case null, default -> throw new AssertionError("Unexpected response");
+                }
+            } catch (IOException e) {
+                exception = e;
+            }
+        }
+        if (exception != null) {
+            throw exception;
         }
     }
 
     public void migrateRegistrationLockPin(String pin, MasterKey masterKey) throws IOException {
         setRegistrationLockPin(pin, masterKey);
+    }
 
-        for (final var keyBackupService : fallbackKeyBackupServices) {
+    public void removeRegistrationLockPin() throws IOException {
+        IOException exception = null;
+        for (final var secureValueRecovery : secureValueRecoveries) {
             try {
-                final var pinChangeSession = keyBackupService.newPinChangeSession();
-                pinChangeSession.removePin();
-            } catch (Exception e) {
-                logger.warn("Failed to remove PIN from fallback KBS: {}", e.getMessage());
+                final var deleteResponse = secureValueRecovery.deleteData();
+                switch (deleteResponse) {
+                    case SecureValueRecovery.DeleteResponse.Success success -> {
+                    }
+                    case SecureValueRecovery.DeleteResponse.ServerRejected serverRejected ->
+                            logger.warn("Delete svr2 failed: ServerRejected");
+                    case SecureValueRecovery.DeleteResponse.EnclaveNotFound enclaveNotFound ->
+                            logger.warn("Delete svr2 failed: EnclaveNotFound");
+                    case SecureValueRecovery.DeleteResponse.ApplicationError error ->
+                            throw new IOException(error.getException());
+                    case SecureValueRecovery.DeleteResponse.NetworkError error -> throw error.getException();
+                    case null, default -> throw new AssertionError("Unexpected response");
+                }
+            } catch (IOException e) {
+                exception = e;
             }
         }
-    }
-
-    public void removeRegistrationLockPin() throws IOException {
-        secureValueRecoveryV1.deleteData();
-        final var deleteResponse = secureValueRecoveryV2.deleteData();
-        if (deleteResponse instanceof SecureValueRecovery.DeleteResponse.Success) {
-        } else if (deleteResponse instanceof SecureValueRecovery.DeleteResponse.ServerRejected) {
-            logger.warn("Delete svr2 failed: ServerRejected");
-        } else if (deleteResponse instanceof SecureValueRecovery.DeleteResponse.EnclaveNotFound) {
-            logger.warn("Delete svr2 failed: EnclaveNotFound");
-        } else if (deleteResponse instanceof SecureValueRecovery.DeleteResponse.ApplicationError error) {
-            throw new IOException(error.getException());
-        } else if (deleteResponse instanceof SecureValueRecovery.DeleteResponse.NetworkError error) {
-            throw error.getException();
-        } else {
-            throw new AssertionError("Unexpected response");
+        if (exception != null) {
+            throw exception;
         }
     }
 
     public SecureValueRecovery.RestoreResponse.Success getRegistrationLockData(
-            String pin, LockedException e
+            String pin,
+            LockedException lockedException
     ) throws IOException, IncorrectPinException {
-        var svr1Credentials = e.getSvr1Credentials();
-        if (svr1Credentials != null) {
-            final var registrationLockData = getRegistrationLockData(secureValueRecoveryV1, svr1Credentials, pin);
-            if (registrationLockData != null) {
-                return registrationLockData;
-            }
-        }
-
-        var svr2Credentials = e.getSvr2Credentials();
+        var svr2Credentials = lockedException.getSvr2Credentials();
         if (svr2Credentials != null) {
-            return getRegistrationLockData(secureValueRecoveryV2, svr2Credentials, pin);
+            IOException exception = null;
+            for (final var secureValueRecovery : secureValueRecoveries) {
+                try {
+                    final var lockData = getRegistrationLockData(secureValueRecovery, svr2Credentials, pin);
+                    if (lockData == null) {
+                        continue;
+                    }
+                    return lockData;
+                } catch (IOException e) {
+                    exception = e;
+                }
+            }
+            if (exception != null) {
+                throw exception;
+            }
         }
 
         return null;
     }
 
     public SecureValueRecovery.RestoreResponse.Success getRegistrationLockData(
-            SecureValueRecovery secureValueRecovery, AuthCredentials authCredentials, String pin
+            SecureValueRecovery secureValueRecovery,
+            AuthCredentials authCredentials,
+            String pin
     ) throws IOException, IncorrectPinException {
-        final var restoreResponse = secureValueRecovery.restoreDataPreRegistration(authCredentials, pin);
+        final var restoreResponse = secureValueRecovery.restoreDataPreRegistration(authCredentials, null, pin);
 
-        if (restoreResponse instanceof SecureValueRecovery.RestoreResponse.Success s) {
-            return s;
-        } else if (restoreResponse instanceof SecureValueRecovery.RestoreResponse.PinMismatch pinMismatch) {
-            throw new IncorrectPinException(pinMismatch.getTriesRemaining());
-        } else if (restoreResponse instanceof SecureValueRecovery.RestoreResponse.ApplicationError error) {
-            throw new IOException(error.getException());
-        } else if (restoreResponse instanceof SecureValueRecovery.RestoreResponse.NetworkError error) {
-            throw error.getException();
-        } else if (restoreResponse instanceof SecureValueRecovery.RestoreResponse.Missing) {
-            logger.debug("No SVR data stored for the given credentials.");
-            return null;
-        } else {
-            throw new AssertionError("Unexpected response: " + restoreResponse.getClass().getSimpleName());
+        switch (restoreResponse) {
+            case SecureValueRecovery.RestoreResponse.Success s -> {
+                return s;
+            }
+            case SecureValueRecovery.RestoreResponse.PinMismatch pinMismatch ->
+                    throw new IncorrectPinException(pinMismatch.getTriesRemaining());
+            case SecureValueRecovery.RestoreResponse.ApplicationError error ->
+                    throw new IOException(error.getException());
+            case SecureValueRecovery.RestoreResponse.NetworkError error -> throw error.getException();
+            case SecureValueRecovery.RestoreResponse.Missing missing -> {
+                logger.debug("No SVR data stored for the given credentials.");
+                return null;
+            }
+            case null, default ->
+                    throw new AssertionError("Unexpected response: " + restoreResponse.getClass().getSimpleName());
         }
     }
 }