]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/util/NumberVerificationUtils.java
Update libsignal-service
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / util / NumberVerificationUtils.java
index 62474e665460dec7227c800c42bcf8f07e3e49f6..e0c4b61924e66e70bc7ed32645fd58ef5c032c24 100644 (file)
@@ -6,17 +6,18 @@ import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
 import org.asamk.signal.manager.api.Pair;
 import org.asamk.signal.manager.api.PinLockedException;
 import org.asamk.signal.manager.api.RateLimitException;
+import org.asamk.signal.manager.api.VerificationMethodNotAvailableException;
 import org.asamk.signal.manager.helper.PinHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.whispersystems.signalservice.api.SignalServiceAccountManager;
 import org.whispersystems.signalservice.api.kbs.MasterKey;
 import org.whispersystems.signalservice.api.push.exceptions.NoSuchSessionException;
 import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
 import org.whispersystems.signalservice.api.push.exceptions.PushChallengeRequiredException;
 import org.whispersystems.signalservice.api.push.exceptions.TokenNotAcceptedException;
-import org.whispersystems.signalservice.internal.ServiceResponse;
+import org.whispersystems.signalservice.api.registration.RegistrationApi;
 import org.whispersystems.signalservice.internal.push.LockedException;
+import org.whispersystems.signalservice.internal.push.PushServiceSocket.VerificationCodeTransport;
 import org.whispersystems.signalservice.internal.push.RegistrationSessionMetadataResponse;
 import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
 
