CCLogin.m 18 KB

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