From 7543a828582d965af4c241770267b82cf03654eb Mon Sep 17 00:00:00 2001
From: Matthew Chen <charlesmchen@gmail.com>
Date: Mon, 5 Mar 2018 13:05:21 -0300
Subject: [PATCH 1/2] Handle rate limits in registration flow.

---
 .../CodeVerificationViewController.m          | 35 +++++++------------
 .../translations/en.lproj/Localizable.strings |  5 ++-
 .../src/Account/TSAccountManager.m            | 21 ++++++++---
 3 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/Signal/src/ViewControllers/CodeVerificationViewController.m b/Signal/src/ViewControllers/CodeVerificationViewController.m
index 0fd43b98f..adaacd8e1 100644
--- a/Signal/src/ViewControllers/CodeVerificationViewController.m
+++ b/Signal/src/ViewControllers/CodeVerificationViewController.m
@@ -302,29 +302,18 @@ NS_ASSUME_NONNULL_BEGIN
 
 - (void)presentAlertWithVerificationError:(NSError *)error
 {
-    UIAlertController *alertController;
-    // In the case of the "rate limiting" error, we want to show the
-    // "recovery suggestion", not the error's "description."
-    if ([error.domain isEqualToString:TSNetworkManagerDomain] &&
-        error.code == 413) {
-        alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"REGISTRATION_VERIFICATION_FAILED_TITLE",
-                                                                      @"Alert view title")
-                                                              message:error.localizedRecoverySuggestion
-                                                       preferredStyle:UIAlertControllerStyleAlert];
-    } else {
-        alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"REGISTRATION_VERIFICATION_FAILED_TITLE",
-                                                                                        @"Alert view title")
-                                                              message:error.localizedDescription
-                                                       preferredStyle:UIAlertControllerStyleAlert];
-    }
-    UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
-                                                            style:UIAlertActionStyleDefault
-                                                          handler:^(UIAlertAction *action) {
-                                                              [_challengeTextField becomeFirstResponder];
-                                                          }];
-    [alertController addAction:dismissAction];
-
-    [self presentViewController:alertController animated:YES completion:nil];
+    UIAlertController *alert;
+    alert = [UIAlertController
+        alertControllerWithTitle:NSLocalizedString(@"REGISTRATION_VERIFICATION_FAILED_TITLE", @"Alert view title")
+                         message:error.localizedDescription
+                  preferredStyle:UIAlertControllerStyleAlert];
+    [alert addAction:[UIAlertAction actionWithTitle:CommonStrings.dismissButton
+                                              style:UIAlertActionStyleDefault
+                                            handler:^(UIAlertAction *action) {
+                                                [_challengeTextField becomeFirstResponder];
+                                            }]];
+
+    [self presentViewController:alert animated:YES completion:nil];
 }
 
 - (NSString *)validationCodeFromTextField {
diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings
index 08e45c912..2faedd24a 100644
--- a/Signal/translations/en.lproj/Localizable.strings
+++ b/Signal/translations/en.lproj/Localizable.strings
@@ -1444,9 +1444,12 @@
 /* Alert view title */
 "REGISTRATION_VERIFICATION_FAILED_TITLE" = "Verification Failed";
 
-/* Alert body, during registration */
+/* Error message indicating that regitration failed due to a missing or incorrect verification code. */
 "REGISTRATION_VERIFICATION_FAILED_WRONG_CODE_DESCRIPTION" = "The numbers you submitted don't match what we sent. Want to double check?";
 
+/* Error message indicating that regitration failed due to a missing or incorrect 2FA PIN. */
+"REGISTRATION_VERIFICATION_FAILED_WRONG_PIN" = "Incorrect Registration Lock PIN.";
+
 /* No comment provided by engineer. */
 "REGISTRATION_VERIFY_DEVICE" = "Activate This Device";
 
diff --git a/SignalServiceKit/src/Account/TSAccountManager.m b/SignalServiceKit/src/Account/TSAccountManager.m
index da57b972c..d81599323 100644
--- a/SignalServiceKit/src/Account/TSAccountManager.m
+++ b/SignalServiceKit/src/Account/TSAccountManager.m
@@ -386,24 +386,37 @@ NSString *const TSAccountManager_ServerSignalingKey = @"TSStorageServerSignaling
             if (!IsNSErrorNetworkFailure(error)) {
                 OWSProdError([OWSAnalyticsEvents accountsErrorVerifyAccountRequestFailed]);
             }
+            OWSAssert([error.domain isEqualToString:TSNetworkManagerDomain]);
+
             DDLogWarn(@"%@ Error verifying code: %@", self.logTag, error.debugDescription);
+
             switch (error.code) {
                 case 403: {
                     NSError *userError = OWSErrorWithCodeDescription(OWSErrorCodeUserError,
                         NSLocalizedString(@"REGISTRATION_VERIFICATION_FAILED_WRONG_CODE_DESCRIPTION",
-                            "Alert body, during registration"));
+                            "Error message indicating that regitration failed due to a missing or incorrect "
+                            "verification code."));
+                    failureBlock(userError);
+                    break;
+                }
+                case 413: {
+                    // In the case of the "rate limiting" error, we want to show the
+                    // "recovery suggestion", not the error's "description."
+                    NSError *userError
+                        = OWSErrorWithCodeDescription(OWSErrorCodeUserError, error.localizedRecoverySuggestion);
                     failureBlock(userError);
                     break;
                 }
                 case 423: {
                     DDLogError(@"%@ 2FA PIN required: %ld", self.logTag, error.code);
-                    NSError *error = OWSErrorWithCodeDescription(
-                        OWSErrorCodeRegistrationMissing2FAPIN, @"Registration missing 2FA PIN.");
+                    NSError *error = OWSErrorWithCodeDescription(OWSErrorCodeRegistrationMissing2FAPIN,
+                        NSLocalizedString(@"REGISTRATION_VERIFICATION_FAILED_WRONG_PIN",
+                            "Error message indicating that regitration failed due to a missing or incorrect 2FA PIN."));
                     failureBlock(error);
                     break;
                 }
                 default: {
-                    DDLogError(@"%@ verifying code failed with unhandled error: %@", self.logTag, error);
+                    DDLogError(@"%@ verifying code failed with unknown error: %@", self.logTag, error);
                     failureBlock(error);
                     break;
                 }