@@ -26,22 +27,22 @@ import java.util.function.Consumer;
 
 public class NumberVerificationUtils {
 
-    private final static Logger logger = LoggerFactory.getLogger(NumberVerificationUtils.class);
+    private static final Logger logger = LoggerFactory.getLogger(NumberVerificationUtils.class);
 
     public static String handleVerificationSession(
-            SignalServiceAccountManager accountManager,
+            RegistrationApi registrationApi,
             String sessionId,
             Consumer<String> sessionIdSaver,
             boolean voiceVerification,
             String captcha
-    ) throws CaptchaRequiredException, IOException, RateLimitException {
+    ) throws CaptchaRequiredException, IOException, RateLimitException, VerificationMethodNotAvailableException {
         RegistrationSessionMetadataResponse sessionResponse;
         try {
-            sessionResponse = getValidSession(accountManager, sessionId);
+            sessionResponse = getValidSession(registrationApi, sessionId);
         } catch (PushChallengeRequiredException |
                  org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException e) {
             if (captcha != null) {
-                sessionResponse = submitCaptcha(accountManager, sessionId, captcha);
+                sessionResponse = submitCaptcha(registrationApi, sessionId, captcha);
             } else {
                 throw new CaptchaRequiredException("Captcha Required");
             }
@@ -61,7 +62,9 @@ public class NumberVerificationUtils {
         final var nextAttempt = voiceVerification
                 ? sessionResponse.getBody().getNextCall()
                 : sessionResponse.getBody().getNextSms();
-        if (nextAttempt != null && nextAttempt > 0) {
+        if (nextAttempt == null) {
+            throw new VerificationMethodNotAvailableException();
+        } else if (nextAttempt > 0) {
             final var timestamp = sessionResponse.getHeaders().getTimestamp() + nextAttempt * 1000;
             throw new RateLimitException(timestamp);
         }
@@ -74,7 +77,7 @@ public class NumberVerificationUtils {
 
         if (sessionResponse.getBody().getRequestedInformation().contains("captcha")) {
             if (captcha != null) {
-                sessionResponse = submitCaptcha(accountManager, sessionId, captcha);
+                sessionResponse = submitCaptcha(registrationApi, sessionId, captcha);
             }
             if (!sessionResponse.getBody().getAllowedToRequestCode()) {
                 throw new CaptchaRequiredException("Captcha Required");
@@ -85,15 +88,15 @@ public class NumberVerificationUtils {
     }
 
     public static void requestVerificationCode(
-            SignalServiceAccountManager accountManager, String sessionId, boolean voiceVerification
+            RegistrationApi registrationApi,
+            String sessionId,
+            boolean voiceVerification
     ) throws IOException, CaptchaRequiredException, NonNormalizedPhoneNumberException {
-        final ServiceResponse<RegistrationSessionMetadataResponse> response;
         final var locale = Utils.getDefaultLocale(Locale.US);
-        if (voiceVerification) {
-            response = accountManager.requestVoiceVerificationCode(sessionId, locale, false);
-        } else {
-            response = accountManager.requestSmsVerificationCode(sessionId, locale, false);
-        }
+        final var response = registrationApi.requestSmsVerificationCode(sessionId,
+                locale,
+                false,
+                voiceVerification ? VerificationCodeTransport.VOICE : VerificationCodeTransport.SMS);
         try {
             Utils.handleResponseException(response);
         } catch (org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException e) {
@@ -107,7 +110,11 @@ public class NumberVerificationUtils {
     }
 
     public static Pair<VerifyAccountResponse, MasterKey> verifyNumber(
-            String sessionId, String verificationCode, String pin, PinHelper pinHelper, Verifier verifier
+            String sessionId,
+            String verificationCode,
+            String pin,
+            PinHelper pinHelper,
+            Verifier verifier
     ) throws IOException, PinLockedException, IncorrectPinException {
         verificationCode = verificationCode.replace("-", "");
         try {
@@ -137,43 +144,47 @@ public class NumberVerificationUtils {
     }
 
     private static RegistrationSessionMetadataResponse validateSession(
-            final SignalServiceAccountManager accountManager, final String sessionId
+            final RegistrationApi registrationApi,
+            final String sessionId
     ) throws IOException {
         if (sessionId == null || sessionId.isEmpty()) {
             throw new NoSuchSessionException();
         }
-        return Utils.handleResponseException(accountManager.getRegistrationSession(sessionId));
+        return Utils.handleResponseException(registrationApi.getRegistrationSessionStatus(sessionId));
     }
 
     private static RegistrationSessionMetadataResponse requestValidSession(
-            final SignalServiceAccountManager accountManager
+            final RegistrationApi registrationApi
     ) throws IOException {
-        return Utils.handleResponseException(accountManager.createRegistrationSession(null, "", ""));
+        return Utils.handleResponseException(registrationApi.createRegistrationSession(null, "", ""));
     }
 
     private static RegistrationSessionMetadataResponse getValidSession(
-            final SignalServiceAccountManager accountManager, final String sessionId
+            final RegistrationApi registrationApi,
+            final String sessionId
     ) throws IOException {
         try {
-            return validateSession(accountManager, sessionId);
+            return validateSession(registrationApi, sessionId);
         } catch (NoSuchSessionException e) {
             logger.debug("No registration session, creating new one.");
-            return requestValidSession(accountManager);
+            return requestValidSession(registrationApi);
         }
     }
 
     private static RegistrationSessionMetadataResponse submitCaptcha(
-            SignalServiceAccountManager accountManager, String sessionId, String captcha
+            RegistrationApi registrationApi,
+            String sessionId,
+            String captcha
     ) throws IOException, CaptchaRequiredException {
         captcha = captcha == null ? null : captcha.replace("signalcaptcha://", "");
         try {
-            return Utils.handleResponseException(accountManager.submitCaptchaToken(sessionId, captcha));
+            return Utils.handleResponseException(registrationApi.submitCaptchaToken(sessionId, captcha));
         } catch (PushChallengeRequiredException |
                  org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException |
                  TokenNotAcceptedException _e) {
             throw new CaptchaRequiredException("Captcha not accepted");
         } catch (NonSuccessfulResponseCodeException e) {
-            if (e.getCode() == 400) {
+            if (e.code == 400) {
                 throw new CaptchaRequiredException("Captcha has invalid format");
             }
             throw e;
@@ -183,7 +194,9 @@ public class NumberVerificationUtils {
     public interface Verifier {
 
         VerifyAccountResponse verify(
-                String sessionId, String verificationCode, String registrationLock
+                String sessionId,
+                String verificationCode,
+                String registrationLock
         ) throws IOException;
     }
 }