CCLogin.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 () <CCLoginDelegateWeb, NCLoginQRCodeDelegate>
  29. {
  30. AppDelegate *appDelegate;
  31. UIView *rootView;
  32. }
  33. @end
  34. @implementation CCLogin
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  39. Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
  40. // Background color
  41. self.view.backgroundColor = [NCBrandColor sharedInstance].customer;
  42. // Image Brand
  43. self.imageBrand.image = [UIImage imageNamed:@"logo"];
  44. // Annulla
  45. [self.annulla setTitle:NSLocalizedString(@"_cancel_", nil) forState:UIControlStateNormal];
  46. self.annulla.tintColor = [NCBrandColor sharedInstance].customerText;
  47. // Base URL
  48. _imageBaseUrl.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginURL"] multiplier:2 color:[NCBrandColor sharedInstance].customerText];
  49. _baseUrl.textColor = [NCBrandColor sharedInstance].customerText;
  50. _baseUrl.tintColor = [NCBrandColor sharedInstance].customerText;
  51. _baseUrl.placeholder = NSLocalizedString(@"_login_url_", nil);
  52. UILabel *baseUrlPlaceholder = object_getIvar(_baseUrl, ivar);
  53. baseUrlPlaceholder.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.7];
  54. [self.baseUrl setFont:[UIFont systemFontOfSize:13]];
  55. [self.baseUrl setDelegate:self];
  56. // User
  57. _imageUser.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginUser"] multiplier:2 color:[NCBrandColor sharedInstance].customerText];
  58. _user.textColor = [NCBrandColor sharedInstance].customerText;
  59. _user.tintColor = [NCBrandColor sharedInstance].customerText;
  60. _user.placeholder = NSLocalizedString(@"_username_", nil);
  61. UILabel *userPlaceholder = object_getIvar(_user, ivar);
  62. userPlaceholder.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.7];
  63. [self.user setFont:[UIFont systemFontOfSize:13]];
  64. [self.user setDelegate:self];
  65. // Password
  66. _imagePassword.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginPassword"] multiplier:2 color:[NCBrandColor sharedInstance].customerText];
  67. _password.textColor = [NCBrandColor sharedInstance].customerText;
  68. _password.tintColor = [NCBrandColor sharedInstance].customerText;
  69. _password.placeholder = NSLocalizedString(@"_password_", nil);
  70. UILabel *passwordPlaceholder = object_getIvar(_password, ivar);
  71. passwordPlaceholder.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.7];
  72. [self.password setFont:[UIFont systemFontOfSize:13]];
  73. [self.password setDelegate:self];
  74. [self.toggleVisiblePassword setImage:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"visiblePassword"] multiplier:2 color:[UIColor whiteColor]] forState:UIControlStateNormal];
  75. // Login
  76. [self.login setTitle:NSLocalizedString(@"_login_", nil) forState:UIControlStateNormal] ;
  77. self.login.backgroundColor = [NCBrandColor sharedInstance].customerText;
  78. self.login.tintColor = [UIColor blackColor];
  79. self.login.layer.cornerRadius = 20;
  80. self.login.clipsToBounds = YES;
  81. // Type view
  82. [self.loginTypeView setTitle:NSLocalizedString(@"_traditional_login_", nil) forState:UIControlStateNormal];
  83. [self.loginTypeView setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.7] forState:UIControlStateNormal];
  84. // Brand
  85. if ([NCBrandOptions sharedInstance].disable_request_login_url) {
  86. _baseUrl.text = [NCBrandOptions sharedInstance].loginBaseUrl;
  87. _imageBaseUrl.hidden = YES;
  88. _baseUrl.hidden = YES;
  89. }
  90. if (_loginType == k_login_Add ) {
  91. _imageUser.hidden = YES;
  92. _user.hidden = YES;
  93. _imagePassword.hidden = YES;
  94. _password.hidden = YES;
  95. }
  96. if (_loginType == k_login_Add_Forced) {
  97. _imageUser.hidden = YES;
  98. _user.hidden = YES;
  99. _imagePassword.hidden = YES;
  100. _password.hidden = YES;
  101. _annulla.hidden = YES;
  102. }
  103. if (_loginType == k_login_Modify_Password) {
  104. _baseUrl.text = appDelegate.activeUrl;
  105. _baseUrl.userInteractionEnabled = NO;
  106. _baseUrl.textColor = [UIColor lightGrayColor];
  107. _user.text = appDelegate.activeUser;
  108. _user.userInteractionEnabled = NO;
  109. _user.textColor = [UIColor lightGrayColor];
  110. }
  111. }
  112. - (void)viewWillAppear:(BOOL)animated
  113. {
  114. [super viewWillAppear:animated];
  115. // verify URL
  116. if (_loginType == k_login_Modify_Password && [self.baseUrl.text length] > 0)
  117. [self testUrl];
  118. }
  119. // E' apparsa
  120. - (void)viewDidAppear:(BOOL)animated
  121. {
  122. [super viewDidAppear:animated];
  123. // Stop timer error network
  124. [appDelegate.timerErrorNetworking invalidate];
  125. }
  126. //
  127. - (void)viewDidDisappear:(BOOL)animated
  128. {
  129. [super viewDidDisappear:animated];
  130. // Start timer
  131. [appDelegate startTimerErrorNetworking];
  132. }
  133. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  134. {
  135. [textField resignFirstResponder];
  136. return YES;
  137. }
  138. #pragma --------------------------------------------------------------------------------------------
  139. #pragma mark == Chech Server URL ==
  140. #pragma --------------------------------------------------------------------------------------------
  141. - (void)testUrl
  142. {
  143. self.login.enabled = NO;
  144. [self.activity startAnimating];
  145. // Check whether baseUrl contain protocol. If not add https:// by default.
  146. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  147. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  148. }
  149. // Remove trailing slash
  150. if ([self.baseUrl.text hasSuffix:@"/"])
  151. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  152. [[OCNetworking sharedManager] serverStatusUrl:self.baseUrl.text completion:^(NSString *serverProductName, NSInteger versionMajor, NSInteger versionMicro, NSInteger versionMinor, BOOL extendedSupport, NSString *message, NSInteger errorCode) {
  153. if (errorCode == 0) {
  154. [self.activity stopAnimating];
  155. self.login.enabled = YES;
  156. // Login Flow
  157. if (_user.hidden && _password.hidden && versionMajor >= k_flow_version_available) {
  158. appDelegate.activeLoginWeb = [CCLoginWeb new];
  159. appDelegate.activeLoginWeb.loginType = _loginType;
  160. appDelegate.activeLoginWeb.delegate = self;
  161. appDelegate.activeLoginWeb.urlBase = self.baseUrl.text;
  162. [appDelegate.activeLoginWeb open:self];
  163. }
  164. // NO Login Flow available
  165. if (versionMajor < k_flow_version_available) {
  166. [self.loginTypeView setHidden:YES];
  167. _imageUser.hidden = NO;
  168. _user.hidden = NO;
  169. _imagePassword.hidden = NO;
  170. _password.hidden = NO;
  171. [_user becomeFirstResponder];
  172. }
  173. } else {
  174. [self.activity stopAnimating];
  175. self.login.enabled = YES;
  176. if (errorCode == NSURLErrorServerCertificateUntrusted) {
  177. [[CCCertificate sharedManager] presentViewControllerCertificateWithAccount:nil viewController:self delegate:self];
  178. } else {
  179. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_connection_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  180. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  181. [alertController addAction:okAction];
  182. [self presentViewController:alertController animated:YES completion:nil];
  183. }
  184. }
  185. }];
  186. }
  187. - (void)trustedCerticateAccepted
  188. {
  189. NSLog(@"[LOG] Certificate trusted");
  190. }
  191. - (void)trustedCerticateDenied
  192. {
  193. if (_loginType == k_login_Modify_Password)
  194. [self handleAnnulla:self];
  195. }
  196. #pragma --------------------------------------------------------------------------------------------
  197. #pragma mark == TextField ==
  198. #pragma --------------------------------------------------------------------------------------------
  199. -(void)textFieldDidBeginEditing:(UITextField *)textField
  200. {
  201. if (textField == self.password) {
  202. self.toggleVisiblePassword.hidden = NO;
  203. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].customerText};
  204. }
  205. }
  206. -(void)textFieldDidEndEditing:(UITextField *)textField
  207. {
  208. if (textField == self.password) {
  209. self.toggleVisiblePassword.hidden = YES;
  210. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].customerText};
  211. }
  212. }
  213. #pragma --------------------------------------------------------------------------------------------
  214. #pragma mark === NCLoginQRCodeDelegate ===
  215. #pragma --------------------------------------------------------------------------------------------
  216. - (void)dismissQRCode:(NSString *)value metadataType:(NSString *)metadataType
  217. {
  218. NSString *protocolLogin = [[NCBrandOptions sharedInstance].webLoginAutenticationProtocol stringByAppendingString:@"login/"];
  219. if (value != nil && [value hasPrefix:protocolLogin] && [value containsString:@"user:"] && [value containsString:@"password:"] && [value containsString:@"server:"]) {
  220. value = [value stringByReplacingOccurrencesOfString:protocolLogin withString:@""];
  221. NSArray *valueArray = [value componentsSeparatedByString: @"&"];
  222. if (valueArray.count == 3) {
  223. _imageUser.hidden = NO;
  224. _user.hidden = NO;
  225. _imagePassword.hidden = NO;
  226. _password.hidden = NO;
  227. [self.loginTypeView setTitle:NSLocalizedString(@"_web_login_", nil) forState:UIControlStateNormal];
  228. self.user.text = [valueArray[0] stringByReplacingOccurrencesOfString:@"user:" withString:@""];
  229. self.password.text = [valueArray[1] stringByReplacingOccurrencesOfString:@"password:" withString:@""];
  230. self.baseUrl.text = [valueArray[2] stringByReplacingOccurrencesOfString:@"server:" withString:@""];
  231. // Check whether baseUrl contain protocol. If not add https:// by default.
  232. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  233. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  234. }
  235. [self handleButtonLogin:self];
  236. }
  237. }
  238. }
  239. #pragma --------------------------------------------------------------------------------------------
  240. #pragma mark === CCLoginDelegateWeb ===
  241. #pragma --------------------------------------------------------------------------------------------
  242. - (void)loginSuccess:(NSInteger)loginType
  243. {
  244. [self.delegate loginSuccess:_loginType];
  245. }
  246. - (void)webDismiss
  247. {
  248. [self dismissViewControllerAnimated:YES completion:nil];
  249. }
  250. #pragma --------------------------------------------------------------------------------------------
  251. #pragma mark == Action ==
  252. #pragma --------------------------------------------------------------------------------------------
  253. - (IBAction)handlebaseUrlchange:(id)sender
  254. {
  255. if ([self.baseUrl.text length] > 0 && !_user.hidden && !_password.hidden)
  256. [self performSelector:@selector(testUrl) withObject:nil];
  257. }
  258. - (IBAction)handleButtonLogin:(id)sender
  259. {
  260. if ([self.baseUrl.text length] > 0 && _user.hidden && _password.hidden) {
  261. [self testUrl];
  262. return;
  263. }
  264. if ([self.baseUrl.text length] > 0 && [self.user.text length] && [self.password.text length]) {
  265. // remove last char if /
  266. if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
  267. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  268. NSString *url = self.baseUrl.text;
  269. NSString *user = self.user.text;
  270. NSString *password = self.password.text;
  271. self.login.enabled = NO;
  272. [self.activity startAnimating];
  273. [[OCNetworking sharedManager] checkServerUrl:[NSString stringWithFormat:@"%@%@", url, k_webDAV] user:user userID:user password:password completion:^(NSString *message, NSInteger errorCode) {
  274. if (errorCode == 0) {
  275. [self.activity stopAnimating];
  276. // account
  277. NSString *account = [NSString stringWithFormat:@"%@ %@", user, url];
  278. if (_loginType == k_login_Modify_Password) {
  279. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  280. // Change Password
  281. [CCUtility setPassword:account password:password];
  282. // Setting appDelegate active account
  283. [appDelegate settingActiveAccount:account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:password];
  284. [self.delegate loginSuccess:_loginType];
  285. [self dismissViewControllerAnimated:YES completion:nil];
  286. } else {
  287. // NO account found, clear
  288. if ([NCManageDatabase.sharedInstance getAccounts] == nil) {
  289. [NCUtility.sharedInstance removeAllSettings];
  290. }
  291. // STOP Intro
  292. [CCUtility setIntro:YES];
  293. [[NCManageDatabase sharedInstance] deleteAccount:account];
  294. [[NCManageDatabase sharedInstance] addAccount:account url:url user:user password:password loginFlow:false];
  295. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  296. // Setting appDelegate active account
  297. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:[CCUtility getPassword:tableAccount.account]];
  298. [self.delegate loginSuccess:_loginType];
  299. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  300. [self dismissViewControllerAnimated:YES completion:nil];
  301. });
  302. }
  303. } else {
  304. self.login.enabled = YES;
  305. [self.activity stopAnimating];
  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