CCManageCryptoCloud.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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:@"activatecryptocloud" 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. row.hidden = @(YES);
  48. [section addFormRow:row];
  49. // Deactivation Crypto Cloud Mode
  50. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deactivatecryptocloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_deactivation_crypto_cloud_", nil)];
  51. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  52. [row.cellConfig setObject:[UIImage imageNamed:image_settingsRemoveCryptoCloud] forKey:@"imageView.image"];
  53. row.action.formSelector = @selector(disactivateCryptoCloud:);
  54. row.hidden = @(YES);
  55. [section addFormRow:row];
  56. // Send aes-256 password via mail
  57. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"sendmailencryptpass" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_encryptpass_by_email_", nil)];
  58. [row.cellConfig setObject:@(NSTextAlignmentCenter) forKey:@"textLabel.textAlignment"];
  59. [row.cellConfig setObject:COLOR_ENCRYPTED forKey:@"textLabel.textColor"];
  60. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  61. [row.cellConfig setObject:[UIImage imageNamed:image_settingsKeyMail] forKey:@"imageView.image"];
  62. row.action.formSelector = @selector(checkEncryptPass:);
  63. row.hidden = @(YES);
  64. [section addFormRow:row];
  65. section = [XLFormSectionDescriptor formSection];
  66. [form addFormSection:section];
  67. self.form = form;
  68. }
  69. - (void)viewDidLoad
  70. {
  71. [super viewDidLoad];
  72. // Color
  73. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  74. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  75. }
  76. // Apparirà
  77. - (void)viewWillAppear:(BOOL)animated
  78. {
  79. [super viewWillAppear:animated];
  80. // Color
  81. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  82. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  83. [self reloadForm];
  84. }
  85. - (void)activateCryptoCloud:(XLFormRowDescriptor *)sender
  86. {
  87. [self deselectFormRow:sender];
  88. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  89. viewController.delegate = self;
  90. viewController.type = BKPasscodeViewControllerNewPasscodeType;
  91. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  92. viewController.passcodeInputView.maximumLength = 64;
  93. viewController.title = NSLocalizedString(@"_key_aes_256_", nil);
  94. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  95. viewController.navigationItem.leftBarButtonItem.tintColor = COLOR_ENCRYPTED;
  96. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  97. [self presentViewController:navigationController animated:YES completion:nil];
  98. }
  99. - (void)disactivateCryptoCloud:(XLFormRowDescriptor *)sender
  100. {
  101. }
  102. - (void)checkEncryptPass:(XLFormRowDescriptor *)sender
  103. {
  104. }
  105. - (void)closeSecurityOptions
  106. {
  107. 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];
  108. [alertView show];
  109. }
  110. - (void)activateSecurityOptions
  111. {
  112. CCSecurityOptions *securityOptionsVC = [[CCSecurityOptions alloc] initWithDelegate:self];
  113. UINavigationController *securityOptionsNC = [[UINavigationController alloc] initWithRootViewController:securityOptionsVC];
  114. [securityOptionsNC setModalPresentationStyle:UIModalPresentationFormSheet];
  115. [self presentViewController:securityOptionsNC animated:YES completion:nil];
  116. }
  117. #pragma --------------------------------------------------------------------------------------------
  118. #pragma mark == BKPasscodeViewController ==
  119. #pragma --------------------------------------------------------------------------------------------
  120. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  121. {
  122. switch (aViewController.type) {
  123. case BKPasscodeViewControllerNewPasscodeType:
  124. case BKPasscodeViewControllerCheckPasscodeType: {
  125. // min passcode 4 chars
  126. if ([aPasscode length] >= 4) {
  127. [CCUtility setKeyChainPasscodeForUUID:[CCUtility getUUID] conPasscode:aPasscode];
  128. // verify
  129. NSString *pwd = [CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]];
  130. if ([pwd isEqualToString:aPasscode] == NO || pwd == nil) {
  131. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:@"Fatal error writing key" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  132. [alertView show];
  133. } else {
  134. // ok !!
  135. app.isCryptoCloudMode = YES;
  136. // reload
  137. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  138. // Request : Send Passcode email
  139. [self performSelector:@selector(activateSecurityOptions) withObject:nil afterDelay:0.1];
  140. }
  141. } else {
  142. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_passcode_too_short_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  143. [alertView show];
  144. }
  145. [aViewController dismissViewControllerAnimated:YES completion:nil];
  146. }
  147. break;
  148. case BKPasscodeViewControllerChangePasscodeType:
  149. if ([aPasscode length]) {
  150. // [CCUtility WriteDatiLogin:@"" ConNomeUtente:@"" ConPassword:@"" ConPassCode:aPasscode];
  151. // [aViewController dismissViewControllerAnimated:YES completion:nil];
  152. }
  153. self.failedAttempts = 0;
  154. self.lockUntilDate = nil;
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  161. {
  162. if ([aPasscode length]) {
  163. self.lockUntilDate = nil;
  164. self.failedAttempts = 0;
  165. aResultHandler(YES);
  166. } else {
  167. aResultHandler(NO);
  168. }
  169. }
  170. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  171. {
  172. self.failedAttempts++;
  173. if (self.failedAttempts > 5) {
  174. NSTimeInterval timeInterval = 60;
  175. if (self.failedAttempts > 6) {
  176. NSUInteger multiplier = self.failedAttempts - 6;
  177. timeInterval = (5 * 60) * multiplier;
  178. if (timeInterval > 3600 * 24) {
  179. timeInterval = 3600 * 24;
  180. }
  181. }
  182. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  183. }
  184. }
  185. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  186. {
  187. return self.failedAttempts;
  188. }
  189. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  190. {
  191. return self.lockUntilDate;
  192. }
  193. - (void)passcodeViewCloseButtonPressed:(id)sender
  194. {
  195. [self dismissViewControllerAnimated:YES completion:nil];
  196. }
  197. #pragma --------------------------------------------------------------------------------------------
  198. #pragma mark === Reload Form ===
  199. #pragma --------------------------------------------------------------------------------------------
  200. - (void)reloadForm
  201. {
  202. XLFormRowDescriptor *rowActivateCryptoCloud = [self.form formRowWithTag:@"activatecryptocloud"];
  203. XLFormRowDescriptor *rowDeactivateCryptoCloud = [self.form formRowWithTag:@"deactivatecryptocloud"];
  204. XLFormRowDescriptor *rowSendMailEncryptPass = [self.form formRowWithTag:@"sendmailencryptpass"];
  205. if (app.isCryptoCloudMode) {
  206. rowActivateCryptoCloud.hidden = @(YES);
  207. rowDeactivateCryptoCloud.hidden = @(NO);
  208. rowSendMailEncryptPass.hidden = @(NO);
  209. } else {
  210. rowActivateCryptoCloud.hidden = @(NO);
  211. rowDeactivateCryptoCloud.hidden = @(YES);
  212. rowSendMailEncryptPass.hidden = @(YES);
  213. }
  214. [self.tableView reloadData];
  215. }
  216. @end