CCLogin.m 14 KB

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