CCLogin.m 21 KB

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