RevocationKeyHandler.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-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 "RevocationKeyHandler.h"
  21. #import "MyIdentityStore.h"
  22. #import "ServerAPIConnector.h"
  23. #import "MBProgressHUD.h"
  24. #import "AppDelegate.h"
  25. @interface RevocationKeyHandler ()
  26. @property UIViewController *viewController;
  27. @end
  28. @implementation RevocationKeyHandler
  29. -(void)passwordResult:(NSString *)password fromViewController:(UIViewController *)viewController {
  30. _viewController = viewController;
  31. [MBProgressHUD showHUDAddedTo:_viewController.view animated:YES];
  32. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  33. [self setPassword:password];
  34. });
  35. }
  36. - (void)setPassword:(NSString *)password {
  37. ServerAPIConnector *connector = [[ServerAPIConnector alloc] init];
  38. MyIdentityStore *store = [MyIdentityStore sharedMyIdentityStore];
  39. [connector setRevocationPassword:password forStore:store onCompletion:^{
  40. store.revocationPasswordLastCheck = nil;
  41. [self hideUI:YES];
  42. } onError:^(NSError *error) {
  43. [self hideUI:NO];
  44. }];
  45. }
  46. - (void)hideUI:(BOOL)success {
  47. dispatch_async(dispatch_get_main_queue(), ^{
  48. [MBProgressHUD hideHUDForView:_viewController.view animated:YES];
  49. [_viewController dismissViewControllerAnimated:YES completion:^{
  50. if (success) {
  51. [self showAlertWithMessage:NSLocalizedString(@"revocation_request_success", nil)];
  52. } else {
  53. [self showAlertWithMessage:NSLocalizedString(@"revocation_request_failed", nil)];
  54. }
  55. }];
  56. });
  57. }
  58. - (void)showAlertWithMessage:(NSString *)message {
  59. [UIAlertTemplate showAlertWithOwner:[[AppDelegate sharedAppDelegate] currentTopViewController] title:@"" message:message actionOk:nil];
  60. }
  61. - (void)updateLastSetDateForLabel:(UILabel *)label {
  62. label.text = @"...";
  63. ServerAPIConnector *connector = [[ServerAPIConnector alloc] init];
  64. MyIdentityStore *store = [MyIdentityStore sharedMyIdentityStore];
  65. if (store.revocationPasswordLastCheck == nil) {
  66. /* Check revocation password now */
  67. [connector checkRevocationPasswordForStore:store onCompletion:^(BOOL revocationPasswordSet, NSDate *lastChanged) {
  68. store.revocationPasswordLastCheck = [NSDate date];
  69. if (revocationPasswordSet)
  70. store.revocationPasswordSetDate = lastChanged;
  71. else
  72. store.revocationPasswordSetDate = nil;
  73. [self updateLastSetDateForLabelAfterCheck:label];
  74. } onError:^(NSError *error) {
  75. label.text = NSLocalizedString(@"revocation_check_failed", nil);
  76. }];
  77. } else {
  78. [self updateLastSetDateForLabelAfterCheck:label];
  79. }
  80. }
  81. - (void)updateLastSetDateForLabelAfterCheck:(UILabel *)label {
  82. MyIdentityStore *store = [MyIdentityStore sharedMyIdentityStore];
  83. if (store.revocationPasswordSetDate != nil) {
  84. label.text = [DateFormatter getShortDate:store.revocationPasswordSetDate];
  85. } else {
  86. label.text = NSLocalizedString(@"revocation_password_not_set", nil);
  87. }
  88. }
  89. @end