CCLogin.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // CCLogin.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/04/15.
  6. // Copyright (c) 2014 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 "CCCoreData.h"
  27. @interface CCLogin ()
  28. {
  29. UIAlertView *alertView;
  30. UIView *rootView;
  31. }
  32. @end
  33. @implementation CCLogin
  34. - (void)viewDidLoad
  35. {
  36. [super viewDidLoad];
  37. self.imageBrand.image = [UIImage imageNamed:image_brandLogin];
  38. self.login.backgroundColor = COLOR_BRAND;
  39. self.bottomLabel.text = NSLocalizedString(@"_login_bottom_label_", nil);
  40. 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))) {
  41. // Portrait
  42. self.bottomLabel.hidden = NO;
  43. } else {
  44. // Landscape
  45. self.bottomLabel.hidden = YES;
  46. }
  47. #ifdef NO_REQUEST_LOGIN_URL
  48. _baseUrl.text = k_loginBaseUrl;
  49. _imageBaseUrl.hidden = YES;
  50. _baseUrl.hidden = YES;
  51. #endif
  52. [self.baseUrl setDelegate:self];
  53. [self.password setDelegate:self];
  54. [self.user setDelegate:self];
  55. [self.baseUrl setFont:[UIFont systemFontOfSize:13]];
  56. [self.user setFont:[UIFont systemFontOfSize:13]];
  57. [self.password setFont:[UIFont systemFontOfSize:13]];
  58. self.loadingBaseUrl.image = [UIImage animatedImageWithAnimatedGIFURL:[[NSBundle mainBundle] URLForResource: @"loading" withExtension:@"gif"]];
  59. self.loadingBaseUrl.hidden = YES;
  60. if (_loginType == loginAdd) {
  61. }
  62. if (_loginType == loginAddForced) {
  63. _annulla.hidden = YES;
  64. }
  65. if (_loginType == loginModifyPasswordUser) {
  66. _baseUrl.text = app.activeUrl;
  67. _baseUrl.userInteractionEnabled = NO;
  68. _baseUrl.textColor = [UIColor lightGrayColor];
  69. _user.text = app.activeUser;
  70. _user.userInteractionEnabled = NO;
  71. _user.textColor = [UIColor lightGrayColor];
  72. }
  73. [self.annulla setTitle:NSLocalizedString(@"_cancel_", nil) forState:UIControlStateNormal];
  74. [self.login setTitle:NSLocalizedString(@"_login_", nil) forState:UIControlStateNormal];
  75. [self.baseUrl setKeyboardType:UIKeyboardTypeURL];
  76. }
  77. - (void)viewWillAppear:(BOOL)animated
  78. {
  79. [super viewWillAppear:animated];
  80. // verify URL
  81. if (_loginType == loginModifyPasswordUser && [self.baseUrl.text length] > 0)
  82. [self testUrl];
  83. }
  84. // E' apparsa
  85. - (void)viewDidAppear:(BOOL)animated
  86. {
  87. [super viewDidAppear:animated];
  88. [self showIntro];
  89. }
  90. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  91. {
  92. [textField resignFirstResponder];
  93. return YES;
  94. }
  95. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  96. {
  97. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  98. 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))) {
  99. // Portrait
  100. self.bottomLabel.hidden = NO;
  101. } else {
  102. // Landscape
  103. self.bottomLabel.hidden = YES;
  104. }
  105. }];
  106. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  107. }
  108. #pragma --------------------------------------------------------------------------------------------
  109. #pragma mark ===== Intro =====
  110. #pragma --------------------------------------------------------------------------------------------
  111. - (void)showIntro
  112. {
  113. #ifdef OPTION_DISABLE_INTRO
  114. [CCUtility setIntro:@"1.0"];
  115. #endif
  116. if ([CCUtility getIntro:@"1.0"] == NO) {
  117. _intro = [[CCIntro alloc] initWithDelegate:self delegateView:self.view];
  118. [_intro showIntroCryptoCloud:0.0];
  119. }
  120. }
  121. - (void)introDidFinish:(EAIntroView *)introView wasSkipped:(BOOL)wasSkipped
  122. {
  123. [CCUtility setIntro:@"1.0"];
  124. }
  125. #pragma --------------------------------------------------------------------------------------------
  126. #pragma mark == Chech Server URL ==
  127. #pragma --------------------------------------------------------------------------------------------
  128. - (void)testUrl
  129. {
  130. self.login.enabled = NO;
  131. self.loadingBaseUrl.hidden = NO;
  132. // Check whether baseUrl contain protocol. If not add https:// by default.
  133. if(![self.baseUrl.text hasPrefix:@"https"] && ![self.baseUrl.text hasPrefix:@"http"]) {
  134. self.baseUrl.text = [NSString stringWithFormat:@"https://%@",self.baseUrl.text];
  135. }
  136. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.baseUrl.text] cachePolicy:0 timeoutInterval:20.0];
  137. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  138. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  139. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  140. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  141. dispatch_async(dispatch_get_main_queue(), ^{
  142. self.login.enabled = YES;
  143. self.loadingBaseUrl.hidden = YES;
  144. });
  145. if (error != nil) {
  146. NSLog(@"[LOG] Error: %ld - %@",(long)[error code] , [error localizedDescription]);
  147. // self signed certificate
  148. if ([error code] == NSURLErrorServerCertificateUntrusted) {
  149. NSLog(@"[LOG] Error NSURLErrorServerCertificateUntrusted");
  150. dispatch_async(dispatch_get_main_queue(), ^{
  151. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:self delegate:self];
  152. });
  153. } else {
  154. dispatch_async(dispatch_get_main_queue(), ^{
  155. alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_connection_error_",nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  156. [alertView show];
  157. });
  158. }
  159. }
  160. }];
  161. [task resume];
  162. }
  163. - (void)trustedCerticateAccepted
  164. {
  165. NSLog(@"[LOG] Certificate trusted");
  166. }
  167. - (void)trustedCerticateDenied
  168. {
  169. if (_loginType == loginModifyPasswordUser)
  170. [self handleAnnulla:self];
  171. }
  172. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  173. {
  174. // The pinnning check
  175. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  176. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  177. } else {
  178. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  179. }
  180. }
  181. #pragma --------------------------------------------------------------------------------------------
  182. #pragma mark == Login ==
  183. #pragma --------------------------------------------------------------------------------------------
  184. - (void)loginCloud
  185. {
  186. self.login.enabled = NO;
  187. self.loadingBaseUrl.hidden = NO;
  188. // remove last char if /
  189. if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
  190. self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
  191. OCnetworking *ocNet = [[OCnetworking alloc] initWithDelegate:self metadataNet:nil withUser:self.user.text withPassword:self.password.text withUrl:nil isCryptoCloudMode:NO];
  192. NSError *error = [ocNet checkServerSync:[NSString stringWithFormat:@"%@%@", self.baseUrl.text, webDAV]];
  193. if (!error) {
  194. // account
  195. NSString *account = [NSString stringWithFormat:@"%@ %@", self.user.text, self.baseUrl.text];
  196. if (_loginType == loginModifyPasswordUser) {
  197. [CCCoreData updateAccount:account withPassword:self.password.text];
  198. } else {
  199. [CCCoreData deleteAccount:account];
  200. // Add default account
  201. [CCCoreData addAccount:account url:self.baseUrl.text user:self.user.text password:self.password.text];
  202. // ---------------------------------------
  203. // *** DEFAULT OPTION ***
  204. // ---------------------------------------
  205. #ifdef OPTION_AUTOMATIC_UPLOAD_ENABLE
  206. [CCCoreData setCameraUpload:YES activeAccount:account];
  207. // Default parameter
  208. [CCCoreData setCameraUploadFolderName:nil activeAccount:account];
  209. [CCCoreData setCameraUploadFolderPath:nil activeUrl:self.baseUrl.text activeAccount:account];
  210. [CCCoreData setCameraUploadPhoto:YES activeAccount:account];
  211. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  212. [CCCoreData setCameraUploadVideo:YES activeAccount:account];
  213. [CCCoreData setCameraUploadWWanVideo:YES activeAccount:account];
  214. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  215. [CCCoreData setCameraUploadCreateSubfolderActiveAccount:YES activeAccount:account];
  216. #endif
  217. }
  218. TableAccount *tableAccount = [CCCoreData setActiveAccount:account];
  219. // verifica
  220. if ([tableAccount.account isEqualToString:account]) {
  221. [app settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activePassword:tableAccount.password];
  222. [self.delegate loginSuccess:_loginType];
  223. // close
  224. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  225. [self dismissViewControllerAnimated:YES completion:nil];
  226. });
  227. } else {
  228. if (_loginType != loginModifyPasswordUser)
  229. [CCCoreData deleteAccount:account];
  230. alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:@"Fatal error writing database" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  231. [alertView show];
  232. }
  233. } else {
  234. if ([error code] != NSURLErrorServerCertificateUntrusted) {
  235. NSString *description = [error.userInfo objectForKey:@"NSLocalizedDescription"];
  236. NSString *message = [NSString stringWithFormat:@"%@.\n%@", NSLocalizedStringFromTable(@"_not_possible_connect_to_server_", @"Error", nil), description];
  237. alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  238. [alertView show];
  239. }
  240. }
  241. self.login.enabled = YES;
  242. self.loadingBaseUrl.hidden = YES;
  243. }
  244. #pragma --------------------------------------------------------------------------------------------
  245. #pragma mark == TextField ==
  246. #pragma --------------------------------------------------------------------------------------------
  247. -(void)textFieldDidBeginEditing:(UITextField *)textField
  248. {
  249. if (textField == self.password) {
  250. self.toggleVisiblePassword.hidden = NO;
  251. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
  252. }
  253. }
  254. -(void)textFieldDidEndEditing:(UITextField *)textField
  255. {
  256. if (textField == self.password) {
  257. self.toggleVisiblePassword.hidden = YES;
  258. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
  259. }
  260. }
  261. #pragma --------------------------------------------------------------------------------------------
  262. #pragma mark == IBAction ==
  263. #pragma --------------------------------------------------------------------------------------------
  264. - (IBAction)handlebaseUrlchange:(id)sender
  265. {
  266. if ([self.baseUrl.text length] > 0)
  267. [self performSelector:@selector(testUrl) withObject:nil];
  268. }
  269. - (IBAction)handleButtonLogin:(id)sender
  270. {
  271. if ([self.baseUrl.text length] > 0 && [self.user.text length] && [self.password.text length])
  272. [self performSelector:@selector(loginCloud) withObject:nil];
  273. }
  274. - (IBAction)handleAnnulla:(id)sender
  275. {
  276. [self dismissViewControllerAnimated:YES completion:nil];
  277. }
  278. - (IBAction)handleToggleVisiblePassword:(id)sender
  279. {
  280. NSString *currentPassword = self.password.text;
  281. self.password.secureTextEntry = ! self.password.secureTextEntry;
  282. self.password.text = @"";
  283. self.password.text = currentPassword;
  284. self.password.defaultTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
  285. }
  286. @end