From 9499e684e751e370d3044bc739d60e78233935f7 Mon Sep 17 00:00:00 2001
From: Matthew Chen <charlesmchen@gmail.com>
Date: Mon, 5 Mar 2018 13:08:42 -0300
Subject: [PATCH 2/2] Handle rate limits in registration flow.

---
 Signal/translations/en.lproj/Localizable.strings | 4 ++--
 SignalServiceKit/src/Account/TSAccountManager.m  | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings
index 2faedd24a..cea88ccb5 100644
--- a/Signal/translations/en.lproj/Localizable.strings
+++ b/Signal/translations/en.lproj/Localizable.strings
@@ -1444,10 +1444,10 @@
 /* Alert view title */
 "REGISTRATION_VERIFICATION_FAILED_TITLE" = "Verification Failed";
 
-/* Error message indicating that regitration failed due to a missing or incorrect verification code. */
+/* Error message indicating that registration failed due to a missing or incorrect verification code. */
 "REGISTRATION_VERIFICATION_FAILED_WRONG_CODE_DESCRIPTION" = "The numbers you submitted don't match what we sent. Want to double check?";
 
-/* Error message indicating that regitration failed due to a missing or incorrect 2FA PIN. */
+/* Error message indicating that registration failed due to a missing or incorrect 2FA PIN. */
 "REGISTRATION_VERIFICATION_FAILED_WRONG_PIN" = "Incorrect Registration Lock PIN.";
 
 /* No comment provided by engineer. */
diff --git a/SignalServiceKit/src/Account/TSAccountManager.m b/SignalServiceKit/src/Account/TSAccountManager.m
index d81599323..17d2018a0 100644
--- a/SignalServiceKit/src/Account/TSAccountManager.m
+++ b/SignalServiceKit/src/Account/TSAccountManager.m
@@ -394,7 +394,7 @@ NSString *const TSAccountManager_ServerSignalingKey = @"TSStorageServerSignaling
                 case 403: {
                     NSError *userError = OWSErrorWithCodeDescription(OWSErrorCodeUserError,
                         NSLocalizedString(@"REGISTRATION_VERIFICATION_FAILED_WRONG_CODE_DESCRIPTION",
-                            "Error message indicating that regitration failed due to a missing or incorrect "
+                            "Error message indicating that registration failed due to a missing or incorrect "
                             "verification code."));
                     failureBlock(userError);
                     break;
@@ -411,7 +411,7 @@ NSString *const TSAccountManager_ServerSignalingKey = @"TSStorageServerSignaling
                     DDLogError(@"%@ 2FA PIN required: %ld", self.logTag, error.code);
                     NSError *error = OWSErrorWithCodeDescription(OWSErrorCodeRegistrationMissing2FAPIN,
                         NSLocalizedString(@"REGISTRATION_VERIFICATION_FAILED_WRONG_PIN",
-                            "Error message indicating that regitration failed due to a missing or incorrect 2FA PIN."));
+                            "Error message indicating that registration failed due to a missing or incorrect 2FA PIN."));
                     failureBlock(error);
                     break;
                 }