CCLogin.m 13 KB

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