From 8914b0b16fcac7fe5d579af56ce8aed1fb860fa6 Mon Sep 17 00:00:00 2001 From: Tom Carpel Date: Wed, 20 Sep 2017 14:12:32 -0700 Subject: [PATCH] Auth Chanllenge alert could attempt to present off of the UI Thread --- .../OAuth2/BOXAuthorizationViewController.m | 108 +++++++++--------- 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/BoxContentSDK/BoxContentSDK/OAuth2/BOXAuthorizationViewController.m b/BoxContentSDK/BoxContentSDK/OAuth2/BOXAuthorizationViewController.m index 015b014e8..fde060c83 100644 --- a/BoxContentSDK/BoxContentSDK/OAuth2/BOXAuthorizationViewController.m +++ b/BoxContentSDK/BoxContentSDK/OAuth2/BOXAuthorizationViewController.m @@ -646,59 +646,61 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didRece BOXLog(@"Presenting modal username and password window"); self.authenticationChallenge = challenge; - UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Please Log In", @"Alert view title: title for SSO authentication challenge") - message:nil - preferredStyle:UIAlertControllerStyleAlert]; - - UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Label: Cancel action. Usually used on buttons.") - style:UIAlertActionStyleCancel - handler:^(UIAlertAction *action) { - BOXLog(@"Cancel"); - if (completionHandler) { - completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, NULL); - } else { - [[self.authenticationChallenge sender] cancelAuthenticationChallenge:self.authenticationChallenge]; - } - [alertController dismissViewControllerAnimated:YES completion:nil]; - }]; - UIAlertAction *submitAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Submit", @"Alert view button: submit button for authentication challenge") - style:UIAlertActionStyleDefault - handler:^(UIAlertAction *action) { - [alertController dismissViewControllerAnimated:YES completion:nil]; - - BOXLog(@"Submitting credential for authentication challenge %@", self.authenticationChallenge); - [self setWebViewCanBeUsedDirectly:YES forHost:task.currentRequest.URL.host]; - UITextField *login = alertController.textFields.firstObject; - UITextField *password = alertController.textFields.lastObject; - - self.authenticationChallengeCredential = [NSURLCredential credentialWithUser:[login text] - password:[password text] - persistence:NSURLCredentialPersistenceNone]; - if (completionHandler) { - completionHandler(NSURLSessionAuthChallengeUseCredential, self.authenticationChallengeCredential); - } else { - [[self.authenticationChallenge sender] useCredential:self.authenticationChallengeCredential - forAuthenticationChallenge:self.authenticationChallenge]; - } - - // Clear out the authentication challenge in memory - self.authenticationChallenge = nil; - }]; - - [alertController addAction:cancelAction]; - [alertController addAction:submitAction]; - - [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) - { - textField.placeholder = NSLocalizedString(@"Username", @"Alert view text placeholder: Placeholder for where to enter user name for SSO authentication challenge"); - }]; - - [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { - textField.placeholder = NSLocalizedString(@"Password", @"Alert view text placeholder: Placeholder for where to enter password for SSO authentication challenge"); - textField.secureTextEntry = YES; - }]; - - [self presentViewController:alertController animated:YES completion:nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Please Log In", @"Alert view title: title for SSO authentication challenge") + message:nil + preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Label: Cancel action. Usually used on buttons.") + style:UIAlertActionStyleCancel + handler:^(UIAlertAction *action) { + BOXLog(@"Cancel"); + if (completionHandler) { + completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, NULL); + } else { + [[self.authenticationChallenge sender] cancelAuthenticationChallenge:self.authenticationChallenge]; + } + [alertController dismissViewControllerAnimated:YES completion:nil]; + }]; + UIAlertAction *submitAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Submit", @"Alert view button: submit button for authentication challenge") + style:UIAlertActionStyleDefault + handler:^(UIAlertAction *action) { + [alertController dismissViewControllerAnimated:YES completion:nil]; + + BOXLog(@"Submitting credential for authentication challenge %@", self.authenticationChallenge); + [self setWebViewCanBeUsedDirectly:YES forHost:task.currentRequest.URL.host]; + UITextField *login = alertController.textFields.firstObject; + UITextField *password = alertController.textFields.lastObject; + + self.authenticationChallengeCredential = [NSURLCredential credentialWithUser:[login text] + password:[password text] + persistence:NSURLCredentialPersistenceNone]; + if (completionHandler) { + completionHandler(NSURLSessionAuthChallengeUseCredential, self.authenticationChallengeCredential); + } else { + [[self.authenticationChallenge sender] useCredential:self.authenticationChallengeCredential + forAuthenticationChallenge:self.authenticationChallenge]; + } + + // Clear out the authentication challenge in memory + self.authenticationChallenge = nil; + }]; + + [alertController addAction:cancelAction]; + [alertController addAction:submitAction]; + + [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) + { + textField.placeholder = NSLocalizedString(@"Username", @"Alert view text placeholder: Placeholder for where to enter user name for SSO authentication challenge"); + }]; + + [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { + textField.placeholder = NSLocalizedString(@"Password", @"Alert view text placeholder: Placeholder for where to enter password for SSO authentication challenge"); + textField.secureTextEntry = YES; + }]; + + [self presentViewController:alertController animated:YES completion:nil]; + }); } } }