Settings: Add UIA support when adding a new 3pid.

This commit is contained in:
SBiOSoftWhare
2021-02-11 14:58:54 +01:00
parent 593f7946f5
commit 62543b7654
3 changed files with 124 additions and 119 deletions
+122 -117
View File
@@ -250,10 +250,24 @@ TableViewSectionsDelegate>
@property (nonatomic, strong) CrossSigningSetupCoordinatorBridgePresenter *crossSigningSetupCoordinatorBridgePresenter;
@property (nonatomic, strong) ReauthenticationCoordinatorBridgePresenter *reauthenticationCoordinatorBridgePresenter;
@property (nonatomic, strong) UserInteractiveAuthenticationService *userInteractiveAuthenticationService;
@end
@implementation SettingsViewController
- (UserInteractiveAuthenticationService*)userInteractiveAuthenticationService
{
if (!_userInteractiveAuthenticationService)
{
_userInteractiveAuthenticationService = [self createUserInteractiveAuthenticationService];
}
return _userInteractiveAuthenticationService;
}
+ (instancetype)instantiate
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
@@ -791,6 +805,11 @@ TableViewSectionsDelegate>
onReadyToDestroyHandler = nil;
}
- (MXSession*)currentSession
{
return [AppDelegate theDelegate].mxSessions.firstObject;
}
-(void)setNewEmailEditingEnabled:(BOOL)newEmailEditingEnabled
{
if (newEmailEditingEnabled != _newEmailEditingEnabled)
@@ -853,7 +872,7 @@ TableViewSectionsDelegate>
}
}
- (void)showValidationEmailDialogWithMessage:(NSString*)message for3PidAddSession:(MX3PidAddSession*)threePidAddSession threePidAddManager:(MX3PidAddManager*)threePidAddManager password:(NSString*)password
- (void)showValidationEmailDialogWithMessage:(NSString*)message for3PidAddSession:(MX3PidAddSession*)threePidAddSession threePidAddManager:(MX3PidAddManager*)threePidAddManager authenticationParameters:(NSDictionary*)authenticationParameters
{
MXWeakify(self);
[currentAlert dismissViewControllerAnimated:NO completion:nil];
@@ -870,18 +889,19 @@ TableViewSectionsDelegate>
[currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"continue"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
MXStrongifyAndReturnIfNil(self);
[self tryFinaliseAddEmailSession:threePidAddSession withPassword:password threePidAddManager:threePidAddManager];
[self tryFinaliseAddEmailSession:threePidAddSession withAuthenticationParameters:authenticationParameters
threePidAddManager:threePidAddManager];
}]];
[currentAlert mxk_setAccessibilityIdentifier:@"SettingsVCEmailValidationAlert"];
[self presentViewController:currentAlert animated:YES completion:nil];
}
- (void)tryFinaliseAddEmailSession:(MX3PidAddSession*)threePidAddSession withPassword:(NSString*)password threePidAddManager:(MX3PidAddManager*)threePidAddManager
- (void)tryFinaliseAddEmailSession:(MX3PidAddSession*)threePidAddSession withAuthenticationParameters:(NSDictionary*)authParams threePidAddManager:(MX3PidAddManager*)threePidAddManager
{
self->is3PIDBindingInProgress = YES;
[threePidAddManager tryFinaliseAddEmailSession:threePidAddSession withPassword:password success:^{
[threePidAddManager tryFinaliseAddEmailSession:threePidAddSession authParams:authParams success:^{
self->is3PIDBindingInProgress = NO;
@@ -911,7 +931,7 @@ TableViewSectionsDelegate>
MXError *mxError = [[MXError alloc] initWithNSError:error];
if (mxError && [mxError.errcode isEqualToString:kMXErrCodeStringForbidden])
{
NSLog(@"[SettingsViewController] tryFinaliseAddEmailSession: Wrong password");
NSLog(@"[SettingsViewController] tryFinaliseAddEmailSession: Wrong credentials");
// Ask password again
self->currentAlert = [UIAlertController alertControllerWithTitle:nil
@@ -920,11 +940,9 @@ TableViewSectionsDelegate>
[self->currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"retry", @"Vector", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
self->currentAlert = nil;
[self requestAccountPasswordWithTitle:NSLocalizedStringFromTable(@"settings_add_3pid_password_title_email", @"Vector", nil)
message:NSLocalizedStringFromTable(@"settings_add_3pid_password_message", @"Vector", nil)
onComplete:^(NSString *password) {
[self tryFinaliseAddEmailSession:threePidAddSession withPassword:password threePidAddManager:threePidAddManager];
[self showAuthenticationIfNeededForAdding:kMX3PIDMediumEmail withSession:self.currentSession completion:^(NSDictionary *authParams) {
[self tryFinaliseAddEmailSession:threePidAddSession withAuthenticationParameters:authParams threePidAddManager:threePidAddManager];
}];
}]];
@@ -950,10 +968,7 @@ TableViewSectionsDelegate>
MXError *mxError = [[MXError alloc] initWithNSError:error];
if (mxError && [mxError.errcode isEqualToString:kMXErrCodeStringThreePIDAuthFailed])
{
[self showValidationEmailDialogWithMessage:[NSBundle mxk_localizedStringForKey:@"account_email_validation_error"]
for3PidAddSession:threePidAddSession
threePidAddManager:threePidAddManager
password:password];
[self showValidationEmailDialogWithMessage:[NSBundle mxk_localizedStringForKey:@"account_email_validation_error"] for3PidAddSession:threePidAddSession threePidAddManager:threePidAddManager authenticationParameters:authParams];
}
else
{
@@ -967,7 +982,7 @@ TableViewSectionsDelegate>
}];
}
- (void)showValidationMsisdnDialogWithMessage:(NSString*)message for3PidAddSession:(MX3PidAddSession*)threePidAddSession threePidAddManager:(MX3PidAddManager*)threePidAddManager password:(NSString*)password
- (void)showValidationMsisdnDialogWithMessage:(NSString*)message for3PidAddSession:(MX3PidAddSession*)threePidAddSession threePidAddManager:(MX3PidAddManager*)threePidAddManager authenticationParameters:(NSDictionary*)authenticationParameters
{
MXWeakify(self);
@@ -1001,12 +1016,12 @@ TableViewSectionsDelegate>
if (smsCode.length)
{
[self finaliseAddPhoneNumberSession:threePidAddSession withToken:smsCode andPassword:password message:message threePidAddManager:threePidAddManager];
[self finaliseAddPhoneNumberSession:threePidAddSession withToken:smsCode andAuthenticationParameters:authenticationParameters message:message threePidAddManager:threePidAddManager];
}
else
{
// Ask again the sms token
[self showValidationMsisdnDialogWithMessage:message for3PidAddSession:threePidAddSession threePidAddManager:threePidAddManager password:password];
[self showValidationMsisdnDialogWithMessage:message for3PidAddSession:threePidAddSession threePidAddManager:threePidAddManager authenticationParameters:authenticationParameters];
}
}]];
@@ -1014,12 +1029,12 @@ TableViewSectionsDelegate>
[self presentViewController:currentAlert animated:YES completion:nil];
}
- (void)finaliseAddPhoneNumberSession:(MX3PidAddSession*)threePidAddSession withToken:(NSString*)token andPassword:(NSString*)password message:(NSString*)message threePidAddManager:(MX3PidAddManager*)threePidAddManager
- (void)finaliseAddPhoneNumberSession:(MX3PidAddSession*)threePidAddSession withToken:(NSString*)token andAuthenticationParameters:(NSDictionary*)authParams message:(NSString*)message threePidAddManager:(MX3PidAddManager*)threePidAddManager
{
self->is3PIDBindingInProgress = YES;
[threePidAddManager finaliseAddPhoneNumberSession:threePidAddSession withToken:token password:password success:^{
[threePidAddManager finaliseAddPhoneNumberSession:threePidAddSession withToken:token authParams:authParams success:^{
self->is3PIDBindingInProgress = NO;
// Check whether destroy has been called during the binding
@@ -1047,7 +1062,7 @@ TableViewSectionsDelegate>
MXError *mxError = [[MXError alloc] initWithNSError:error];
if (mxError && [mxError.errcode isEqualToString:kMXErrCodeStringForbidden])
{
NSLog(@"[SettingsViewController] finaliseAddPhoneNumberSession: Wrong password");
NSLog(@"[SettingsViewController] finaliseAddPhoneNumberSession: Wrong authentication credentials");
// Ask password again
self->currentAlert = [UIAlertController alertControllerWithTitle:nil
@@ -1056,11 +1071,9 @@ TableViewSectionsDelegate>
[self->currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"retry", @"Vector", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
self->currentAlert = nil;
[self requestAccountPasswordWithTitle:NSLocalizedStringFromTable(@"settings_add_3pid_password_title_msidsn", @"Vector", nil)
message:NSLocalizedStringFromTable(@"settings_add_3pid_password_message", @"Vector", nil)
onComplete:^(NSString *password) {
[self finaliseAddPhoneNumberSession:threePidAddSession withToken:token andPassword:password message:message threePidAddManager:threePidAddManager];
[self showAuthenticationIfNeededForAdding:kMX3PIDMediumMSISDN withSession:self.currentSession completion:^(NSDictionary *authParams) {
[self finaliseAddPhoneNumberSession:threePidAddSession withToken:token andAuthenticationParameters:authParams message:message threePidAddManager:threePidAddManager];
}];
}]];
@@ -1110,7 +1123,7 @@ TableViewSectionsDelegate>
self->currentAlert = nil;
// Ask again the sms token
[self showValidationMsisdnDialogWithMessage:message for3PidAddSession:threePidAddSession threePidAddManager:threePidAddManager password:password];
[self showValidationMsisdnDialogWithMessage:message for3PidAddSession:threePidAddSession threePidAddManager:threePidAddManager authenticationParameters:authParams];
}]];
[self->currentAlert mxk_setAccessibilityIdentifier: @"SettingsVCErrorAlert"];
@@ -1219,97 +1232,90 @@ TableViewSectionsDelegate>
self.settingsDiscoveryTableViewSection = discoverySection;
}
- (UserInteractiveAuthenticationService*)createUserInteractiveAuthenticationService
{
MXSession *session = self.currentSession;
UserInteractiveAuthenticationService *userInteractiveAuthenticationService;
if (session)
{
userInteractiveAuthenticationService = [[UserInteractiveAuthenticationService alloc] initWithSession:session];
}
return userInteractiveAuthenticationService;
}
#pragma mark - 3Pid Add
-(void)checkAuthenticationFlowForAdding:(MX3PIDMedium)medium withSession:(MXSession*)session onComplete:(void (^)(NSString *password))onComplete
- (void)showAuthenticationIfNeededForAdding:(MX3PIDMedium)medium withSession:(MXSession*)session completion:(void (^)(NSDictionary* authParams))completion
{
[self startActivityIndicator];
[session.threePidAddManager authenticationFlowForAdd3PidWithSuccess:^(NSArray<MXLoginFlow *> * _Nullable flows) {
MXWeakify(self);
void (^animationCompletion)(void) = ^void () {
MXStrongifyAndReturnIfNil(self);
[self stopActivityIndicator];
if (flows)
[self.reauthenticationCoordinatorBridgePresenter dismissWithAnimated:YES completion:^{}];
self.reauthenticationCoordinatorBridgePresenter = nil;
};
NSString *title;
if ([medium isEqualToString:kMX3PIDMediumMSISDN])
{
title = NSLocalizedStringFromTable(@"settings_add_3pid_password_title_msidsn", @"Vector", nil);
}
else
{
title = NSLocalizedStringFromTable(@"settings_add_3pid_password_title_email", @"Vector", nil);
}
NSString *message = NSLocalizedStringFromTable(@"settings_add_3pid_password_message", @"Vector", nil);
[session.matrixRestClient add3PIDOnlyWithSessionId:@"" clientSecret:[MXTools generateSecret] authParams:nil success:^{
} failure:^(NSError *error) {
if (error)
{
// We support only "m.login.password"
BOOL hasPasswordFlow = NO;
for (MXLoginFlow *flow in flows)
{
if ([flow.stages containsObject:kMXLoginFlowTypePassword])
[self.userInteractiveAuthenticationService authenticationSessionFromRequestError:error success:^(MXAuthenticationSession * _Nullable authenticationSession) {
if (authenticationSession)
{
hasPasswordFlow = YES;
break;
ReauthenticationCoordinatorParameters *coordinatorParameters = [[ReauthenticationCoordinatorParameters alloc] initWithSession:self.mainSession presenter:self title:title message:message authenticationSession:authenticationSession];
ReauthenticationCoordinatorBridgePresenter *reauthenticationPresenter = [ReauthenticationCoordinatorBridgePresenter new];
[reauthenticationPresenter presentWith:coordinatorParameters animated:YES success:^(NSDictionary<NSString *,id> *_Nullable authParams) {
completion(authParams);
} cancel:^{
animationCompletion();
} failure:^(NSError * _Nonnull error) {
animationCompletion();
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
self.reauthenticationCoordinatorBridgePresenter = reauthenticationPresenter;
}
}
if (hasPasswordFlow)
{
// Ask password to the user while we are here
NSString *title = NSLocalizedStringFromTable(@"settings_add_3pid_password_title_email", @"Vector", nil);
if ([medium isEqualToString:kMX3PIDMediumMSISDN])
else
{
title = NSLocalizedStringFromTable(@"settings_add_3pid_password_title_msidsn", @"Vector", nil);
animationCompletion();
completion(nil);
}
[self requestAccountPasswordWithTitle:title
message:NSLocalizedStringFromTable(@"settings_add_3pid_password_message", @"Vector", nil)
onComplete:onComplete];
}
else
{
// The user needs to use Riot-web
NSString *appName = [[NSBundle mainBundle] infoDictionary][@"CFBundleDisplayName"];
NSString *message = [NSString stringWithFormat:NSLocalizedStringFromTable(@"error_not_supported_on_mobile", @"Vector", nil), appName];
[[AppDelegate theDelegate] showAlertWithTitle:nil message:message];
}
} failure:^(NSError * _Nonnull error) {
animationCompletion();
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
}
else
{
// No auth
onComplete(nil);
animationCompletion();
[[AppDelegate theDelegate] showErrorAsAlert:error];
}
} failure:^(NSError * _Nonnull error) {
[self stopActivityIndicator];
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
}
- (void)requestAccountPasswordWithTitle:(NSString*)title message:(NSString*)message onComplete:(void (^)(NSString *password))onComplete
{
[currentAlert dismissViewControllerAnimated:NO completion:nil];
// Prompt the user before deleting the device.
currentAlert = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[currentAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = YES;
textField.placeholder = nil;
textField.keyboardType = UIKeyboardTypeDefault;
}];
MXWeakify(self);
[currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
MXStrongifyAndReturnIfNil(self);
self->currentAlert = nil;
}]];
[currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"continue"]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
MXStrongifyAndReturnIfNil(self);
UITextField *textField = [self->currentAlert textFields].firstObject;
self->currentAlert = nil;
onComplete(textField.text);
}]];
[self presentViewController:currentAlert animated:YES completion:nil];
}];
}
#pragma mark - Segues
@@ -1437,7 +1443,7 @@ TableViewSectionsDelegate>
return cell;
}
MXSession* session = [AppDelegate theDelegate].mxSessions[0];
MXSession* session = self.currentSession;
MXKAccount* account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (section == SECTION_TAG_SIGN_OUT)
@@ -2455,7 +2461,7 @@ TableViewSectionsDelegate>
}
else if (section == SECTION_TAG_IGNORED_USERS)
{
MXSession* session = [AppDelegate theDelegate].mxSessions[0];
MXSession* session = self.currentSession;
NSString *ignoredUserId = session.ignoredUsers[row];
@@ -2476,7 +2482,7 @@ TableViewSectionsDelegate>
typeof(self) self = weakSelf;
self->currentAlert = nil;
MXSession* session = [AppDelegate theDelegate].mxSessions[0];
MXSession* session = self.currentSession;
// Remove the member from the ignored user list
[self startActivityIndicator];
@@ -3253,10 +3259,9 @@ TableViewSectionsDelegate>
// Dismiss the keyboard
[newEmailTextField resignFirstResponder];
MXSession* session = [AppDelegate theDelegate].mxSessions[0];
[self checkAuthenticationFlowForAdding:kMX3PIDMediumEmail withSession:session onComplete:^(NSString *password) {
MXSession* session = self.currentSession;
[self showAuthenticationIfNeededForAdding:kMX3PIDMediumEmail withSession:session completion:^(NSDictionary *authParams) {
[self startActivityIndicator];
__block MX3PidAddSession *thirdPidAddSession;
@@ -3265,7 +3270,7 @@ TableViewSectionsDelegate>
[self showValidationEmailDialogWithMessage:[NSBundle mxk_localizedStringForKey:@"account_email_validation_message"]
for3PidAddSession:thirdPidAddSession
threePidAddManager:session.threePidAddManager
password:password];
authenticationParameters:authParams];
} failure:^(NSError * _Nonnull error) {
@@ -3361,7 +3366,7 @@ TableViewSectionsDelegate>
// Dismiss the keyboard
[newPhoneNumberCell.mxkTextField resignFirstResponder];
MXSession* session = [AppDelegate theDelegate].mxSessions[0];
MXSession* session = self.currentSession;
NSString *e164 = [[NBPhoneNumberUtil sharedInstance] format:newPhoneNumber numberFormat:NBEPhoneNumberFormatE164 error:nil];
NSString *msisdn;
@@ -3373,14 +3378,14 @@ TableViewSectionsDelegate>
{
msisdn = [NSString stringWithFormat:@"+%@", [e164 substringFromIndex:2]];
}
[self checkAuthenticationFlowForAdding:kMX3PIDMediumMSISDN withSession:session onComplete:^(NSString *password) {
[self showAuthenticationIfNeededForAdding:kMX3PIDMediumMSISDN withSession:session completion:^(NSDictionary *authParams) {
[self startActivityIndicator];
__block MX3PidAddSession *new3Pid;
new3Pid = [session.threePidAddManager startAddPhoneNumberSessionWithPhoneNumber:msisdn countryCode:nil success:^{
[self showValidationMsisdnDialogWithMessage:[NSBundle mxk_localizedStringForKey:@"account_msisdn_validation_message"] for3PidAddSession:new3Pid threePidAddManager:session.threePidAddManager password:password];
[self showValidationMsisdnDialogWithMessage:[NSBundle mxk_localizedStringForKey:@"account_msisdn_validation_message"] for3PidAddSession:new3Pid threePidAddManager:session.threePidAddManager authenticationParameters:authParams];
} failure:^(NSError *error) {
@@ -3440,7 +3445,7 @@ TableViewSectionsDelegate>
{
if ([AppDelegate theDelegate].mxSessions.count > 0)
{
MXSession* session = [AppDelegate theDelegate].mxSessions[0];
MXSession* session = self.currentSession;
MXMyUser* myUser = session.myUser;
BOOL saveButtonEnabled = (nil != newAvatarImage);