CCLogin.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //
  2. // CCLogin.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/04/15.
  6. // Copyright (c) 2015 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. @interface CCLogin () <NCLoginQRCodeDelegate>
  28. {
  29. AppDelegate *appDelegate;
  30. UIView *rootView;
  31. UIColor *textColor;
  32. UIColor *textColorOpponent;
  33. UIBarButtonItem *cancelButton;
  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. cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(handleAnnulla:)];
  70. cancelButton.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. } else {
  123. _imageUser.hidden = YES;
  124. _user.hidden = YES;
  125. _imagePassword.hidden = YES;
  126. _password.hidden = YES;
  127. self.navigationItem.leftBarButtonItem = cancelButton;
  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 NO;
  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 shared] getServerStatusWithServerUrl:self.baseUrl.text customUserAgent:nil 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 shared] getLoginFlowV2WithServerUrl:self.baseUrl.text 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 shared] 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. NSString *serverUrl = [NSString stringWithFormat:@"%@%@", url, k_webDAV];
  263. [[NCCommunication shared] checkServerWithServerUrl:serverUrl completionHandler:^(NSInteger errorCode, NSString *errorDescription) {
  264. [self.activity stopAnimating];
  265. self.login.enabled = YES;
  266. [self AfterLoginWithUrl:url user:user token:token errorCode:errorCode message:errorDescription];
  267. }];
  268. }
  269. }
  270. }
  271. #pragma --------------------------------------------------------------------------------------------
  272. #pragma mark == Login ==
  273. #pragma --------------------------------------------------------------------------------------------
  274. - (IBAction)handlebaseUrlchange:(id)sender
  275. {
  276. if ([self.baseUrl.text length] > 0 && !_user.hidden && !_password.hidden)
  277. [self performSelector:@selector(testUrl) withObject:nil];
  278. }
  279. - (IBAction)handleButtonLogin:(id)sender
  280. {
  281. if ([self.baseUrl.text length] > 0 && _user.hidden && _password.hidden) {
  282. [self testUrl];
  283. return;
  284. }
  285. if ([self.baseUrl.text length] > 0 && [self.user.text length] && [self.password.text length]) {
  286. // remove last char if /
  287. if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
  288. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  289. NSString *url = self.baseUrl.text;
  290. NSString *user = self.user.text;
  291. NSString *password = self.password.text;
  292. self.login.enabled = NO;
  293. [self.activity startAnimating];
  294. [[NCCommunication shared] getAppPasswordWithServerUrl:url username:user password:password customUserAgent:nil completionHandler:^(NSString *token, NSInteger errorCode, NSString *errorDescription) {
  295. [self.activity stopAnimating];
  296. self.login.enabled = YES;
  297. [self AfterLoginWithUrl:url user:user token:token errorCode:errorCode message:errorDescription];
  298. }];
  299. }
  300. }
  301. - (void)AfterLoginWithUrl:(NSString *)url user:(NSString *)user token:(NSString *)token errorCode:(NSInteger)errorCode message:(NSString *)message
  302. {
  303. if (errorCode == 0) {
  304. NSString *account = [NSString stringWithFormat:@"%@ %@", user, url];
  305. // NO account found, clear
  306. if ([NCManageDatabase.sharedInstance getAccounts] == nil) { [NCUtility.sharedInstance removeAllSettings]; }
  307. [[NCManageDatabase sharedInstance] deleteAccount:account];
  308. [[NCManageDatabase sharedInstance] addAccount:account url:url user:user password:token];
  309. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  310. // Setting appDelegate active account
  311. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:[CCUtility getPassword:tableAccount.account]];
  312. if ([CCUtility getIntro]) {
  313. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  314. [self dismissViewControllerAnimated:YES completion:nil];
  315. } else {
  316. [CCUtility setIntro:YES];
  317. if (self.presentingViewController == nil) {
  318. UISplitViewController *splitController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];
  319. splitController.modalPresentationStyle = UIModalPresentationFullScreen;
  320. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  321. appDelegate.window.rootViewController = splitController;
  322. [appDelegate.window makeKeyWindow];
  323. } else {
  324. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  325. [self dismissViewControllerAnimated:YES completion:nil];
  326. }
  327. }
  328. } else {
  329. if (errorCode != NSURLErrorServerCertificateUntrusted) {
  330. NSString *messageAlert = [NSString stringWithFormat:@"%@.\n%@", NSLocalizedString(@"_not_possible_connect_to_server_", nil), message];
  331. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:messageAlert preferredStyle:UIAlertControllerStyleAlert];
  332. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  333. [alertController addAction:okAction];
  334. [self presentViewController:alertController animated:YES completion:nil];
  335. }
  336. }
  337. }
  338. - (IBAction)handleAnnulla:(id)sender
  339. {
  340. [self dismissViewControllerAnimated:YES completion:nil];
  341. }
  342. - (IBAction)handleToggleVisiblePassword:(id)sender
  343. {
  344. NSString *currentPassword = self.password.text;
  345. self.password.secureTextEntry = ! self.password.secureTextEntry;
  346. self.password.text = @"";
  347. self.password.text = currentPassword;
  348. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: textColor};
  349. }
  350. - (IBAction)handleLoginTypeView:(id)sender
  351. {
  352. if (_user.hidden && _password.hidden) {
  353. _imageUser.hidden = NO;
  354. _user.hidden = NO;
  355. _imagePassword.hidden = NO;
  356. _password.hidden = NO;
  357. [self.loginTypeView setTitle:NSLocalizedString(@"_web_login_", nil) forState:UIControlStateNormal];
  358. } else {
  359. _imageUser.hidden = YES;
  360. _user.hidden = YES;
  361. _imagePassword.hidden = YES;
  362. _password.hidden = YES;
  363. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  364. }
  365. }
  366. - (IBAction)handleQRCode:(id)sender
  367. {
  368. NCLoginQRCode *qrCode = [[NCLoginQRCode alloc] initWithDelegate:self];
  369. [qrCode scan];
  370. }
  371. @end