CCLogin.m 20 KB

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