FLEXKeychainTableViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // FLEXKeychainTableViewController.m
  3. // FLEX
  4. //
  5. // Created by ray on 2019/8/17.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXKeychain.h"
  9. #import "FLEXKeychainQuery.h"
  10. #import "FLEXKeychainTableViewController.h"
  11. #import "FLEXUtility.h"
  12. #import "UIPasteboard+FLEX.h"
  13. @interface FLEXKeychainTableViewController ()
  14. @property (nonatomic) NSArray<NSDictionary *> *keychainItems;
  15. @property (nonatomic) NSString *headerTitle;
  16. @end
  17. @implementation FLEXKeychainTableViewController
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. self.navigationItem.rightBarButtonItems = @[
  22. [[UIBarButtonItem alloc]
  23. initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trashPressed)
  24. ],
  25. [[UIBarButtonItem alloc]
  26. initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPressed)
  27. ],
  28. ];
  29. [self refreshkeychainItems];
  30. [self updateHeaderTitle];
  31. }
  32. - (void)refreshkeychainItems
  33. {
  34. self.keychainItems = [FLEXKeychain allAccounts];
  35. }
  36. - (void)updateHeaderTitle
  37. {
  38. self.headerTitle = [NSString stringWithFormat:@"%@ items", @(self.keychainItems.count)];
  39. }
  40. - (FLEXKeychainQuery *)queryForItemAtIndex:(NSInteger)idx
  41. {
  42. NSDictionary *item = self.keychainItems[idx];
  43. FLEXKeychainQuery *query = [FLEXKeychainQuery new];
  44. query.service = item[kFLEXKeychainWhereKey];
  45. query.account = item[kFLEXKeychainAccountKey];
  46. [query fetch:nil];
  47. return query;
  48. }
  49. #pragma mark Buttons
  50. - (void)trashPressed
  51. {
  52. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  53. make.title(@"Clear Keychain");
  54. make.message(@"This will remove all keychain items for this app.\n");
  55. make.message(@"This action cannot be undone. Are you sure?");
  56. make.button(@"Yes, clear the keychain").destructiveStyle().handler(^(NSArray *strings) {
  57. for (id account in self.keychainItems) {
  58. FLEXKeychainQuery *query = [FLEXKeychainQuery new];
  59. query.service = account[kFLEXKeychainWhereKey];
  60. query.account = account[kFLEXKeychainAccountKey];
  61. // Delete item or display error
  62. NSError *error = nil;
  63. if (![query deleteItem:&error]) {
  64. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  65. make.title(@"Error Deleting Item");
  66. make.message(error.localizedDescription);
  67. } showFrom:self];
  68. }
  69. }
  70. [self refreshkeychainItems];
  71. [self.tableView reloadData];
  72. });
  73. make.button(@"Cancel").cancelStyle();
  74. } showFrom:self];
  75. }
  76. - (void)addPressed
  77. {
  78. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  79. make.title(@"Add Keychain Item");
  80. make.textField(@"Service name, i.e. Instagram");
  81. make.textField(@"Account, i.e. username@example.com");
  82. make.textField(@"Password");
  83. make.button(@"Cancel").cancelStyle();
  84. make.button(@"Save").handler(^(NSArray<NSString *> *strings) {
  85. // Display errors
  86. NSError *error = nil;
  87. if (![FLEXKeychain setPassword:strings[2] forService:strings[0] account:strings[1] error:&error]) {
  88. [FLEXAlert showAlert:@"Error" message:error.localizedDescription from:self];
  89. }
  90. [self refreshkeychainItems];
  91. [self.tableView reloadData];
  92. });
  93. } showFrom:self];
  94. }
  95. #pragma mark - FLEXGlobalsEntry
  96. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row
  97. {
  98. return @"🔑 Keychain";
  99. }
  100. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  101. FLEXKeychainTableViewController *viewController = [self new];
  102. viewController.title = [self globalsEntryTitle:row];
  103. return viewController;
  104. }
  105. #pragma mark - Table View Data Source
  106. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  107. {
  108. return self.keychainItems.count;
  109. }
  110. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  111. {
  112. static NSString *CellIdentifier = @"Cell";
  113. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  114. if (!cell) {
  115. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  116. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  117. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  118. }
  119. NSDictionary *item = self.keychainItems[indexPath.row];
  120. id account = item[kFLEXKeychainAccountKey];
  121. if ([account isKindOfClass:[NSString class]]) {
  122. cell.textLabel.text = account;
  123. } else {
  124. cell.textLabel.text = [NSString stringWithFormat:
  125. @"[%@]\n\n%@",
  126. NSStringFromClass([account class]),
  127. [account description]
  128. ];
  129. }
  130. return cell;
  131. }
  132. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  133. {
  134. return self.headerTitle;
  135. }
  136. #pragma mark - Table View Delegate
  137. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  138. {
  139. FLEXKeychainQuery *query = [self queryForItemAtIndex:indexPath.row];
  140. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  141. make.title(query.service);
  142. make.message(@"Service: ").message(query.service);
  143. make.message(@"\nAccount: ").message(query.account);
  144. make.message(@"\nPassword: ").message(query.password);
  145. make.button(@"Copy Service").handler(^(NSArray<NSString *> *strings) {
  146. [UIPasteboard.generalPasteboard flex_copy:query.service];
  147. });
  148. make.button(@"Copy Account").handler(^(NSArray<NSString *> *strings) {
  149. [UIPasteboard.generalPasteboard flex_copy:query.account];
  150. });
  151. make.button(@"Copy Password").handler(^(NSArray<NSString *> *strings) {
  152. [UIPasteboard.generalPasteboard flex_copy:query.password];
  153. });
  154. make.button(@"Dismiss").cancelStyle();
  155. } showFrom:self];
  156. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  157. }
  158. @end