CCLogin.m 11 KB

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