RestoreIdentityViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "RestoreIdentityViewController.h"
  21. #import "UIDefines.h"
  22. #import "MBProgressHUD.h"
  23. #import "MyIdentityStore.h"
  24. #import "NSData+Base32.h"
  25. #import "ScanBackupController.h"
  26. #import "AppDelegate.h"
  27. #import "IdentityBackupStore.h"
  28. #import "ServerAPIConnector.h"
  29. #import "UIImage+ColoredImage.h"
  30. #import "RectUtil.h"
  31. #import "Utils.h"
  32. #import "IntroQuestionView.h"
  33. #import "NibUtil.h"
  34. @interface RestoreIdentityViewController () <IntroQuestionDelegate>
  35. @end
  36. @implementation RestoreIdentityViewController
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. [self setup];
  41. }
  42. - (void)setup {
  43. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedScan:)];
  44. [_scanView addGestureRecognizer:tapGesture];
  45. _scanView.userInteractionEnabled = YES;
  46. _scanView.isAccessibilityElement = YES;
  47. [_scanView setAccessibilityHint: [BundleUtil localizedStringForKey:@"scan_id_backup"]];
  48. _scanLabel.text = [BundleUtil localizedStringForKey:@"scan_id_backup"];
  49. _scanLabel.textColor = [Colors mainThemeDark];
  50. self.view.backgroundColor = [UIColor clearColor];
  51. _backupTextView.delegate = self;
  52. _passwordTextField.delegate = self;
  53. _textViewBackground.layer.cornerRadius = 3;
  54. _passwordView.layer.cornerRadius = 3;
  55. _passwordView.layer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1].CGColor;
  56. _passwordView.layer.borderWidth = 0.5;
  57. _passwordFieldBackground.layer.cornerRadius = 3;
  58. _scanView.layer.cornerRadius = 3;
  59. _scanView.layer.borderWidth = 1;
  60. _scanView.layer.borderColor = [Colors mainThemeDark].CGColor;
  61. _scanView.isAccessibilityElement = YES;
  62. _scanView.accessibilityTraits = UIAccessibilityTraitButton;
  63. _doneButton.layer.cornerRadius = 3;
  64. _cancelButton.layer.borderWidth = 1;
  65. _cancelButton.layer.borderColor = [Colors mainThemeDark].CGColor;
  66. _cancelButton.layer.cornerRadius = 3;
  67. [_doneButton setTitle:[BundleUtil localizedStringForKey:@"Done"] forState:UIControlStateNormal];
  68. [_cancelButton setTitle:[BundleUtil localizedStringForKey:@"Cancel"] forState:UIControlStateNormal];
  69. _doneButton.backgroundColor = [Colors mainThemeDark];
  70. [_doneButton setTitleColor:[Colors white] forState:UIControlStateNormal];
  71. [_cancelButton setTitleColor:[Colors mainThemeDark] forState:UIControlStateNormal];
  72. NSString *placeholder = [BundleUtil localizedStringForKey:@"Password"];
  73. _passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: THREEMA_COLOR_PLACEHOLDER}];
  74. _backupLabel.verticalTextAlignment = SSLabelVerticalTextAlignmentTop;
  75. _backupLabel.text = [BundleUtil localizedStringForKey:@"id_backup_placeholder"];
  76. _backupLabel.numberOfLines = 0;
  77. _titleLabel.text = [BundleUtil localizedStringForKey:@"restore_id_export"];
  78. _titleLabel.accessibilityIdentifier = @"restore_id_export";
  79. _scanImageView.image = [UIImage imageNamed:@"QRScan" inColor:[Colors mainThemeDark]];
  80. _backupTextView.accessibilityIdentifier = @"backupTextView";
  81. _backupTextView.tintColor = [Colors mainThemeDark];
  82. _passwordTextField.tintColor = [Colors mainThemeDark];
  83. _keyImageView.image = [UIImage imageNamed:@"Key" inColor:[UIColor whiteColor]];
  84. UITapGestureRecognizer *mainTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedMainView:)];
  85. [self.mainContentView addGestureRecognizer:mainTapGesture];
  86. UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
  87. [self.view addGestureRecognizer:swipeGesture];
  88. }
  89. - (void)viewWillAppear:(BOOL)animated {
  90. [super viewWillAppear:animated];
  91. [self refreshView];
  92. }
  93. - (void)viewDidAppear:(BOOL)animated {
  94. [super viewDidAppear:animated];
  95. [self updateTextViewWithBackupCode];
  96. }
  97. - (void)viewWillDisappear:(BOOL)animated {
  98. [super viewWillDisappear:animated];
  99. [self hideKeyboard];
  100. }
  101. - (BOOL)shouldAutorotate {
  102. return YES;
  103. }
  104. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  105. if (SYSTEM_IS_IPAD) {
  106. return UIInterfaceOrientationMaskAll;
  107. }
  108. return UIInterfaceOrientationMaskAllButUpsideDown;
  109. }
  110. - (void)textViewDidBeginEditing:(UITextView *)textView {
  111. [self refreshView];
  112. }
  113. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  114. if ([text isEqualToString:@"\n"]) {
  115. [_passwordTextField becomeFirstResponder];
  116. return NO;
  117. }
  118. NSCharacterSet *allowedCharacters = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"];
  119. if (range.length == 0 && text.length > 0 && ![allowedCharacters characterIsMember:[text characterAtIndex:0]]) {
  120. return NO;
  121. }
  122. return YES;
  123. }
  124. - (void)textViewDidChange:(UITextView *)textView {
  125. //format text into (XXXX-XXXX-XXXX...)
  126. NSString *hyphen = @"-";
  127. NSString *rawText = [textView.text stringByReplacingOccurrencesOfString:hyphen withString:@""];
  128. if (rawText.length >= 4) {
  129. NSString *newText = @"";
  130. NSUInteger index = 0;
  131. while (rawText.length > index) {
  132. NSRange range = NSMakeRange(index, (rawText.length - index) > 4 ? 4 : rawText.length - index);
  133. NSString *n = [rawText substringWithRange:range];
  134. newText = [newText stringByAppendingString:n];
  135. if (range.length == 4 && (rawText.length - index) > 4) {
  136. newText = [newText stringByAppendingString:hyphen];
  137. }
  138. index += 4;
  139. }
  140. //calculate new cursor position
  141. NSRange cursorPos = textView.selectedRange;
  142. NSUInteger hyphenCount = [[textView.text componentsSeparatedByString:hyphen] count] - 1;
  143. if (cursorPos.location > (hyphenCount * 4)) {
  144. NSUInteger newHyphenCount = [[newText componentsSeparatedByString:hyphen] count] - 1;
  145. cursorPos.location += newHyphenCount - hyphenCount;
  146. }
  147. //update modified text and set cursor position
  148. textView.text = newText;
  149. textView.selectedRange = cursorPos;
  150. }
  151. _backupLabel.hidden = textView.text.length > 0 ? YES : NO;
  152. [self updateDoneEnabledWithPassword:_passwordTextField.text];
  153. }
  154. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  155. [self refreshView];
  156. }
  157. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  158. [self doneAction:nil];
  159. return YES;
  160. }
  161. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  162. NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
  163. [self updateDoneEnabledWithPassword:newText];
  164. return YES;
  165. }
  166. - (IBAction)cancelAction:(id)sender {
  167. if ([_delegate respondsToSelector:@selector(restoreIdentityCancelled)]) {
  168. [_delegate restoreIdentityCancelled];
  169. }
  170. }
  171. - (IBAction)doneAction:(id)sender {
  172. [self hideKeyboard];
  173. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  174. MyIdentityStore *myIdentityStore = [MyIdentityStore sharedMyIdentityStore];
  175. [myIdentityStore restoreFromBackup:_backupTextView.text withPassword:_passwordTextField.text onCompletion:^{
  176. ServerAPIConnector *apiConnector = [[ServerAPIConnector alloc] init];
  177. /* Obtain server group from server */
  178. [apiConnector updateMyIdentityStore:myIdentityStore onCompletion:^{
  179. [myIdentityStore storeInKeychain];
  180. dispatch_async(dispatch_get_main_queue(), ^{
  181. [MBProgressHUD hideHUDForView:self.view animated:YES];
  182. });
  183. if ([_delegate respondsToSelector:@selector(restoreIdentityDone)]) {
  184. [_delegate restoreIdentityDone];
  185. }
  186. } onError:^(NSError *error) {
  187. [self handleError:error];
  188. }];
  189. } onError:^(NSError *error) {
  190. [self handleError:error];
  191. }];
  192. }
  193. - (void)showScanViewController {
  194. ScanBackupController *scanController = [[ScanBackupController alloc] init];
  195. scanController.containingViewController = self;
  196. scanController.delegate = self;
  197. [scanController startScan];
  198. }
  199. - (void)hideKeyboard {
  200. [_backupTextView resignFirstResponder];
  201. [_passwordTextField resignFirstResponder];
  202. }
  203. - (void)handleError:(NSError *)error {
  204. dispatch_async(dispatch_get_main_queue(), ^{
  205. [self hideKeyboard];
  206. [MBProgressHUD hideHUDForView:self.view animated:YES];
  207. IntroQuestionView *view = (IntroQuestionView *)[NibUtil loadViewFromNibWithName:@"IntroQuestionView"];
  208. view.showOnlyOkButton = YES;
  209. view.questionLabel.text = error.localizedDescription;
  210. view.delegate = self;
  211. view.frame = [RectUtil rect:view.frame centerIn:self.view.frame round:YES];
  212. [self.view addSubview:view];
  213. [self showMessageView:view];
  214. });
  215. }
  216. - (void)updateTextViewWithBackupCode {
  217. if (_backupTextView.text.length == 0) {
  218. if ([AppDelegate sharedAppDelegate].urlRestoreData != nil) {
  219. /* put the dashes back in */
  220. _backupTextView.text = [[MyIdentityStore sharedMyIdentityStore] addBackupGroupDashes:[AppDelegate sharedAppDelegate].urlRestoreData];
  221. _backupLabel.hidden = YES;
  222. [_passwordTextField becomeFirstResponder];
  223. } else if (_backupData) {
  224. _backupTextView.text = [[MyIdentityStore sharedMyIdentityStore] addBackupGroupDashes:_backupData];
  225. _backupLabel.hidden = YES;
  226. if (_passwordData) {
  227. _passwordTextField.text = _passwordData;
  228. } else {
  229. [_passwordTextField becomeFirstResponder];
  230. }
  231. } else {
  232. [_backupTextView becomeFirstResponder];
  233. }
  234. }
  235. [self updateDoneEnabledWithPassword:_passwordTextField.text];
  236. }
  237. - (void)updateDoneEnabledWithPassword:(NSString*)password {
  238. /* enable done only if we have 50 bytes worth of Base32 data in backup data
  239. and a suitable password */
  240. BOOL enabled = YES;
  241. if (password.length < kMinimumPasswordLength) {
  242. enabled = NO;
  243. }
  244. if (![[MyIdentityStore sharedMyIdentityStore] isValidBackupFormat:_backupTextView.text]) {
  245. enabled = NO;
  246. }
  247. if (enabled) {
  248. _passwordTextField.enablesReturnKeyAutomatically = YES;
  249. _doneButton.userInteractionEnabled = YES;
  250. _doneButton.alpha = 1.0;
  251. } else {
  252. _passwordTextField.enablesReturnKeyAutomatically = NO;
  253. _doneButton.userInteractionEnabled = NO;
  254. _doneButton.alpha = 0.4;
  255. }
  256. }
  257. - (void) refreshView {
  258. _scanView.hidden = _backupTextView.isFirstResponder && [ScanBackupController canScan] ? NO : YES;
  259. }
  260. #pragma mark - IntroQuestionViewDelegate
  261. - (void)selectedOk:(IntroQuestionView *)sender {
  262. [self hideMessageView:sender ignoreControls:YES];
  263. [_passwordTextField becomeFirstResponder];
  264. }
  265. #pragma mark Scan backup controller delegate
  266. - (void)didScanBackup:(NSString *)backup {
  267. _backupLabel.hidden = YES;
  268. _backupTextView.text = backup;
  269. }
  270. #pragma mark - UITapGestureRecognizer
  271. - (void)tappedScan:(UITapGestureRecognizer *)sender
  272. {
  273. if (sender.state == UIGestureRecognizerStateEnded) {
  274. [self showScanViewController];
  275. }
  276. }
  277. - (void)tappedMainView:(UITapGestureRecognizer *)sender
  278. {
  279. if (sender.state == UIGestureRecognizerStateEnded) {
  280. [self hideKeyboard];
  281. }
  282. }
  283. #pragma mark - UISwipeGestureRecognizer
  284. - (void)swipeAction:(UISwipeGestureRecognizer *)sender {
  285. if (sender.state == UIGestureRecognizerStateEnded) {
  286. if ([_delegate respondsToSelector:@selector(restoreIdentityCancelled)]) {
  287. [_delegate restoreIdentityCancelled];
  288. }
  289. }
  290. }
  291. @end