FLEXKeychainTableViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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) NSMutableArray<NSDictionary *> *keychainItems;
  15. @property (nonatomic) NSString *headerTitle;
  16. @end
  17. @implementation FLEXKeychainTableViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. self.navigationItem.rightBarButtonItems = @[
  21. [[UIBarButtonItem alloc]
  22. initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trashPressed)
  23. ],
  24. [[UIBarButtonItem alloc]
  25. initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPressed)
  26. ],
  27. ];
  28. [self refreshkeychainItems];
  29. [self updateHeaderTitle];
  30. }
  31. - (void)refreshkeychainItems {
  32. self.keychainItems = [FLEXKeychain allAccounts].mutableCopy;
  33. }
  34. - (void)updateHeaderTitle {
  35. self.headerTitle = [NSString stringWithFormat:@"%@ items", @(self.keychainItems.count)];
  36. }
  37. - (FLEXKeychainQuery *)queryForItemAtIndex:(NSInteger)idx {
  38. NSDictionary *item = self.keychainItems[idx];
  39. FLEXKeychainQuery *query = [FLEXKeychainQuery new];
  40. query.service = item[kFLEXKeychainWhereKey];
  41. query.account = item[kFLEXKeychainAccountKey];
  42. [query fetch:nil];
  43. return query;
  44. }
  45. - (void)deleteItem:(NSDictionary *)item {
  46. NSError *error = nil;
  47. BOOL success = [FLEXKeychain
  48. deletePasswordForService:item[kFLEXKeychainWhereKey]
  49. account:item[kFLEXKeychainAccountKey]
  50. error:&error
  51. ];
  52. if (!success) {
  53. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  54. make.title(@"Error Deleting Item");
  55. make.message(error.localizedDescription);
  56. } showFrom:self];
  57. }
  58. }
  59. #pragma mark Buttons
  60. - (void)trashPressed {
  61. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  62. make.title(@"Clear Keychain");
  63. make.message(@"This will remove all keychain items for this app.\n");
  64. make.message(@"This action cannot be undone. Are you sure?");
  65. make.button(@"Yes, clear the keychain").destructiveStyle().handler(^(NSArray *strings) {
  66. for (id account in self.keychainItems) {
  67. [self deleteItem:account];
  68. }
  69. [self refreshkeychainItems];
  70. [self.tableView reloadData];
  71. });
  72. make.button(@"Cancel").cancelStyle();
  73. } showFrom:self];
  74. }
  75. - (void)addPressed {
  76. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  77. make.title(@"Add Keychain Item");
  78. make.textField(@"Service name, i.e. Instagram");
  79. make.textField(@"Account, i.e. username@example.com");
  80. make.textField(@"Password");
  81. make.button(@"Cancel").cancelStyle();
  82. make.button(@"Save").handler(^(NSArray<NSString *> *strings) {
  83. // Display errors
  84. NSError *error = nil;
  85. if (![FLEXKeychain setPassword:strings[2] forService:strings[0] account:strings[1] error:&error]) {
  86. [FLEXAlert showAlert:@"Error" message:error.localizedDescription from:self];
  87. }
  88. [self refreshkeychainItems];
  89. [self.tableView reloadData];
  90. });
  91. } showFrom:self];
  92. }
  93. #pragma mark - FLEXGlobalsEntry
  94. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  95. return @"🔑 Keychain";
  96. }
  97. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  98. FLEXKeychainTableViewController *viewController = [self new];
  99. viewController.title = [self globalsEntryTitle:row];
  100. return viewController;
  101. }
  102. #pragma mark - Table View Data Source
  103. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  104. return self.keychainItems.count;
  105. }
  106. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  107. static NSString *CellIdentifier = @"Cell";
  108. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  109. if (!cell) {
  110. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  111. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  112. cell.textLabel.font = UIFont.flex_defaultTableCellFont;
  113. }
  114. NSDictionary *item = self.keychainItems[indexPath.row];
  115. id account = item[kFLEXKeychainAccountKey];
  116. if ([account isKindOfClass:[NSString class]]) {
  117. cell.textLabel.text = account;
  118. } else {
  119. cell.textLabel.text = [NSString stringWithFormat:
  120. @"[%@]\n\n%@",
  121. NSStringFromClass([account class]),
  122. [account description]
  123. ];
  124. }
  125. return cell;
  126. }
  127. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  128. return self.headerTitle;
  129. }
  130. - (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)style forRowAtIndexPath:(NSIndexPath *)ip {
  131. if (style == UITableViewCellEditingStyleDelete) {
  132. [self deleteItem:self.keychainItems[ip.row]];
  133. [self.keychainItems removeObjectAtIndex:ip.row];
  134. [tv deleteRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationAutomatic];
  135. }
  136. }
  137. #pragma mark - Table View Delegate
  138. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  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