CCLogin.m 11 KB

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