CCLogin.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. _owncloud.hidden = YES;
  39. _dropbox.hidden = YES;
  40. [_nextcloud setTitle:[NSString stringWithFormat:@" %@", NSLocalizedString(@"_add_your_nextcloud_", nil)] forState:UIControlStateNormal];
  41. [_owncloud setTitle:[NSString stringWithFormat:@" %@", NSLocalizedString(@"_add_your_owncloud_", nil)] forState:UIControlStateNormal];
  42. [_dropbox setTitle:[NSString stringWithFormat:@" %@", NSLocalizedString(@"_add_your_dropbox_", nil)] forState:UIControlStateNormal];
  43. }
  44. -(void)viewDidAppear:(BOOL)animated
  45. {
  46. [super viewDidAppear:animated];
  47. [self selectFunction];
  48. }
  49. #pragma --------------------------------------------------------------------------------------------
  50. #pragma mark == select Function ==
  51. #pragma --------------------------------------------------------------------------------------------
  52. - (void)selectFunction
  53. {
  54. // Show Intro
  55. if ([CCUtility getIntro:@"1.0"] == NO) {
  56. self.intro = [[CCIntro alloc] initWithDelegate:self delegateView:self.view];
  57. [self.intro showIntroCryptoCloud:2.0];
  58. }
  59. // Request : Passcode
  60. if ([CCUtility getIntro:@"1.0"] == YES && [[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] == 0) {
  61. [self passcodeVC];
  62. }
  63. // Request : Send Passcode email
  64. if ([CCUtility getIntro:@"1.0"] == YES && [[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] > 0 && [CCUtility getEmail] == nil && [app.activeAccount length] == 0) {
  65. CCSecurityOptions *viewController = [[CCSecurityOptions alloc] initWithDelegate:self];
  66. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  67. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  68. [self presentViewController:navigationController animated:YES completion:nil];
  69. }
  70. // OK all - Close
  71. if ([CCUtility getIntro:@"1.0"] == YES && [[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] > 0 && [app.activeAccount length] > 0) {
  72. [self loginCorrect];
  73. }
  74. }
  75. #pragma --------------------------------------------------------------------------------------------
  76. #pragma mark == IBAction ==
  77. #pragma --------------------------------------------------------------------------------------------
  78. - (IBAction)handleNextcloud:(id)sender
  79. {
  80. if ([[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] == 0) {
  81. [self passcodeVC];
  82. return;
  83. }
  84. self.owncloud.enabled = NO;
  85. self.dropbox.enabled = NO;
  86. CCLoginNCOC *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  87. [loginVC setModifyOnlyPassword:NO];
  88. [loginVC setTypeCloud:typeCloudNextcloud];
  89. [self presentViewController:loginVC animated:YES completion:NULL];
  90. }
  91. - (IBAction)handleOwnCloud:(id)sender
  92. {
  93. if ([[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] == 0) {
  94. [self passcodeVC];
  95. return;
  96. }
  97. self.owncloud.enabled = NO;
  98. self.dropbox.enabled = NO;
  99. CCLoginNCOC *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginOwnCloud"];
  100. [loginVC setModifyOnlyPassword:NO];
  101. [loginVC setTypeCloud:typeCloudOwnCloud];
  102. [self presentViewController:loginVC animated:YES completion:NULL];
  103. }
  104. #ifdef CC
  105. - (IBAction)handleDropBox:(id)sender
  106. {
  107. if ([[CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]] length] == 0) {
  108. [self passcodeVC];
  109. return;
  110. }
  111. self.owncloud.enabled = NO;
  112. self.dropbox.enabled = NO;
  113. [[DBSession sharedSession] linkFromController:self];
  114. }
  115. #endif
  116. #pragma --------------------------------------------------------------------------------------------
  117. #pragma mark == BKPasscodeViewController ==
  118. #pragma --------------------------------------------------------------------------------------------
  119. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  120. {
  121. switch (aViewController.type) {
  122. case BKPasscodeViewControllerNewPasscodeType:
  123. case BKPasscodeViewControllerCheckPasscodeType: {
  124. // min passcode 4 chars
  125. if ([aPasscode length] >= 4) {
  126. [CCUtility setKeyChainPasscodeForUUID:[CCUtility getUUID] conPasscode:aPasscode];
  127. // verify
  128. NSString *pwd = [CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]];
  129. if ([pwd isEqualToString:aPasscode] == NO || pwd == nil) {
  130. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:@"Fatal error writing key" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  131. [alertView show];
  132. }
  133. } else {
  134. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_passcode_too_short_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  135. [alertView show];
  136. }
  137. [aViewController dismissViewControllerAnimated:YES completion:nil];
  138. }
  139. break;
  140. case BKPasscodeViewControllerChangePasscodeType:
  141. if ([aPasscode length]) {
  142. // [CCUtility WriteDatiLogin:@"" ConNomeUtente:@"" ConPassword:@"" ConPassCode:aPasscode];
  143. // [aViewController dismissViewControllerAnimated:YES completion:nil];
  144. }
  145. self.failedAttempts = 0;
  146. self.lockUntilDate = nil;
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  153. {
  154. if ([aPasscode length]) {
  155. self.lockUntilDate = nil;
  156. self.failedAttempts = 0;
  157. aResultHandler(YES);
  158. } else {
  159. aResultHandler(NO);
  160. }
  161. }
  162. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  163. {
  164. self.failedAttempts++;
  165. if (self.failedAttempts > 5) {
  166. NSTimeInterval timeInterval = 60;
  167. if (self.failedAttempts > 6) {
  168. NSUInteger multiplier = self.failedAttempts - 6;
  169. timeInterval = (5 * 60) * multiplier;
  170. if (timeInterval > 3600 * 24) {
  171. timeInterval = 3600 * 24;
  172. }
  173. }
  174. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  175. }
  176. }
  177. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  178. {
  179. return self.failedAttempts;
  180. }
  181. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  182. {
  183. return self.lockUntilDate;
  184. }
  185. - (void)passcodeViewCloseButtonPressed:(id)sender
  186. {
  187. [self dismissViewControllerAnimated:YES completion:nil];
  188. }
  189. #pragma --------------------------------------------------------------------------------------------
  190. #pragma mark == Intro Delegate ==
  191. #pragma --------------------------------------------------------------------------------------------
  192. - (void)introDidFinish:(EAIntroView *)introView wasSkipped:(BOOL)wasSkipped
  193. {
  194. [CCUtility setIntro:@"1.0"];
  195. [self selectFunction];
  196. }
  197. #pragma --------------------------------------------------------------------------------------------
  198. #pragma mark == navigation ==
  199. #pragma --------------------------------------------------------------------------------------------
  200. - (void)passcodeVC
  201. {
  202. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  203. viewController.delegate = self;
  204. viewController.type = BKPasscodeViewControllerNewPasscodeType;
  205. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  206. viewController.passcodeInputView.maximumLength = 64;
  207. viewController.title = NSLocalizedString(@"_key_aes_256_", nil);
  208. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  209. [self presentViewController:navigationController animated:YES completion:nil];
  210. }
  211. - (void)loginCorrect
  212. {
  213. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  214. // close
  215. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  216. [self dismissViewControllerAnimated:YES completion:nil];
  217. });
  218. }
  219. - (void)loginIncorrect
  220. {
  221. NSLog(@"[LOG] Incorrect login");
  222. self.owncloud.enabled = YES;
  223. self.dropbox.enabled = YES;
  224. }
  225. @end