CCLogin.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissCCLogin) name:@"dismissCCLogin" object:nil];
  42. }
  43. return self;
  44. }
  45. - (void)viewDidLoad
  46. {
  47. [super viewDidLoad];
  48. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  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. [[OCNetworking sharedManager] serverStatusUrl:self.baseUrl.text completion:^(NSString *serverProductName, NSInteger versionMajor, NSInteger versionMicro, NSInteger versionMinor, BOOL extendedSupport, NSString *message, NSInteger errorCode) {
  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. [[CCCertificate sharedManager] presentViewControllerCertificateWithAccount:nil viewController:self delegate:self];
  171. } else {
  172. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_connection_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  173. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  174. [alertController addAction:okAction];
  175. [self presentViewController:alertController animated:YES completion:nil];
  176. }
  177. }
  178. }];
  179. }
  180. - (void)trustedCerticateAccepted
  181. {
  182. NSLog(@"[LOG] Certificate trusted");
  183. }
  184. - (void)trustedCerticateDenied
  185. {
  186. // if (_loginType == k_login_Modify_Password)
  187. //[self handleAnnulla:self];
  188. }
  189. #pragma --------------------------------------------------------------------------------------------
  190. #pragma mark == TextField ==
  191. #pragma --------------------------------------------------------------------------------------------
  192. -(void)textFieldDidBeginEditing:(UITextField *)textField
  193. {
  194. if (textField == self.password) {
  195. self.toggleVisiblePassword.hidden = NO;
  196. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.customerText};
  197. }
  198. }
  199. -(void)textFieldDidEndEditing:(UITextField *)textField
  200. {
  201. if (textField == self.password) {
  202. self.toggleVisiblePassword.hidden = YES;
  203. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.customerText};
  204. }
  205. }
  206. #pragma --------------------------------------------------------------------------------------------
  207. #pragma mark === NCLoginQRCodeDelegate ===
  208. #pragma --------------------------------------------------------------------------------------------
  209. - (void)dismissQRCode:(NSString *)value metadataType:(NSString *)metadataType
  210. {
  211. NSString *protocolLogin = [[NCBrandOptions sharedInstance].webLoginAutenticationProtocol stringByAppendingString:@"login/"];
  212. if (value != nil && [value hasPrefix:protocolLogin] && [value containsString:@"user:"] && [value containsString:@"password:"] && [value containsString:@"server:"]) {
  213. value = [value stringByReplacingOccurrencesOfString:protocolLogin withString:@""];
  214. NSArray *valueArray = [value componentsSeparatedByString: @"&"];
  215. if (valueArray.count == 3) {
  216. _imageUser.hidden = NO;
  217. _user.hidden = NO;
  218. _imagePassword.hidden = NO;
  219. _password.hidden = NO;
  220. [self.loginTypeView setTitle:NSLocalizedString(@"_web_login_", nil) forState:UIControlStateNormal];
  221. self.user.text = [valueArray[0] stringByReplacingOccurrencesOfString:@"user:" withString:@""];
  222. self.password.text = [valueArray[1] stringByReplacingOccurrencesOfString:@"password:" withString:@""];
  223. self.baseUrl.text = [valueArray[2] stringByReplacingOccurrencesOfString:@"server:" withString:@""];
  224. // Check whether baseUrl contain protocol. If not add https:// by default.
  225. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  226. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  227. }
  228. NSString *url = self.baseUrl.text;
  229. NSString *user = self.user.text;
  230. NSString *token = self.password.text;
  231. self.login.enabled = NO;
  232. [self.activity startAnimating];
  233. [[OCNetworking sharedManager] checkServerUrl:[NSString stringWithFormat:@"%@%@", url, k_webDAV] user:user userID:user password:token completion:^(NSString *message, NSInteger errorCode) {
  234. [self.activity stopAnimating];
  235. self.login.enabled = YES;
  236. if (errorCode == 0) {
  237. NSString *account = [NSString stringWithFormat:@"%@ %@", user, url];
  238. // NO account found, clear
  239. if ([NCManageDatabase.sharedInstance getAccounts] == nil) { [NCUtility.sharedInstance removeAllSettings]; }
  240. // STOP Intro
  241. [CCUtility setIntro:YES];
  242. [[NCManageDatabase sharedInstance] deleteAccount:account];
  243. [[NCManageDatabase sharedInstance] addAccount:account url:url user:user password:token];
  244. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  245. // Setting appDelegate active account
  246. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:[CCUtility getPassword:tableAccount.account]];
  247. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil userInfo:nil];
  248. [self dismissViewControllerAnimated:YES completion:nil];
  249. } else {
  250. if (errorCode != NSURLErrorServerCertificateUntrusted) {
  251. NSString *messageAlert = [NSString stringWithFormat:@"%@.\n%@", NSLocalizedString(@"_not_possible_connect_to_server_", nil), message];
  252. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:messageAlert preferredStyle:UIAlertControllerStyleAlert];
  253. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  254. [alertController addAction:okAction];
  255. [self presentViewController:alertController animated:YES completion:nil];
  256. }
  257. }
  258. }];
  259. }
  260. }
  261. }
  262. - (void)dismissCCLogin
  263. {
  264. [self dismissViewControllerAnimated:NO completion:nil];
  265. }
  266. #pragma --------------------------------------------------------------------------------------------
  267. #pragma mark == Action ==
  268. #pragma --------------------------------------------------------------------------------------------
  269. - (IBAction)handlebaseUrlchange:(id)sender
  270. {
  271. if ([self.baseUrl.text length] > 0 && !_user.hidden && !_password.hidden)
  272. [self performSelector:@selector(testUrl) withObject:nil];
  273. }
  274. - (IBAction)handleButtonLogin:(id)sender
  275. {
  276. if ([self.baseUrl.text length] > 0 && _user.hidden && _password.hidden) {
  277. [self testUrl];
  278. return;
  279. }
  280. if ([self.baseUrl.text length] > 0 && [self.user.text length] && [self.password.text length]) {
  281. // remove last char if /
  282. if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
  283. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  284. NSString *url = self.baseUrl.text;
  285. NSString *user = self.user.text;
  286. NSString *password = self.password.text;
  287. self.login.enabled = NO;
  288. [self.activity startAnimating];
  289. [[OCNetworking sharedManager] getAppPassword:url username:user password:password completion:^(NSString *token, NSString *message, NSInteger errorCode) {
  290. [self.activity stopAnimating];
  291. self.login.enabled = YES;
  292. if (errorCode == 0) {
  293. NSString *account = [NSString stringWithFormat:@"%@ %@", user, url];
  294. // NO account found, clear
  295. if ([NCManageDatabase.sharedInstance getAccounts] == nil) { [NCUtility.sharedInstance removeAllSettings]; }
  296. // STOP Intro
  297. [CCUtility setIntro:YES];
  298. [[NCManageDatabase sharedInstance] deleteAccount:account];
  299. [[NCManageDatabase sharedInstance] addAccount:account url:url user:user password:token];
  300. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  301. // Setting appDelegate active account
  302. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:[CCUtility getPassword:tableAccount.account]];
  303. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil userInfo:nil];
  304. [self dismissViewControllerAnimated:YES completion:nil];
  305. } else {
  306. if (errorCode != NSURLErrorServerCertificateUntrusted) {
  307. NSString *messageAlert = [NSString stringWithFormat:@"%@.\n%@", NSLocalizedString(@"_not_possible_connect_to_server_", nil), message];
  308. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:messageAlert preferredStyle:UIAlertControllerStyleAlert];
  309. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  310. [alertController addAction:okAction];
  311. [self presentViewController:alertController animated:YES completion:nil];
  312. }
  313. }
  314. }];
  315. }
  316. }
  317. - (IBAction)handleAnnulla:(id)sender
  318. {
  319. [self dismissViewControllerAnimated:YES completion:nil];
  320. }
  321. - (IBAction)handleToggleVisiblePassword:(id)sender
  322. {
  323. NSString *currentPassword = self.password.text;
  324. self.password.secureTextEntry = ! self.password.secureTextEntry;
  325. self.password.text = @"";
  326. self.password.text = currentPassword;
  327. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: NCBrandColor.sharedInstance.customerText};
  328. }
  329. - (IBAction)handleLoginTypeView:(id)sender
  330. {
  331. if (_user.hidden && _password.hidden) {
  332. _imageUser.hidden = NO;
  333. _user.hidden = NO;
  334. _imagePassword.hidden = NO;
  335. _password.hidden = NO;
  336. [self.loginTypeView setTitle:NSLocalizedString(@"_web_login_", nil) forState:UIControlStateNormal];
  337. } else {
  338. _imageUser.hidden = YES;
  339. _user.hidden = YES;
  340. _imagePassword.hidden = YES;
  341. _password.hidden = YES;
  342. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  343. }
  344. }
  345. - (IBAction)handleQRCode:(id)sender
  346. {
  347. NCLoginQRCode *qrCode = [[NCLoginQRCode alloc] initWithDelegate:self];
  348. [qrCode scan];
  349. }
  350. @end