CCLogin.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // CCLogin.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/04/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCLogin.h"
  24. #import "AppDelegate.h"
  25. #import "CCUtility.h"
  26. #import "NCBridgeSwift.h"
  27. #import "NCNetworkingEndToEnd.h"
  28. @interface CCLogin () <NCLoginQRCodeDelegate>
  29. {
  30. AppDelegate *appDelegate;
  31. UIView *rootView;
  32. }
  33. @end
  34. @implementation CCLogin
  35. #pragma --------------------------------------------------------------------------------------------
  36. #pragma mark ===== Init =====
  37. #pragma --------------------------------------------------------------------------------------------
  38. - (id)initWithCoder:(NSCoder *)aDecoder
  39. {
  40. if (self = [super initWithCoder:aDecoder]) {
  41. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  42. }
  43. return self;
  44. }
  45. - (void)viewDidLoad
  46. {
  47. [super viewDidLoad];
  48. Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
  49. // Background color
  50. self.view.backgroundColor = NCBrandColor.sharedInstance.customer;
  51. // Image Brand
  52. self.imageBrand.image = [UIImage imageNamed:@"logo"];
  53. // Annulla
  54. [self.annulla setTitle:NSLocalizedString(@"_cancel_", nil) forState:UIControlStateNormal];
  55. self.annulla.tintColor = NCBrandColor.sharedInstance.customerText;
  56. // Base URL
  57. _imageBaseUrl.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginURL"] multiplier:2 color:NCBrandColor.sharedInstance.customerText];
  58. _baseUrl.textColor = NCBrandColor.sharedInstance.customerText;
  59. _baseUrl.tintColor = NCBrandColor.sharedInstance.customerText;
  60. _baseUrl.placeholder = NSLocalizedString(@"_login_url_", nil);
  61. UILabel *baseUrlPlaceholder = object_getIvar(_baseUrl, ivar);
  62. baseUrlPlaceholder.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.7];
  63. [self.baseUrl setFont:[UIFont systemFontOfSize:13]];
  64. [self.baseUrl setDelegate:self];
  65. // User
  66. _imageUser.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginUser"] multiplier:2 color:NCBrandColor.sharedInstance.customerText];
  67. _user.textColor = NCBrandColor.sharedInstance.customerText;
  68. _user.tintColor = NCBrandColor.sharedInstance.customerText;
  69. _user.placeholder = NSLocalizedString(@"_username_", nil);
  70. UILabel *userPlaceholder = object_getIvar(_user, ivar);
  71. userPlaceholder.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.7];
  72. [self.user setFont:[UIFont systemFontOfSize:13]];
  73. [self.user setDelegate:self];
  74. // Password
  75. _imagePassword.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginPassword"] multiplier:2 color:NCBrandColor.sharedInstance.customerText];
  76. _password.textColor = NCBrandColor.sharedInstance.customerText;
  77. _password.tintColor = NCBrandColor.sharedInstance.customerText;
  78. _password.placeholder = NSLocalizedString(@"_password_", nil);
  79. UILabel *passwordPlaceholder = object_getIvar(_password, ivar);
  80. passwordPlaceholder.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.7];
  81. [self.password setFont:[UIFont systemFontOfSize:13]];
  82. [self.password setDelegate:self];
  83. [self.toggleVisiblePassword setImage:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"visiblePassword"] multiplier:2 color:[UIColor whiteColor]] forState:UIControlStateNormal];
  84. // Login
  85. [self.login setTitle:NSLocalizedString(@"_login_", nil) forState:UIControlStateNormal] ;
  86. self.login.backgroundColor = NCBrandColor.sharedInstance.customerText;
  87. self.login.tintColor = [UIColor blackColor];
  88. self.login.layer.cornerRadius = 20;
  89. self.login.clipsToBounds = YES;
  90. // Type view
  91. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  92. [self.loginTypeView setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.7] forState:UIControlStateNormal];
  93. // Brand
  94. if ([NCBrandOptions sharedInstance].disable_request_login_url) {
  95. _baseUrl.text = [NCBrandOptions sharedInstance].loginBaseUrl;
  96. _imageBaseUrl.hidden = YES;
  97. _baseUrl.hidden = YES;
  98. }
  99. // QrCode image
  100. [self.qrCode setImage:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"qrcode"] width:100 height:100 color:[UIColor whiteColor]] forState:UIControlStateNormal];
  101. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  102. if ([listAccount count] == 0) {
  103. _imageUser.hidden = YES;
  104. _user.hidden = YES;
  105. _imagePassword.hidden = YES;
  106. _password.hidden = YES;
  107. _annulla.hidden = YES;
  108. } else {
  109. _imageUser.hidden = YES;
  110. _user.hidden = YES;
  111. _imagePassword.hidden = YES;
  112. _password.hidden = YES;
  113. }
  114. }
  115. - (void)viewDidAppear:(BOOL)animated
  116. {
  117. [super viewDidAppear:animated];
  118. // Stop timer error network
  119. [appDelegate.timerErrorNetworking invalidate];
  120. }
  121. - (void)viewDidDisappear:(BOOL)animated
  122. {
  123. [super viewDidDisappear:animated];
  124. // Start timer
  125. [appDelegate startTimerErrorNetworking];
  126. }
  127. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  128. {
  129. [textField resignFirstResponder];
  130. return YES;
  131. }
  132. #pragma --------------------------------------------------------------------------------------------
  133. #pragma mark == Chech Server URL ==
  134. #pragma --------------------------------------------------------------------------------------------
  135. - (void)testUrl
  136. {
  137. self.login.enabled = NO;
  138. [self.activity startAnimating];
  139. // Check whether baseUrl contain protocol. If not add https:// by default.
  140. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  141. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  142. }
  143. // Remove trailing slash
  144. if ([self.baseUrl.text hasSuffix:@"/"])
  145. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  146. [[NCCommunication sharedInstance] getServerStatusWithServerUrl:self.baseUrl.text completionHandler:^(NSString *serverProductName, NSString *serverVersion, NSInteger versionMajor, NSInteger versionMinor, NSInteger versionMicro, BOOL extendedSupport, NSInteger errorCode, NSString *errorDescription) {
  147. if (errorCode == 0) {
  148. [self.activity stopAnimating];
  149. self.login.enabled = YES;
  150. // Login Flow V2
  151. [[NCCommunication sharedInstance] getLoginFlowV2WithServerUrl:self.baseUrl.text completionHandler:^(NSString *token, NSString *endpoint, NSString *login, NSInteger errorCode, NSString *errorDescription) {
  152. // Login Flow V2
  153. if (errorCode == 0 && [[NCBrandOptions sharedInstance] use_loginflowv2] && token != nil && endpoint != nil && login != nil) {
  154. NCLoginWeb *activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  155. activeLoginWeb.urlBase = self.baseUrl.text;
  156. activeLoginWeb.loginFlowV2Available = true;
  157. activeLoginWeb.loginFlowV2Token = token;
  158. activeLoginWeb.loginFlowV2Endpoint = endpoint;
  159. activeLoginWeb.loginFlowV2Login = login;
  160. [self.navigationController pushViewController:activeLoginWeb animated:true];
  161. }
  162. // Login Flow
  163. else if (_user.hidden && _password.hidden && versionMajor >= k_flow_version_available) {
  164. NCLoginWeb *activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  165. activeLoginWeb.urlBase = self.baseUrl.text;
  166. [self.navigationController pushViewController:activeLoginWeb animated:true];
  167. }
  168. // NO Login Flow available
  169. else if (versionMajor < k_flow_version_available) {
  170. [self.loginTypeView setHidden:YES];
  171. _imageUser.hidden = NO;
  172. _user.hidden = NO;
  173. _imagePassword.hidden = NO;
  174. _password.hidden = NO;
  175. [_user becomeFirstResponder];
  176. }
  177. }];
  178. } else {
  179. [self.activity stopAnimating];
  180. self.login.enabled = YES;
  181. if (errorCode == NSURLErrorServerCertificateUntrusted) {
  182. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_ssl_certificate_untrusted_", nil) message:NSLocalizedString(@"_connect_server_anyway_", nil) preferredStyle:UIAlertControllerStyleAlert];
  183. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  184. [[NCNetworking sharedInstance] wrtiteCertificateWithDirectoryCertificate:[CCUtility getDirectoryCerificates]];
  185. [appDelegate startTimerErrorNetworking];
  186. }]];
  187. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  188. [appDelegate startTimerErrorNetworking];
  189. }]];
  190. [self presentViewController:alertController animated:YES completion:^{
  191. // Stop timer error network
  192. [appDelegate.timerErrorNetworking invalidate];
  193. }];
  194. } else {
  195. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_connection_error_", nil) message:errorDescription preferredStyle:UIAlertControllerStyleAlert];
  196. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  197. [alertController addAction:okAction];
  198. [self presentViewController:alertController animated:YES completion:nil];
  199. }
  200. }
  201. }];
  202. }
  203. #pragma --------------------------------------------------------------------------------------------
  204. #pragma mark == TextField ==
  205. #pragma --------------------------------------------------------------------------------------------
  206. -(void)textFieldDidBeginEditing:(UITextField *)textField
  207. {
  208. if (textField == self.password) {
  209. self.toggleVisiblePassword.hidden = NO;
  210. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.customerText};
  211. }
  212. }
  213. -(void)textFieldDidEndEditing:(UITextField *)textField
  214. {
  215. if (textField == self.password) {
  216. self.toggleVisiblePassword.hidden = YES;
  217. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.customerText};
  218. }
  219. }
  220. #pragma --------------------------------------------------------------------------------------------
  221. #pragma mark === NCLoginQRCodeDelegate ===
  222. #pragma --------------------------------------------------------------------------------------------
  223. - (void)dismissQRCode:(NSString *)value metadataType:(NSString *)metadataType
  224. {
  225. NSString *protocolLogin = [[NCBrandOptions sharedInstance].webLoginAutenticationProtocol stringByAppendingString:@"login/"];
  226. if (value != nil && [value hasPrefix:protocolLogin] && [value containsString:@"user:"] && [value containsString:@"password:"] && [value containsString:@"server:"]) {
  227. value = [value stringByReplacingOccurrencesOfString:protocolLogin withString:@""];
  228. NSArray *valueArray = [value componentsSeparatedByString: @"&"];
  229. if (valueArray.count == 3) {
  230. _imageUser.hidden = NO;
  231. _user.hidden = NO;
  232. _imagePassword.hidden = NO;
  233. _password.hidden = NO;
  234. [self.loginTypeView setTitle:NSLocalizedString(@"_web_login_", nil) forState:UIControlStateNormal];
  235. self.user.text = [valueArray[0] stringByReplacingOccurrencesOfString:@"user:" withString:@""];
  236. self.password.text = [valueArray[1] stringByReplacingOccurrencesOfString:@"password:" withString:@""];
  237. self.baseUrl.text = [valueArray[2] stringByReplacingOccurrencesOfString:@"server:" withString:@""];
  238. // Check whether baseUrl contain protocol. If not add https:// by default.
  239. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  240. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  241. }
  242. NSString *url = self.baseUrl.text;
  243. NSString *user = self.user.text;
  244. NSString *token = self.password.text;
  245. self.login.enabled = NO;
  246. [self.activity startAnimating];
  247. [[OCNetworking sharedManager] checkServerUrl:[NSString stringWithFormat:@"%@%@", url, k_webDAV] user:user userID:user password:token completion:^(NSString *message, NSInteger errorCode) {
  248. [self.activity stopAnimating];
  249. self.login.enabled = YES;
  250. [self AfterLoginWithUrl:url user:user token:token errorCode:errorCode message:message];
  251. }];
  252. }
  253. }
  254. }
  255. #pragma --------------------------------------------------------------------------------------------
  256. #pragma mark == Login ==
  257. #pragma --------------------------------------------------------------------------------------------
  258. - (IBAction)handlebaseUrlchange:(id)sender
  259. {
  260. if ([self.baseUrl.text length] > 0 && !_user.hidden && !_password.hidden)
  261. [self performSelector:@selector(testUrl) withObject:nil];
  262. }
  263. - (IBAction)handleButtonLogin:(id)sender
  264. {
  265. if ([self.baseUrl.text length] > 0 && _user.hidden && _password.hidden) {
  266. [self testUrl];
  267. return;
  268. }
  269. if ([self.baseUrl.text length] > 0 && [self.user.text length] && [self.password.text length]) {
  270. // remove last char if /
  271. if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
  272. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  273. NSString *url = self.baseUrl.text;
  274. NSString *user = self.user.text;
  275. NSString *password = self.password.text;
  276. self.login.enabled = NO;
  277. [self.activity startAnimating];
  278. [[OCNetworking sharedManager] getAppPassword:url username:user password:password completion:^(NSString *token, NSString *message, NSInteger errorCode) {
  279. [self.activity stopAnimating];
  280. self.login.enabled = YES;
  281. [self AfterLoginWithUrl:url user:user token:token errorCode:errorCode message:message];
  282. }];
  283. }
  284. }
  285. - (void)AfterLoginWithUrl:(NSString *)url user:(NSString *)user token:(NSString *)token errorCode:(NSInteger)errorCode message:(NSString *)message
  286. {
  287. if (errorCode == 0) {
  288. NSString *account = [NSString stringWithFormat:@"%@ %@", user, url];
  289. // NO account found, clear
  290. if ([NCManageDatabase.sharedInstance getAccounts] == nil) { [NCUtility.sharedInstance removeAllSettings]; }
  291. [[NCManageDatabase sharedInstance] deleteAccount:account];
  292. [[NCManageDatabase sharedInstance] addAccount:account url:url user:user password:token];
  293. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  294. // Setting appDelegate active account
  295. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:[CCUtility getPassword:tableAccount.account]];
  296. if ([CCUtility getIntro]) {
  297. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  298. [self dismissViewControllerAnimated:YES completion:nil];
  299. } else {
  300. [CCUtility setIntro:YES];
  301. if (self.presentingViewController == nil) {
  302. UISplitViewController *splitController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];
  303. splitController.modalPresentationStyle = UIModalPresentationFullScreen;
  304. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  305. appDelegate.window.rootViewController = splitController;
  306. [appDelegate.window makeKeyWindow];
  307. } else {
  308. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  309. [self dismissViewControllerAnimated:YES completion:nil];
  310. }
  311. }
  312. } else {
  313. if (errorCode != NSURLErrorServerCertificateUntrusted) {
  314. NSString *messageAlert = [NSString stringWithFormat:@"%@.\n%@", NSLocalizedString(@"_not_possible_connect_to_server_", nil), message];
  315. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:messageAlert preferredStyle:UIAlertControllerStyleAlert];
  316. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  317. [alertController addAction:okAction];
  318. [self presentViewController:alertController animated:YES completion:nil];
  319. }
  320. }
  321. }
  322. - (IBAction)handleAnnulla:(id)sender
  323. {
  324. [self dismissViewControllerAnimated:YES completion:nil];
  325. }
  326. - (IBAction)handleToggleVisiblePassword:(id)sender
  327. {
  328. NSString *currentPassword = self.password.text;
  329. self.password.secureTextEntry = ! self.password.secureTextEntry;
  330. self.password.text = @"";
  331. self.password.text = currentPassword;
  332. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: NCBrandColor.sharedInstance.customerText};
  333. }
  334. - (IBAction)handleLoginTypeView:(id)sender
  335. {
  336. if (_user.hidden && _password.hidden) {
  337. _imageUser.hidden = NO;
  338. _user.hidden = NO;
  339. _imagePassword.hidden = NO;
  340. _password.hidden = NO;
  341. [self.loginTypeView setTitle:NSLocalizedString(@"_web_login_", nil) forState:UIControlStateNormal];
  342. } else {
  343. _imageUser.hidden = YES;
  344. _user.hidden = YES;
  345. _imagePassword.hidden = YES;
  346. _password.hidden = YES;
  347. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  348. }
  349. }
  350. - (IBAction)handleQRCode:(id)sender
  351. {
  352. NCLoginQRCode *qrCode = [[NCLoginQRCode alloc] initWithDelegate:self];
  353. [qrCode scan];
  354. }
  355. @end