CCManageCryptoCloud.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // CCManageCryptoCloud.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/02/17.
  6. // Copyright (c) 2017 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 "CCManageCryptoCloud.h"
  24. #import "AppDelegate.h"
  25. @implementation CCManageCryptoCloud
  26. - (id)initWithCoder:(NSCoder *)aDecoder
  27. {
  28. self = [super initWithCoder:aDecoder];
  29. if (self) {
  30. [self initializeForm];
  31. }
  32. return self;
  33. }
  34. - (void)initializeForm
  35. {
  36. XLFormDescriptor *form ;
  37. XLFormSectionDescriptor *section;
  38. XLFormRowDescriptor *row;
  39. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"Crypto Cloud", nil)];
  40. section = [XLFormSectionDescriptor formSection];
  41. [form addFormSection:section];
  42. // Activation Crypto Cloud Mode
  43. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"activate_cryptocloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_activation_crypto_cloud_", nil)];
  44. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  45. [row.cellConfig setObject:[UIImage imageNamed:image_settingsCryptoCloud] forKey:@"imageView.image"];
  46. row.action.formSelector = @selector(activateCryptoCloud:);
  47. [section addFormRow:row];
  48. // Send aes-256 password via mail
  49. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"sendmailencryptpass" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_encryptpass_by_email_", nil)];
  50. [row.cellConfig setObject:@(NSTextAlignmentCenter) forKey:@"textLabel.textAlignment"];
  51. [row.cellConfig setObject:COLOR_ENCRYPTED forKey:@"textLabel.textColor"];
  52. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  53. [row.cellConfig setObject:[UIImage imageNamed:image_settingsKeyMail] forKey:@"imageView.image"];
  54. row.action.formSelector = @selector(checkEncryptPass:);
  55. [section addFormRow:row];
  56. section = [XLFormSectionDescriptor formSection];
  57. [form addFormSection:section];
  58. self.form = form;
  59. }
  60. - (void)viewDidLoad
  61. {
  62. [super viewDidLoad];
  63. // Color
  64. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  65. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  66. }
  67. // Apparirà
  68. - (void)viewWillAppear:(BOOL)animated
  69. {
  70. [super viewWillAppear:animated];
  71. // Color
  72. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  73. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  74. }
  75. - (void)activateCryptoCloud:(XLFormRowDescriptor *)sender
  76. {
  77. [self deselectFormRow:sender];
  78. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  79. viewController.delegate = self;
  80. viewController.type = BKPasscodeViewControllerNewPasscodeType;
  81. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  82. viewController.passcodeInputView.maximumLength = 64;
  83. viewController.title = NSLocalizedString(@"_key_aes_256_", nil);
  84. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  85. viewController.navigationItem.leftBarButtonItem.tintColor = COLOR_ENCRYPTED;
  86. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  87. [self presentViewController:navigationController animated:YES completion:nil];
  88. }
  89. - (void)checkEncryptPass:(XLFormRowDescriptor *)sender
  90. {
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark == BKPasscodeViewController ==
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  96. {
  97. switch (aViewController.type) {
  98. case BKPasscodeViewControllerNewPasscodeType:
  99. case BKPasscodeViewControllerCheckPasscodeType: {
  100. // min passcode 4 chars
  101. if ([aPasscode length] >= 4) {
  102. [CCUtility setKeyChainPasscodeForUUID:[CCUtility getUUID] conPasscode:aPasscode];
  103. // verify
  104. NSString *pwd = [CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]];
  105. if ([pwd isEqualToString:aPasscode] == NO || pwd == nil) {
  106. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:@"Fatal error writing key" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  107. [alertView show];
  108. } else {
  109. // ok !!
  110. app.isCryptoCloudMode = YES;
  111. // reload
  112. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  113. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_OK_", nil) message:@"Attivazione avvenuta correttamente, ora potrai usufruire di tutte le funzionalità aggiuntive. Ti ricordiamo che i file cifrati possono essere decifrati sono sui dispositivi iOS" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  114. [alertView show];
  115. }
  116. } else {
  117. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_passcode_too_short_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  118. [alertView show];
  119. }
  120. [aViewController dismissViewControllerAnimated:YES completion:nil];
  121. }
  122. break;
  123. case BKPasscodeViewControllerChangePasscodeType:
  124. if ([aPasscode length]) {
  125. // [CCUtility WriteDatiLogin:@"" ConNomeUtente:@"" ConPassword:@"" ConPassCode:aPasscode];
  126. // [aViewController dismissViewControllerAnimated:YES completion:nil];
  127. }
  128. self.failedAttempts = 0;
  129. self.lockUntilDate = nil;
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  136. {
  137. if ([aPasscode length]) {
  138. self.lockUntilDate = nil;
  139. self.failedAttempts = 0;
  140. aResultHandler(YES);
  141. } else {
  142. aResultHandler(NO);
  143. }
  144. }
  145. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  146. {
  147. self.failedAttempts++;
  148. if (self.failedAttempts > 5) {
  149. NSTimeInterval timeInterval = 60;
  150. if (self.failedAttempts > 6) {
  151. NSUInteger multiplier = self.failedAttempts - 6;
  152. timeInterval = (5 * 60) * multiplier;
  153. if (timeInterval > 3600 * 24) {
  154. timeInterval = 3600 * 24;
  155. }
  156. }
  157. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  158. }
  159. }
  160. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  161. {
  162. return self.failedAttempts;
  163. }
  164. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  165. {
  166. return self.lockUntilDate;
  167. }
  168. - (void)passcodeViewCloseButtonPressed:(id)sender
  169. {
  170. [self dismissViewControllerAnimated:YES completion:nil];
  171. }
  172. @end