2014-10-08 12:14:12 +00:00
|
|
|
/*
|
2015-04-14 08:55:18 +00:00
|
|
|
Copyright 2015 OpenMarket Ltd
|
2017-03-01 10:43:08 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2014-10-08 12:14:12 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-05 10:43:06 +00:00
|
|
|
#import "AuthenticationViewController.h"
|
2014-10-08 12:14:12 +00:00
|
|
|
|
2016-06-03 15:29:34 +00:00
|
|
|
#import "AppDelegate.h"
|
|
|
|
|
2016-03-29 13:35:37 +00:00
|
|
|
#import "AuthInputsView.h"
|
2016-05-03 13:54:05 +00:00
|
|
|
#import "ForgotPasswordInputsView.h"
|
2015-09-28 08:23:33 +00:00
|
|
|
|
2015-04-14 08:55:18 +00:00
|
|
|
#import "RageShakeManager.h"
|
|
|
|
|
2017-03-08 15:14:41 +00:00
|
|
|
#import "RiotDesignValues.h"
|
2016-01-27 11:40:26 +00:00
|
|
|
|
2016-05-09 11:56:37 +00:00
|
|
|
@interface AuthenticationViewController ()
|
|
|
|
{
|
|
|
|
/**
|
2016-09-14 15:28:59 +00:00
|
|
|
Store the potential login error received by using a default homeserver different from matrix.org
|
2016-05-09 11:56:37 +00:00
|
|
|
while we retry a login process against the matrix.org HS.
|
|
|
|
*/
|
|
|
|
NSError *loginError;
|
2017-03-10 10:09:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
The default country code used to initialize the mobile phone number input.
|
|
|
|
*/
|
|
|
|
NSString *defaultCountryCode;
|
2016-05-09 11:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-05 10:43:06 +00:00
|
|
|
@implementation AuthenticationViewController
|
2014-10-08 12:14:12 +00:00
|
|
|
|
2016-01-27 11:40:26 +00:00
|
|
|
+ (UINib *)nib
|
|
|
|
{
|
|
|
|
return [UINib nibWithNibName:NSStringFromClass(self)
|
|
|
|
bundle:[NSBundle bundleForClass:self]];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)authenticationViewController
|
|
|
|
{
|
|
|
|
return [[[self class] alloc] initWithNibName:NSStringFromClass(self)
|
|
|
|
bundle:[NSBundle bundleForClass:self]];
|
|
|
|
}
|
|
|
|
|
2016-05-03 13:54:05 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-01-03 13:40:23 +00:00
|
|
|
- (void)finalizeInit
|
2015-09-28 08:23:33 +00:00
|
|
|
{
|
2017-01-03 13:40:23 +00:00
|
|
|
[super finalizeInit];
|
2015-02-10 15:12:58 +00:00
|
|
|
|
2016-02-10 21:12:17 +00:00
|
|
|
// Setup `MXKViewControllerHandling` properties
|
2017-03-08 15:14:41 +00:00
|
|
|
self.defaultBarTintColor = kRiotNavBarTintColor;
|
2016-02-10 21:12:17 +00:00
|
|
|
self.enableBarTintColorStatusChange = NO;
|
2015-04-14 08:55:18 +00:00
|
|
|
self.rageShakeManager = [RageShakeManager sharedManager];
|
2017-03-10 10:09:46 +00:00
|
|
|
|
2017-03-10 13:48:34 +00:00
|
|
|
// Set a default country code
|
|
|
|
// Note: this value is used only when no MCC and no local country code is available.
|
2017-03-10 10:09:46 +00:00
|
|
|
defaultCountryCode = @"GB";
|
2017-01-03 13:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
2015-09-28 08:23:33 +00:00
|
|
|
|
2016-09-15 09:53:29 +00:00
|
|
|
self.mainNavigationItem.title = nil;
|
2016-01-27 17:17:43 +00:00
|
|
|
self.rightBarButtonItem.title = NSLocalizedStringFromTable(@"auth_register", @"Vector", nil);
|
|
|
|
|
2015-04-30 14:19:12 +00:00
|
|
|
self.defaultHomeServerUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"homeserverurl"];
|
2017-03-08 15:14:41 +00:00
|
|
|
self.homeServerTextField.textColor = kRiotTextColorBlack;
|
|
|
|
self.homeServerLabel.textColor = kRiotTextColorGray;
|
2016-01-27 17:17:43 +00:00
|
|
|
|
2015-04-30 14:19:12 +00:00
|
|
|
self.defaultIdentityServerUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"identityserverurl"];
|
2017-03-08 15:14:41 +00:00
|
|
|
self.identityServerTextField.textColor = kRiotTextColorBlack;
|
|
|
|
self.identityServerLabel.textColor = kRiotTextColorGray;
|
2015-04-14 08:55:18 +00:00
|
|
|
|
2015-09-28 08:23:33 +00:00
|
|
|
self.welcomeImageView.image = [UIImage imageNamed:@"logo"];
|
|
|
|
|
2016-01-27 11:40:26 +00:00
|
|
|
[self.submitButton.layer setCornerRadius:5];
|
|
|
|
self.submitButton.clipsToBounds = YES;
|
2017-03-08 15:14:41 +00:00
|
|
|
self.submitButton.backgroundColor = kRiotColorGreen;
|
2016-01-27 11:40:26 +00:00
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_login", @"Vector", nil) forState:UIControlStateNormal];
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_login", @"Vector", nil) forState:UIControlStateHighlighted];
|
2016-04-01 14:31:13 +00:00
|
|
|
self.submitButton.enabled = YES;
|
2015-09-28 08:23:33 +00:00
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
[self.skipButton.layer setCornerRadius:5];
|
|
|
|
self.skipButton.clipsToBounds = YES;
|
2017-03-08 15:14:41 +00:00
|
|
|
self.skipButton.backgroundColor = kRiotColorGreen;
|
2017-03-01 10:43:08 +00:00
|
|
|
[self.skipButton setTitle:NSLocalizedStringFromTable(@"auth_skip", @"Vector", nil) forState:UIControlStateNormal];
|
|
|
|
[self.skipButton setTitle:NSLocalizedStringFromTable(@"auth_skip", @"Vector", nil) forState:UIControlStateHighlighted];
|
|
|
|
self.skipButton.enabled = YES;
|
|
|
|
|
2016-05-03 13:54:05 +00:00
|
|
|
NSMutableAttributedString *forgotPasswordTitle = [[NSMutableAttributedString alloc] initWithString:NSLocalizedStringFromTable(@"auth_forgot_password", @"Vector", nil)];
|
|
|
|
[forgotPasswordTitle addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, forgotPasswordTitle.length)];
|
2017-03-08 15:14:41 +00:00
|
|
|
[forgotPasswordTitle addAttribute:NSForegroundColorAttributeName value:kRiotColorGreen range:NSMakeRange(0, forgotPasswordTitle.length)];
|
2016-05-03 13:54:05 +00:00
|
|
|
[self.forgotPasswordButton setAttributedTitle:forgotPasswordTitle forState:UIControlStateNormal];
|
|
|
|
[self.forgotPasswordButton setAttributedTitle:forgotPasswordTitle forState:UIControlStateHighlighted];
|
2016-06-02 12:33:21 +00:00
|
|
|
|
|
|
|
[self updateForgotPwdButtonVisibility];
|
2016-01-27 17:17:43 +00:00
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
[self.customServersTickButton setImage:[UIImage imageNamed:@"selection_untick"] forState:UIControlStateNormal];
|
|
|
|
[self.customServersTickButton setImage:[UIImage imageNamed:@"selection_untick"] forState:UIControlStateHighlighted];
|
2016-01-27 17:17:43 +00:00
|
|
|
|
2017-03-08 15:14:41 +00:00
|
|
|
NSAttributedString *serverOptionsTitle = [[NSAttributedString alloc] initWithString:NSLocalizedStringFromTable(@"auth_use_server_options", @"Vector", nil) attributes:@{NSForegroundColorAttributeName : kRiotTextColorGray, NSFontAttributeName: [UIFont systemFontOfSize:14]}];
|
2017-03-01 10:43:08 +00:00
|
|
|
[self.customServersTickButton setAttributedTitle:serverOptionsTitle forState:UIControlStateNormal];
|
|
|
|
[self.customServersTickButton setAttributedTitle:serverOptionsTitle forState:UIControlStateHighlighted];
|
2016-01-27 17:17:43 +00:00
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
[self hideCustomServers:YES];
|
2016-01-27 17:17:43 +00:00
|
|
|
|
2015-04-30 14:19:12 +00:00
|
|
|
// The view controller dismiss itself on successful login.
|
|
|
|
self.delegate = self;
|
2015-09-28 08:23:33 +00:00
|
|
|
|
|
|
|
// Custom used authInputsView
|
2016-03-29 13:35:37 +00:00
|
|
|
[self registerAuthInputsViewClass:AuthInputsView.class forAuthType:MXKAuthenticationTypeLogin];
|
|
|
|
[self registerAuthInputsViewClass:AuthInputsView.class forAuthType:MXKAuthenticationTypeRegister];
|
2016-05-03 13:54:05 +00:00
|
|
|
[self registerAuthInputsViewClass:ForgotPasswordInputsView.class forAuthType:MXKAuthenticationTypeForgotPassword];
|
2016-01-28 12:54:50 +00:00
|
|
|
|
|
|
|
// Initialize the auth inputs display
|
2016-03-29 13:35:37 +00:00
|
|
|
AuthInputsView *authInputsView = [AuthInputsView authInputsView];
|
|
|
|
MXAuthenticationSession *authSession = [MXAuthenticationSession modelFromJSON:@{@"flows":@[@{@"stages":@[kMXLoginFlowTypePassword]}]}];
|
|
|
|
[authInputsView setAuthSession:authSession withAuthType:MXKAuthenticationTypeLogin];
|
|
|
|
self.authInputsView = authInputsView;
|
2015-09-28 08:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 15:29:34 +00:00
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
|
|
|
// Screen tracking (via Google Analytics)
|
|
|
|
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
|
|
|
|
if (tracker)
|
|
|
|
{
|
2016-06-14 09:10:37 +00:00
|
|
|
[tracker set:kGAIScreenName value:@"Authentication"];
|
2016-06-03 15:29:34 +00:00
|
|
|
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 08:23:33 +00:00
|
|
|
- (void)setAuthType:(MXKAuthenticationType)authType
|
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
if (self.authType == MXKAuthenticationTypeRegister)
|
|
|
|
{
|
|
|
|
// Restore the default registration screen
|
|
|
|
[self updateRegistrationScreenWithThirdPartyIdentifiersHidden:YES];
|
|
|
|
}
|
|
|
|
|
2015-09-28 08:23:33 +00:00
|
|
|
super.authType = authType;
|
|
|
|
|
2016-05-11 09:56:33 +00:00
|
|
|
// Check a potential stored error.
|
|
|
|
if (loginError)
|
|
|
|
{
|
|
|
|
// Restore the default HS
|
|
|
|
NSLog(@"[AuthenticationVC] Switch back to default homeserver");
|
2016-09-14 15:28:59 +00:00
|
|
|
[self setHomeServerTextFieldText:nil];
|
2016-05-11 09:56:33 +00:00
|
|
|
loginError = nil;
|
|
|
|
}
|
2016-05-09 22:00:07 +00:00
|
|
|
|
2015-09-28 08:23:33 +00:00
|
|
|
if (authType == MXKAuthenticationTypeLogin)
|
|
|
|
{
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_login", @"Vector", nil) forState:UIControlStateNormal];
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_login", @"Vector", nil) forState:UIControlStateHighlighted];
|
|
|
|
}
|
2016-05-03 13:54:05 +00:00
|
|
|
else if (authType == MXKAuthenticationTypeRegister)
|
2015-09-28 08:23:33 +00:00
|
|
|
{
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_register", @"Vector", nil) forState:UIControlStateNormal];
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_register", @"Vector", nil) forState:UIControlStateHighlighted];
|
|
|
|
}
|
2016-05-03 13:54:05 +00:00
|
|
|
else if (authType == MXKAuthenticationTypeForgotPassword)
|
|
|
|
{
|
|
|
|
if (isPasswordReseted)
|
|
|
|
{
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_return_to_login", @"Vector", nil) forState:UIControlStateNormal];
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_return_to_login", @"Vector", nil) forState:UIControlStateHighlighted];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_send_reset_email", @"Vector", nil) forState:UIControlStateNormal];
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_send_reset_email", @"Vector", nil) forState:UIControlStateHighlighted];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 12:33:21 +00:00
|
|
|
[self updateForgotPwdButtonVisibility];
|
2015-02-10 15:12:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 22:00:07 +00:00
|
|
|
- (void)setAuthInputsView:(MXKAuthInputsView *)authInputsView
|
|
|
|
{
|
2017-03-10 10:09:46 +00:00
|
|
|
// Keep the current country code if any.
|
|
|
|
if ([self.authInputsView isKindOfClass:AuthInputsView.class])
|
|
|
|
{
|
|
|
|
// We will reuse the current country code
|
|
|
|
defaultCountryCode = ((AuthInputsView*)self.authInputsView).isoCountryCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize the new auth inputs view
|
|
|
|
if ([authInputsView isKindOfClass:AuthInputsView.class])
|
|
|
|
{
|
|
|
|
AuthInputsView *authInputsview = (AuthInputsView*)authInputsView;
|
|
|
|
|
|
|
|
// Retrieve the MCC from the SIM card information (Note: the phone book country code is not defined yet)
|
|
|
|
NSString *countryCode = [MXKAppSettings standardAppSettings].phonebookCountryCode;
|
|
|
|
if (!countryCode)
|
|
|
|
{
|
|
|
|
// If none, consider the preferred locale
|
|
|
|
NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]];
|
|
|
|
if ([local respondsToSelector:@selector(countryCode)])
|
|
|
|
{
|
|
|
|
countryCode = local.countryCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!countryCode)
|
|
|
|
{
|
|
|
|
countryCode = defaultCountryCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
authInputsview.isoCountryCode = countryCode;
|
|
|
|
authInputsview.delegate = self;
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:00:07 +00:00
|
|
|
[super setAuthInputsView:authInputsView];
|
|
|
|
|
|
|
|
// Restore here the actual content view height.
|
|
|
|
// Indeed this height has been modified according to the authInputsView height in the default implementation of MXKAuthenticationViewController.
|
2016-05-27 12:24:35 +00:00
|
|
|
[self refreshContentViewHeightConstraint];
|
2016-05-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 14:31:13 +00:00
|
|
|
- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled
|
|
|
|
{
|
|
|
|
super.userInteractionEnabled = userInteractionEnabled;
|
|
|
|
|
|
|
|
// Show/Hide server options
|
2016-05-27 12:24:35 +00:00
|
|
|
if (_optionsContainer.hidden == userInteractionEnabled)
|
|
|
|
{
|
|
|
|
_optionsContainer.hidden = !userInteractionEnabled;
|
|
|
|
|
|
|
|
[self refreshContentViewHeightConstraint];
|
|
|
|
}
|
2016-04-07 07:28:27 +00:00
|
|
|
|
|
|
|
// Update the label of the right bar button according to its actual action.
|
|
|
|
if (!userInteractionEnabled)
|
|
|
|
{
|
|
|
|
// The right bar button is used to cancel the running request.
|
|
|
|
self.rightBarButtonItem.title = NSLocalizedStringFromTable(@"cancel", @"Vector", nil);
|
2017-03-01 10:43:08 +00:00
|
|
|
|
|
|
|
// Remove the potential back button.
|
|
|
|
self.mainNavigationItem.leftBarButtonItem = nil;
|
2016-04-07 07:28:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The right bar button is used to switch the authentication type.
|
|
|
|
if (self.authType == MXKAuthenticationTypeLogin)
|
|
|
|
{
|
|
|
|
self.rightBarButtonItem.title = NSLocalizedStringFromTable(@"auth_register", @"Vector", nil);
|
|
|
|
}
|
2016-05-03 13:54:05 +00:00
|
|
|
else if (self.authType == MXKAuthenticationTypeRegister)
|
2016-04-07 07:28:27 +00:00
|
|
|
{
|
|
|
|
self.rightBarButtonItem.title = NSLocalizedStringFromTable(@"auth_login", @"Vector", nil);
|
2017-03-01 10:43:08 +00:00
|
|
|
|
|
|
|
// Restore the back button
|
|
|
|
if ([self.authInputsView isKindOfClass:AuthInputsView.class])
|
|
|
|
{
|
|
|
|
AuthInputsView *authInputsview = (AuthInputsView*)self.authInputsView;
|
|
|
|
[self updateRegistrationScreenWithThirdPartyIdentifiersHidden:authInputsview.thirdPartyIdentifiersHidden];
|
|
|
|
}
|
2016-04-07 07:28:27 +00:00
|
|
|
}
|
2016-05-03 13:54:05 +00:00
|
|
|
else if (self.authType == MXKAuthenticationTypeForgotPassword)
|
|
|
|
{
|
|
|
|
// The right bar button is used to return to login.
|
|
|
|
self.rightBarButtonItem.title = NSLocalizedStringFromTable(@"cancel", @"Vector", nil);
|
|
|
|
}
|
2016-04-07 07:28:27 +00:00
|
|
|
}
|
2016-04-01 14:31:13 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 17:17:43 +00:00
|
|
|
- (IBAction)onButtonPressed:(id)sender
|
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
if (sender == self.customServersTickButton)
|
2016-01-27 17:17:43 +00:00
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
[self hideCustomServers:!self.customServersContainer.hidden];
|
2016-01-27 17:17:43 +00:00
|
|
|
}
|
|
|
|
else if (sender == self.forgotPasswordButton)
|
|
|
|
{
|
2016-05-03 13:54:05 +00:00
|
|
|
// Update UI to reset password
|
|
|
|
self.authType = MXKAuthenticationTypeForgotPassword;
|
2016-01-27 17:17:43 +00:00
|
|
|
}
|
|
|
|
else if (sender == self.rightBarButtonItem)
|
|
|
|
{
|
2016-04-07 07:28:27 +00:00
|
|
|
// Check whether a request is in progress
|
|
|
|
if (!self.userInteractionEnabled)
|
|
|
|
{
|
2016-04-21 15:53:52 +00:00
|
|
|
// Cancel the current operation
|
|
|
|
[self cancel];
|
2016-04-07 07:28:27 +00:00
|
|
|
}
|
|
|
|
else if (self.authType == MXKAuthenticationTypeLogin)
|
2016-01-27 17:17:43 +00:00
|
|
|
{
|
|
|
|
self.authType = MXKAuthenticationTypeRegister;
|
|
|
|
self.rightBarButtonItem.title = NSLocalizedStringFromTable(@"auth_login", @"Vector", nil);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self.authType = MXKAuthenticationTypeLogin;
|
|
|
|
self.rightBarButtonItem.title = NSLocalizedStringFromTable(@"auth_register", @"Vector", nil);
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 10:43:08 +00:00
|
|
|
else if (sender == self.mainNavigationItem.leftBarButtonItem)
|
2016-04-01 14:31:13 +00:00
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
if ([self.authInputsView isKindOfClass:AuthInputsView.class])
|
2016-04-01 14:31:13 +00:00
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
AuthInputsView *authInputsview = (AuthInputsView*)self.authInputsView;
|
2016-04-01 14:31:13 +00:00
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
// Hide the supported 3rd party ids which may be added to the account
|
|
|
|
authInputsview.thirdPartyIdentifiersHidden = YES;
|
2016-04-01 14:31:13 +00:00
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
[self updateRegistrationScreenWithThirdPartyIdentifiersHidden:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sender == self.submitButton)
|
|
|
|
{
|
|
|
|
// Handle here the second screen used to manage the 3rd party ids during the registration.
|
2017-03-20 18:43:24 +00:00
|
|
|
// Except if there is an external set of parameters defined to perform a registration.
|
|
|
|
if (self.authType == MXKAuthenticationTypeRegister && !self.externalRegistrationParameters)
|
2017-03-01 10:43:08 +00:00
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if ([self.authInputsView isKindOfClass:AuthInputsView.class])
|
|
|
|
{
|
|
|
|
AuthInputsView *authInputsview = (AuthInputsView*)self.authInputsView;
|
2016-06-04 15:39:56 +00:00
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
// Show the 3rd party ids screen if it is not shown yet
|
2017-03-05 17:21:37 +00:00
|
|
|
if (authInputsview.areThirdPartyIdentifiersSupported && authInputsview.isThirdPartyIdentifiersHidden)
|
2017-03-01 10:43:08 +00:00
|
|
|
{
|
|
|
|
[self dismissKeyboard];
|
|
|
|
|
|
|
|
[self.authenticationActivityIndicator startAnimating];
|
|
|
|
|
|
|
|
// Check parameters validity
|
|
|
|
NSString *errorMsg = [self.authInputsView validateParameters];
|
|
|
|
if (errorMsg)
|
|
|
|
{
|
|
|
|
[self onFailureDuringAuthRequest:[NSError errorWithDomain:MXKAuthErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:errorMsg}]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self isUserNameInUse:^(BOOL isUserNameInUse) {
|
|
|
|
|
|
|
|
if (isUserNameInUse)
|
|
|
|
{
|
|
|
|
NSLog(@"[AuthenticationVC] User name is already use");
|
|
|
|
[self onFailureDuringAuthRequest:[NSError errorWithDomain:MXKAuthErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey:[NSBundle mxk_localizedStringForKey:@"auth_username_in_use"]}]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self.authenticationActivityIndicator stopAnimating];
|
|
|
|
|
|
|
|
// Show the supported 3rd party ids which may be added to the account
|
|
|
|
authInputsview.thirdPartyIdentifiersHidden = NO;
|
|
|
|
|
|
|
|
[self updateRegistrationScreenWithThirdPartyIdentifiersHidden:NO];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 14:31:13 +00:00
|
|
|
}
|
2017-03-01 10:43:08 +00:00
|
|
|
|
|
|
|
[super onButtonPressed:sender];
|
|
|
|
}
|
|
|
|
else if (sender == self.skipButton)
|
|
|
|
{
|
|
|
|
// Reset the potential email or phone values
|
|
|
|
if ([self.authInputsView isKindOfClass:AuthInputsView.class])
|
2016-04-01 14:31:13 +00:00
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
AuthInputsView *authInputsview = (AuthInputsView*)self.authInputsView;
|
|
|
|
|
|
|
|
[authInputsview resetThirdPartyIdentifiers];
|
2016-04-01 14:31:13 +00:00
|
|
|
}
|
2017-03-01 10:43:08 +00:00
|
|
|
|
|
|
|
[super onButtonPressed:self.submitButton];
|
2016-04-01 14:31:13 +00:00
|
|
|
}
|
2016-01-27 17:17:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
[super onButtonPressed:sender];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 11:56:37 +00:00
|
|
|
- (void)onFailureDuringAuthRequest:(NSError *)error
|
|
|
|
{
|
2016-09-14 15:28:59 +00:00
|
|
|
// Homeserver migration: When the default homeserver url is different from matrix.org,
|
|
|
|
// the login (or forgot pwd) process with an existing matrix.org accounts will then fail.
|
2016-05-09 11:56:37 +00:00
|
|
|
// Patch: Falling back to matrix.org HS so we don't break everyone's logins
|
2016-09-16 12:27:20 +00:00
|
|
|
if ([self.homeServerTextField.text isEqualToString:self.defaultHomeServerUrl] && ![self.defaultHomeServerUrl isEqualToString:@"https://matrix.org"])
|
2016-05-09 11:56:37 +00:00
|
|
|
{
|
2016-05-09 22:00:07 +00:00
|
|
|
MXError *mxError = [[MXError alloc] initWithNSError:error];
|
|
|
|
|
|
|
|
if (self.authType == MXKAuthenticationTypeLogin)
|
2016-05-09 11:56:37 +00:00
|
|
|
{
|
|
|
|
if (mxError && [mxError.errcode isEqualToString:kMXErrCodeStringForbidden])
|
|
|
|
{
|
2016-05-09 22:00:07 +00:00
|
|
|
// Falling back to matrix.org HS
|
2016-05-11 09:56:33 +00:00
|
|
|
NSLog(@"[AuthenticationVC] Retry login against matrix.org");
|
2016-05-09 11:56:37 +00:00
|
|
|
|
2016-05-09 22:00:07 +00:00
|
|
|
// Store the current error, and change the homeserver url
|
2016-05-09 11:56:37 +00:00
|
|
|
loginError = error;
|
|
|
|
[self setHomeServerTextFieldText:@"https://matrix.org"];
|
|
|
|
|
2016-05-09 22:00:07 +00:00
|
|
|
// Trigger a new request
|
2016-05-09 11:56:37 +00:00
|
|
|
[self onButtonPressed:self.submitButton];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 22:00:07 +00:00
|
|
|
else if (self.authType == MXKAuthenticationTypeForgotPassword)
|
2016-05-09 11:56:37 +00:00
|
|
|
{
|
2016-05-09 22:00:07 +00:00
|
|
|
if (mxError && [mxError.errcode isEqualToString:kMXErrCodeStringNotFound])
|
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if ([self.authInputsView isKindOfClass:ForgotPasswordInputsView.class])
|
|
|
|
{
|
|
|
|
// Falling back to matrix.org HS
|
2016-05-11 09:56:33 +00:00
|
|
|
NSLog(@"[AuthenticationVC] Retry forgot password against matrix.org");
|
2016-05-09 22:00:07 +00:00
|
|
|
|
|
|
|
// Store the current error, and change the homeserver url
|
|
|
|
loginError = error;
|
|
|
|
[self setHomeServerTextFieldText:@"https://matrix.org"];
|
|
|
|
|
|
|
|
// Trigger a new request
|
|
|
|
ForgotPasswordInputsView *authInputsView = (ForgotPasswordInputsView*)self.authInputsView;
|
|
|
|
[authInputsView.nextStepButton sendActionsForControlEvents:UIControlEventTouchUpInside];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 11:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:00:07 +00:00
|
|
|
// Check whether we were retrying against matrix.org HS
|
|
|
|
if (loginError)
|
|
|
|
{
|
|
|
|
// This is not an existing matrix.org accounts
|
2016-05-11 09:56:33 +00:00
|
|
|
NSLog(@"[AuthenticationVC] This is not an existing matrix.org accounts");
|
2016-05-09 22:00:07 +00:00
|
|
|
|
|
|
|
// Restore the default HS
|
2016-09-14 15:28:59 +00:00
|
|
|
[self setHomeServerTextFieldText:nil];
|
2016-05-09 22:00:07 +00:00
|
|
|
|
|
|
|
// Consider the original login error
|
|
|
|
[super onFailureDuringAuthRequest:loginError];
|
|
|
|
loginError = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[super onFailureDuringAuthRequest:error];
|
|
|
|
}
|
2016-05-09 11:56:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 17:21:37 +00:00
|
|
|
- (void)onSuccessfulLogin:(MXCredentials*)credentials
|
|
|
|
{
|
|
|
|
// Check whether a third party identifiers has not been used
|
|
|
|
if ([self.authInputsView isKindOfClass:AuthInputsView.class])
|
|
|
|
{
|
|
|
|
AuthInputsView *authInputsview = (AuthInputsView*)self.authInputsView;
|
|
|
|
if (authInputsview.isThirdPartyIdentifierPending)
|
|
|
|
{
|
|
|
|
// Alert user
|
|
|
|
if (alert)
|
|
|
|
{
|
|
|
|
[alert dismiss:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
alert = [[MXKAlert alloc] initWithTitle:NSLocalizedStringFromTable(@"warning", @"Vector", nil) message:NSLocalizedStringFromTable(@"auth_add_email_and_phone_warning", @"Vector", nil) style:MXKAlertStyleAlert];
|
|
|
|
|
|
|
|
[alert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"ok"] style:MXKAlertActionStyleCancel handler:^(MXKAlert *alert)
|
|
|
|
{
|
|
|
|
[super onSuccessfulLogin:credentials];
|
|
|
|
}];
|
|
|
|
|
|
|
|
[alert showInViewController:self];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[super onSuccessfulLogin:credentials];
|
|
|
|
}
|
|
|
|
|
2016-06-02 12:33:21 +00:00
|
|
|
- (void)updateForgotPwdButtonVisibility
|
|
|
|
{
|
|
|
|
self.forgotPasswordButton.hidden = (self.authType != MXKAuthenticationTypeLogin);
|
|
|
|
|
|
|
|
// Adjust minimum leading constraint of the submit button
|
|
|
|
if (self.forgotPasswordButton.isHidden)
|
|
|
|
{
|
|
|
|
self.submitButtonMinLeadingConstraint.constant = 19;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CGRect frame = self.forgotPasswordButton.frame;
|
|
|
|
self.submitButtonMinLeadingConstraint.constant = frame.origin.x + frame.size.width + 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 17:17:43 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
- (void)updateRegistrationScreenWithThirdPartyIdentifiersHidden:(BOOL)thirdPartyIdentifiersHidden
|
|
|
|
{
|
|
|
|
self.skipButton.hidden = thirdPartyIdentifiersHidden;
|
|
|
|
|
|
|
|
self.serverOptionsContainer.hidden = !thirdPartyIdentifiersHidden;
|
|
|
|
[self refreshContentViewHeightConstraint];
|
|
|
|
|
|
|
|
if (thirdPartyIdentifiersHidden)
|
|
|
|
{
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_register", @"Vector", nil) forState:UIControlStateNormal];
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_register", @"Vector", nil) forState:UIControlStateHighlighted];
|
|
|
|
|
|
|
|
self.mainNavigationItem.leftBarButtonItem = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_submit", @"Vector", nil) forState:UIControlStateNormal];
|
|
|
|
[self.submitButton setTitle:NSLocalizedStringFromTable(@"auth_submit", @"Vector", nil) forState:UIControlStateHighlighted];
|
|
|
|
|
|
|
|
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPressed:)];
|
|
|
|
self.mainNavigationItem.leftBarButtonItem = leftBarButtonItem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 12:24:35 +00:00
|
|
|
- (void)refreshContentViewHeightConstraint
|
|
|
|
{
|
|
|
|
// Refresh content view height by considering the options container display.
|
2017-03-01 10:43:08 +00:00
|
|
|
CGFloat constant = self.optionsContainer.frame.origin.y + 10;
|
2016-05-27 12:24:35 +00:00
|
|
|
|
|
|
|
if (!self.optionsContainer.isHidden)
|
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
constant += self.serverOptionsContainer.frame.origin.y;
|
|
|
|
|
2016-05-27 12:24:35 +00:00
|
|
|
if (!self.serverOptionsContainer.isHidden)
|
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
CGRect customServersContainerFrame = self.customServersContainer.frame;
|
|
|
|
constant += customServersContainerFrame.origin.y;
|
|
|
|
|
|
|
|
if (!self.customServersContainer.isHidden)
|
|
|
|
{
|
|
|
|
constant += customServersContainerFrame.size.height;
|
|
|
|
}
|
2016-05-27 12:24:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-01 10:43:08 +00:00
|
|
|
|
|
|
|
self.contentViewHeightConstraint.constant = constant;
|
2016-05-27 12:24:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
- (void)hideCustomServers:(BOOL)hidden
|
2016-01-27 17:17:43 +00:00
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
if (self.customServersContainer.isHidden == hidden)
|
2016-01-27 17:17:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hidden)
|
|
|
|
{
|
|
|
|
[self.homeServerTextField resignFirstResponder];
|
|
|
|
[self.identityServerTextField resignFirstResponder];
|
|
|
|
|
2016-04-05 16:43:52 +00:00
|
|
|
// Report server url typed by the user as custom url.
|
|
|
|
NSString *homeServerURL = self.homeServerTextField.text;
|
|
|
|
if (homeServerURL.length && ![homeServerURL isEqualToString:self.defaultHomeServerUrl])
|
|
|
|
{
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:homeServerURL forKey:@"customHomeServerURL"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"customHomeServerURL"];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *identityServerURL = self.identityServerTextField.text;
|
|
|
|
if (identityServerURL.length && ![identityServerURL isEqualToString:self.defaultIdentityServerUrl])
|
|
|
|
{
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:identityServerURL forKey:@"customIdentityServerURL"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"customIdentityServerURL"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
|
|
|
|
|
// Restore default configuration
|
|
|
|
[self setHomeServerTextFieldText:self.defaultHomeServerUrl];
|
|
|
|
[self setIdentityServerTextFieldText:self.defaultIdentityServerUrl];
|
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
[self.customServersTickButton setImage:[UIImage imageNamed:@"selection_untick"] forState:UIControlStateNormal];
|
|
|
|
self.customServersContainer.hidden = YES;
|
2016-01-27 17:17:43 +00:00
|
|
|
|
|
|
|
// Refresh content view height
|
2017-03-01 10:43:08 +00:00
|
|
|
self.contentViewHeightConstraint.constant -= self.customServersContainer.frame.size.height;
|
2016-01-27 17:17:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-05 16:43:52 +00:00
|
|
|
// Load custom configuration
|
|
|
|
NSString *customHomeServerURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"customHomeServerURL"];
|
|
|
|
if (customHomeServerURL.length)
|
|
|
|
{
|
|
|
|
[self setHomeServerTextFieldText:customHomeServerURL];
|
|
|
|
}
|
|
|
|
NSString *customIdentityServerURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"customIdentityServerURL"];
|
|
|
|
if (customIdentityServerURL.length)
|
|
|
|
{
|
|
|
|
[self setIdentityServerTextFieldText:customIdentityServerURL];
|
|
|
|
}
|
|
|
|
|
2017-03-01 10:43:08 +00:00
|
|
|
[self.customServersTickButton setImage:[UIImage imageNamed:@"selection_tick"] forState:UIControlStateNormal];
|
|
|
|
self.customServersContainer.hidden = NO;
|
2016-01-27 17:17:43 +00:00
|
|
|
|
|
|
|
// Refresh content view height
|
2017-03-01 10:43:08 +00:00
|
|
|
self.contentViewHeightConstraint.constant += self.customServersContainer.frame.size.height;
|
2016-01-27 17:17:43 +00:00
|
|
|
|
|
|
|
// Scroll to display server options
|
|
|
|
CGPoint offset = self.authenticationScrollView.contentOffset;
|
2017-03-01 10:43:08 +00:00
|
|
|
offset.y += self.customServersContainer.frame.size.height;
|
2016-01-27 17:17:43 +00:00
|
|
|
self.authenticationScrollView.contentOffset = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:00:07 +00:00
|
|
|
#pragma mark - KVO
|
|
|
|
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
|
|
|
|
{
|
|
|
|
// Override here the handling of the authInputsView height change.
|
|
|
|
if ([@"viewHeightConstraint.constant" isEqualToString:keyPath])
|
|
|
|
{
|
|
|
|
self.authInputContainerViewHeightConstraint.constant = self.authInputsView.viewHeightConstraint.constant;
|
|
|
|
|
|
|
|
// Force to render the view
|
|
|
|
[self.view layoutIfNeeded];
|
2016-05-27 12:24:35 +00:00
|
|
|
|
|
|
|
// Refresh content view height by considering the updated frame of the options container.
|
|
|
|
[self refreshContentViewHeightConstraint];
|
2016-05-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 14:19:12 +00:00
|
|
|
#pragma mark - MXKAuthenticationViewControllerDelegate
|
2015-02-10 15:12:58 +00:00
|
|
|
|
2016-01-27 11:40:26 +00:00
|
|
|
- (void)authenticationViewController:(MXKAuthenticationViewController *)authenticationViewController didLogWithUserId:(NSString *)userId
|
|
|
|
{
|
2017-03-01 10:43:08 +00:00
|
|
|
// Hide the custom server details in order to save customized inputs
|
|
|
|
[self hideCustomServers:YES];
|
2015-02-10 15:12:58 +00:00
|
|
|
|
2015-05-21 16:15:45 +00:00
|
|
|
// Remove auth view controller on successful login
|
2016-01-27 11:40:26 +00:00
|
|
|
if (self.navigationController)
|
|
|
|
{
|
2015-05-21 16:15:45 +00:00
|
|
|
// Pop the view controller
|
|
|
|
[self.navigationController popViewControllerAnimated:YES];
|
2016-01-27 11:40:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-21 16:15:45 +00:00
|
|
|
// Dismiss on successful login
|
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
}
|
2015-02-24 09:07:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 17:21:37 +00:00
|
|
|
#pragma mark - MXKAuthInputsViewDelegate
|
|
|
|
|
|
|
|
- (void)authInputsView:(MXKAuthInputsView *)authInputsView presentViewController:(UIViewController*)viewControllerToPresent
|
|
|
|
{
|
|
|
|
[self dismissKeyboard];
|
|
|
|
|
|
|
|
[self presentViewController:viewControllerToPresent animated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)authInputsViewDidCancelOperation:(MXKAuthInputsView *)authInputsView
|
|
|
|
{
|
|
|
|
[self cancel];
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:14:12 +00:00
|
|
|
@end
|