CCLogin.m 20 KB

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