CCLogin.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // CCLogin.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 11/09/14.
  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. @implementation CCLogin
  26. - (id)initWithCoder:(NSCoder *)aDecoder
  27. {
  28. if (self = [super initWithCoder:aDecoder]) {
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginCorrect) name:@"messageLoginCorrect" object:nil];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginIncorrect) name:@"messageLoginIncorrect" object:nil];
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad
  35. {
  36. [super viewDidLoad];
  37. self.brand.image = [UIImage imageNamed:image_launchscreen];
  38. [_nextcloud setTitle:[NSString stringWithFormat:@" %@", NSLocalizedString(@"_add_your_nextcloud_", nil)] forState:UIControlStateNormal];
  39. }
  40. -(void)viewDidAppear:(BOOL)animated
  41. {
  42. [super viewDidAppear:animated];
  43. [self selectFunction];
  44. }
  45. #pragma --------------------------------------------------------------------------------------------
  46. #pragma mark == select Function ==
  47. #pragma --------------------------------------------------------------------------------------------
  48. - (void)selectFunction
  49. {
  50. // Show Intro
  51. if ([CCUtility getIntro:@"1.0"] == NO) {
  52. self.intro = [[CCIntro alloc] initWithDelegate:self delegateView:self.view];
  53. [self.intro showIntroCryptoCloud:2.0];
  54. }
  55. // Request : Passcode
  56. if ([CCUtility getIntro:@"1.0"] == YES && [[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] == 0) {
  57. [self passcodeVC];
  58. }
  59. // Request : Send Passcode email
  60. if ([CCUtility getIntro:@"1.0"] == YES && [[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] > 0 && [CCUtility getEmail] == nil && [app.activeAccount length] == 0) {
  61. CCSecurityOptions *viewController = [[CCSecurityOptions alloc] initWithDelegate:self];
  62. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  63. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  64. [self presentViewController:navigationController animated:YES completion:nil];
  65. }
  66. // OK all - Close
  67. if ([CCUtility getIntro:@"1.0"] == YES && [[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] > 0 && [app.activeAccount length] > 0) {
  68. [self loginCorrect];
  69. }
  70. }
  71. #pragma --------------------------------------------------------------------------------------------
  72. #pragma mark == IBAction ==
  73. #pragma --------------------------------------------------------------------------------------------
  74. - (IBAction)handleNextcloud:(id)sender
  75. {
  76. if ([[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] == 0) {
  77. [self passcodeVC];
  78. return;
  79. }
  80. CCLoginNCOC *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  81. [loginVC setModifyOnlyPassword:NO];
  82. [loginVC setTypeCloud:typeCloudNextcloud];
  83. [self presentViewController:loginVC animated:YES completion:NULL];
  84. }
  85. #pragma --------------------------------------------------------------------------------------------
  86. #pragma mark == BKPasscodeViewController ==
  87. #pragma --------------------------------------------------------------------------------------------
  88. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  89. {
  90. switch (aViewController.type) {
  91. case BKPasscodeViewControllerNewPasscodeType:
  92. case BKPasscodeViewControllerCheckPasscodeType: {
  93. // min passcode 4 chars
  94. if ([aPasscode length] >= 4) {
  95. [CCUtility setKeyChainPasscodeForUUID:[CCUtility getUUID] conPasscode:aPasscode];
  96. // verify
  97. NSString *pwd = [CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]];
  98. if ([pwd isEqualToString:aPasscode] == NO || pwd == nil) {
  99. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:@"Fatal error writing key" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  100. [alertView show];
  101. }
  102. } else {
  103. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_passcode_too_short_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  104. [alertView show];
  105. }
  106. [aViewController dismissViewControllerAnimated:YES completion:nil];
  107. }
  108. break;
  109. case BKPasscodeViewControllerChangePasscodeType:
  110. if ([aPasscode length]) {
  111. // [CCUtility WriteDatiLogin:@"" ConNomeUtente:@"" ConPassword:@"" ConPassCode:aPasscode];
  112. // [aViewController dismissViewControllerAnimated:YES completion:nil];
  113. }
  114. self.failedAttempts = 0;
  115. self.lockUntilDate = nil;
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  122. {
  123. if ([aPasscode length]) {
  124. self.lockUntilDate = nil;
  125. self.failedAttempts = 0;
  126. aResultHandler(YES);
  127. } else {
  128. aResultHandler(NO);
  129. }
  130. }
  131. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  132. {
  133. self.failedAttempts++;
  134. if (self.failedAttempts > 5) {
  135. NSTimeInterval timeInterval = 60;
  136. if (self.failedAttempts > 6) {
  137. NSUInteger multiplier = self.failedAttempts - 6;
  138. timeInterval = (5 * 60) * multiplier;
  139. if (timeInterval > 3600 * 24) {
  140. timeInterval = 3600 * 24;
  141. }
  142. }
  143. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  144. }
  145. }
  146. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  147. {
  148. return self.failedAttempts;
  149. }
  150. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  151. {
  152. return self.lockUntilDate;
  153. }
  154. - (void)passcodeViewCloseButtonPressed:(id)sender
  155. {
  156. [self dismissViewControllerAnimated:YES completion:nil];
  157. }
  158. #pragma --------------------------------------------------------------------------------------------
  159. #pragma mark == Intro Delegate ==
  160. #pragma --------------------------------------------------------------------------------------------
  161. - (void)introDidFinish:(EAIntroView *)introView wasSkipped:(BOOL)wasSkipped
  162. {
  163. [CCUtility setIntro:@"1.0"];
  164. [self selectFunction];
  165. }
  166. #pragma --------------------------------------------------------------------------------------------
  167. #pragma mark == navigation ==
  168. #pragma --------------------------------------------------------------------------------------------
  169. - (void)passcodeVC
  170. {
  171. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  172. viewController.delegate = self;
  173. viewController.type = BKPasscodeViewControllerNewPasscodeType;
  174. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  175. viewController.passcodeInputView.maximumLength = 64;
  176. viewController.title = NSLocalizedString(@"_key_aes_256_", nil);
  177. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  178. [self presentViewController:navigationController animated:YES completion:nil];
  179. }
  180. - (void)loginCorrect
  181. {
  182. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  183. // close
  184. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  185. [self dismissViewControllerAnimated:YES completion:nil];
  186. });
  187. }
  188. - (void)loginIncorrect
  189. {
  190. NSLog(@"[LOG] Incorrect login");
  191. }
  192. @end