CCLogin.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // CCLogin.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 09/04/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 "NCNetworkingSync.h"
  28. @interface CCLogin ()
  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. self.imageBrand.image = [UIImage imageNamed:@"loginLogo"];
  40. self.login.backgroundColor = [NCBrandColor sharedInstance].customer;
  41. // Bottom label
  42. self.bottomLabel.text = NSLocalizedString([NCBrandOptions sharedInstance].textLoginProvider, nil);
  43. self.bottomLabel.userInteractionEnabled = YES;
  44. if ([NCBrandOptions sharedInstance].disable_linkLoginProvider) {
  45. self.bottomLabel.hidden = YES;
  46. } else {
  47. if (self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width*([[UIScreen mainScreen] bounds].size.width<[[UIScreen mainScreen] bounds].size.height))+([[UIScreen mainScreen] bounds].size.height*([[UIScreen mainScreen] bounds].size.width>[[UIScreen mainScreen] bounds].size.height))) {
  48. // Portrait
  49. self.bottomLabel.hidden = NO;
  50. } else {
  51. // Landscape
  52. self.bottomLabel.hidden = YES;
  53. }
  54. }
  55. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabBottomLabel)];
  56. [self.bottomLabel addGestureRecognizer:tapGesture];
  57. self.annulla.tintColor = [NCBrandColor sharedInstance].customer;
  58. [self.baseUrl setDelegate:self];
  59. [self.password setDelegate:self];
  60. [self.user setDelegate:self];
  61. [self.baseUrl setFont:[UIFont systemFontOfSize:13]];
  62. [self.user setFont:[UIFont systemFontOfSize:13]];
  63. [self.password setFont:[UIFont systemFontOfSize:13]];
  64. self.imageBaseUrl.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginURL"] color:[NCBrandColor sharedInstance].customer];
  65. self.imageUser.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginUser"] color:[NCBrandColor sharedInstance].customer];
  66. self.imagePassword.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loginPassword"] color:[NCBrandColor sharedInstance].customer];
  67. self.loadingBaseUrl.image = [UIImage animatedImageWithAnimatedGIFURL:[[NSBundle mainBundle] URLForResource: @"loading" withExtension:@"gif"]];
  68. self.loadingBaseUrl.hidden = YES;
  69. // Brand
  70. if ([NCBrandOptions sharedInstance].disable_request_login_url) {
  71. _baseUrl.text = [NCBrandOptions sharedInstance].loginBaseUrl;
  72. _imageBaseUrl.hidden = YES;
  73. _baseUrl.hidden = YES;
  74. }
  75. if (_loginType == loginAdd) {
  76. // Login Flow ?
  77. if ([NCBrandOptions sharedInstance].use_login_web_flow) {
  78. _imageUser.hidden = YES;
  79. _user.hidden = YES;
  80. _imagePassword.hidden = YES;
  81. _password.hidden = YES;
  82. }
  83. }
  84. if (_loginType == loginAddForced) {
  85. _annulla.hidden = YES;
  86. }
  87. if (_loginType == loginModifyPasswordUser) {
  88. _baseUrl.text = appDelegate.activeUrl;
  89. _baseUrl.userInteractionEnabled = NO;
  90. _baseUrl.textColor = [UIColor lightGrayColor];
  91. _user.text = appDelegate.activeUser;
  92. _user.userInteractionEnabled = NO;
  93. _user.textColor = [UIColor lightGrayColor];
  94. }
  95. self.baseUrl.placeholder = NSLocalizedString(@"_login_url_", nil);
  96. self.user.placeholder = NSLocalizedString(@"_username_", nil);
  97. self.password.placeholder = NSLocalizedString(@"_password_", nil);
  98. [self.annulla setTitle:NSLocalizedString(@"_cancel_", nil) forState:UIControlStateNormal];
  99. [self.login setTitle:NSLocalizedString(@"_login_", nil) forState:UIControlStateNormal];
  100. }
  101. - (void)viewWillAppear:(BOOL)animated
  102. {
  103. [super viewWillAppear:animated];
  104. // verify URL
  105. if (_loginType == loginModifyPasswordUser && [self.baseUrl.text length] > 0)
  106. [self testUrl];
  107. }
  108. // E' apparsa
  109. - (void)viewDidAppear:(BOOL)animated
  110. {
  111. [super viewDidAppear:animated];
  112. }
  113. //
  114. - (void)viewDidDisappear:(BOOL)animated
  115. {
  116. [super viewDidDisappear:animated];
  117. if ([self.delegate respondsToSelector:@selector(loginDisappear)])
  118. [self.delegate loginDisappear];
  119. }
  120. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  121. {
  122. [textField resignFirstResponder];
  123. return YES;
  124. }
  125. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  126. {
  127. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  128. if (![NCBrandOptions sharedInstance].disable_linkLoginProvider) {
  129. if (self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width*([[UIScreen mainScreen] bounds].size.width<[[UIScreen mainScreen] bounds].size.height))+([[UIScreen mainScreen] bounds].size.height*([[UIScreen mainScreen] bounds].size.width>[[UIScreen mainScreen] bounds].size.height))) {
  130. // Portrait
  131. self.bottomLabel.hidden = NO;
  132. } else {
  133. // Landscape
  134. self.bottomLabel.hidden = YES;
  135. }
  136. }
  137. }];
  138. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  139. }
  140. #pragma --------------------------------------------------------------------------------------------
  141. #pragma mark == Chech Server URL ==
  142. #pragma --------------------------------------------------------------------------------------------
  143. - (void)testUrl
  144. {
  145. self.login.enabled = NO;
  146. self.loadingBaseUrl.hidden = NO;
  147. // Check whether baseUrl contain protocol. If not add https:// by default.
  148. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  149. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  150. }
  151. // Test Login Flow
  152. if ([self.baseUrl.text length] > 0 && _user.hidden == YES && _password.hidden == YES) {
  153. NSString *url = self.baseUrl.text;
  154. if ([url hasSuffix:@"/"]) url = [url substringToIndex:[url length] - 1];
  155. url = [url stringByAppendingString:flowEndpoint];
  156. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:0 timeoutInterval:20.0];
  157. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  158. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  159. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  160. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  161. dispatch_async(dispatch_get_main_queue(), ^{
  162. self.login.enabled = YES;
  163. self.loadingBaseUrl.hidden = YES;
  164. if (error) {
  165. NSLog(@"[LOG] Error: %ld - %@",(long)[error code] , [error localizedDescription]);
  166. } else {
  167. }
  168. });
  169. }];
  170. [task resume];
  171. } else {
  172. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.baseUrl.text] cachePolicy:0 timeoutInterval:20.0];
  173. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  174. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  175. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  176. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  177. dispatch_async(dispatch_get_main_queue(), ^{
  178. self.login.enabled = YES;
  179. self.loadingBaseUrl.hidden = YES;
  180. if (error != nil) {
  181. NSLog(@"[LOG] Error: %ld - %@",(long)[error code] , [error localizedDescription]);
  182. // self signed certificate
  183. if ([error code] == NSURLErrorServerCertificateUntrusted) {
  184. NSLog(@"[LOG] Error NSURLErrorServerCertificateUntrusted");
  185. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:self delegate:self];
  186. } else {
  187. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_connection_error_", nil) message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
  188. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  189. [alertController addAction:okAction];
  190. [self presentViewController:alertController animated:YES completion:nil];
  191. }
  192. }
  193. });
  194. }];
  195. [task resume];
  196. }
  197. }
  198. - (void)trustedCerticateAccepted
  199. {
  200. NSLog(@"[LOG] Certificate trusted");
  201. }
  202. - (void)trustedCerticateDenied
  203. {
  204. if (_loginType == loginModifyPasswordUser)
  205. [self handleAnnulla:self];
  206. }
  207. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  208. {
  209. // The pinnning check
  210. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  211. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  212. } else {
  213. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  214. }
  215. }
  216. #pragma --------------------------------------------------------------------------------------------
  217. #pragma mark == Login ==
  218. #pragma --------------------------------------------------------------------------------------------
  219. - (void)loginCloudUrl:(NSString *)url user:(NSString *)user password:(NSString *)password
  220. {
  221. NSError *error = [[NCNetworkingSync sharedManager] checkServer:[NSString stringWithFormat:@"%@%@", url, webDAV] user:user userID:user password:password];
  222. dispatch_async(dispatch_get_main_queue(), ^{
  223. self.login.enabled = NO;
  224. self.loadingBaseUrl.hidden = NO;
  225. if (!error) {
  226. // account
  227. NSString *account = [NSString stringWithFormat:@"%@ %@", user, url];
  228. if (_loginType == loginModifyPasswordUser) {
  229. // Change Password
  230. tableAccount *tbAccount = [[NCManageDatabase sharedInstance] setAccountPassword:account password:password];
  231. // Setting appDelegate active account
  232. [appDelegate settingActiveAccount:tbAccount.account activeUrl:tbAccount.url activeUser:tbAccount.user activeUserID:tbAccount.userID activePassword:tbAccount.password];
  233. // Dismiss
  234. if ([self.delegate respondsToSelector:@selector(loginSuccess:)])
  235. [self.delegate loginSuccess:_loginType];
  236. [self dismissViewControllerAnimated:YES completion:nil];
  237. } else {
  238. [[NCManageDatabase sharedInstance] deleteAccount:account];
  239. [[NCManageDatabase sharedInstance] addAccount:account url:url user:user password:password];
  240. // Read User Profile
  241. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:account];
  242. metadataNet.action = actionGetUserProfile;
  243. [appDelegate.netQueue addOperation:[[OCnetworking alloc] initWithDelegate:self metadataNet:metadataNet withUser:user withUserID:user withPassword:password withUrl:url]];
  244. }
  245. } else {
  246. if ([error code] != NSURLErrorServerCertificateUntrusted) {
  247. NSString *description = [error.userInfo objectForKey:@"NSLocalizedDescription"];
  248. NSString *message = [NSString stringWithFormat:@"%@.\n%@", NSLocalizedStringFromTable(@"_not_possible_connect_to_server_", @"Error", nil), description];
  249. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  250. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  251. [alertController addAction:okAction];
  252. [self presentViewController:alertController animated:YES completion:nil];
  253. }
  254. }
  255. self.login.enabled = YES;
  256. self.loadingBaseUrl.hidden = YES;
  257. });
  258. }
  259. #pragma --------------------------------------------------------------------------------------------
  260. #pragma mark ==== User Profile ====
  261. #pragma --------------------------------------------------------------------------------------------
  262. - (void)getUserProfileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  263. {
  264. [[NCManageDatabase sharedInstance] deleteAccount:metadataNet.account];
  265. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  266. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  267. [alertController addAction:okAction];
  268. [self presentViewController:alertController animated:YES completion:nil];
  269. }
  270. - (void)getUserProfileSuccess:(CCMetadataNet *)metadataNet userProfile:(OCUserProfile *)userProfile
  271. {
  272. // Verify if the account already exists
  273. if (userProfile.id.length > 0 && self.baseUrl.text.length > 0 && self.user.text.length > 0) {
  274. tableAccount *accountAlreadyExists = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"url = %@ AND user = %@ AND userID != %@", self.baseUrl.text, userProfile.id, self.user.text]];
  275. if (accountAlreadyExists) {
  276. [[NCManageDatabase sharedInstance] deleteAccount:metadataNet.account];
  277. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:[NSString stringWithFormat:NSLocalizedString(@"_account_already_exists_", nil), userProfile.id] preferredStyle:UIAlertControllerStyleAlert];
  278. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  279. [alertController addAction:okAction];
  280. [self presentViewController:alertController animated:YES completion:nil];
  281. return;
  282. }
  283. }
  284. // Verify if account is a valid account
  285. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account = %@", metadataNet.account]];
  286. if (account) {
  287. // Update User (+ userProfile.id)
  288. [[NCManageDatabase sharedInstance] setAccountsUserProfile:userProfile];
  289. // Set this account as default
  290. tableAccount *account = [[NCManageDatabase sharedInstance] setAccountActive:metadataNet.account];
  291. if (account) {
  292. // Setting appDelegate active account
  293. [appDelegate settingActiveAccount:account.account activeUrl:account.url activeUser:account.user activeUserID:account.userID activePassword:account.password];
  294. // Ok ! Dismiss
  295. if ([self.delegate respondsToSelector:@selector(loginSuccess:)])
  296. [self.delegate loginSuccess:_loginType];
  297. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  298. [self dismissViewControllerAnimated:YES completion:nil];
  299. });
  300. }
  301. }
  302. }
  303. #pragma --------------------------------------------------------------------------------------------
  304. #pragma mark == TextField ==
  305. #pragma --------------------------------------------------------------------------------------------
  306. -(void)textFieldDidBeginEditing:(UITextField *)textField
  307. {
  308. if (textField == self.password) {
  309. self.toggleVisiblePassword.hidden = NO;
  310. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
  311. }
  312. }
  313. -(void)textFieldDidEndEditing:(UITextField *)textField
  314. {
  315. if (textField == self.password) {
  316. self.toggleVisiblePassword.hidden = YES;
  317. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
  318. }
  319. }
  320. #pragma --------------------------------------------------------------------------------------------
  321. #pragma mark == Action ==
  322. #pragma --------------------------------------------------------------------------------------------
  323. - (void)tabBottomLabel
  324. {
  325. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NCBrandOptions sharedInstance].linkLoginProvider]];
  326. }
  327. - (IBAction)handlebaseUrlchange:(id)sender
  328. {
  329. if ([self.baseUrl.text length] > 0)
  330. [self performSelector:@selector(testUrl) withObject:nil];
  331. }
  332. - (IBAction)handleButtonLogin:(id)sender
  333. {
  334. if ([self.baseUrl.text length] > 0 && _user.hidden == YES && _password.hidden == YES) {
  335. [self testUrl];
  336. return;
  337. }
  338. if ([self.baseUrl.text length] > 0 && [self.user.text length] && [self.password.text length]) {
  339. // remove last char if /
  340. if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
  341. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  342. NSString *url = self.baseUrl.text;
  343. NSString *user = self.user.text;
  344. NSString *password = self.password.text;
  345. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  346. [self loginCloudUrl:url user:user password:password];
  347. });
  348. }
  349. }
  350. - (IBAction)handleAnnulla:(id)sender
  351. {
  352. [self dismissViewControllerAnimated:YES completion:nil];
  353. }
  354. - (IBAction)handleToggleVisiblePassword:(id)sender
  355. {
  356. NSString *currentPassword = self.password.text;
  357. self.password.secureTextEntry = ! self.password.secureTextEntry;
  358. self.password.text = @"";
  359. self.password.text = currentPassword;
  360. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
  361. }
  362. @end