소스 검색

Merge pull request #1650 from deltachat/delete_from_keychain

delete account encryption keys from keychain if account was deleted
cyBerta 3 년 전
부모
커밋
08dae7a946

+ 13 - 0
DcCore/DcCore/Helper/KeychainManager.swift

@@ -35,6 +35,19 @@ public class KeychainManager {
         return status == errSecSuccess || status == errSecItemNotFound
     }
 
+    /**
+     * Tries to deletes the secret for a given account id, errors will be ignored
+     */
+    public static func deleteAccountSecret(id: Int) {
+        let query = [
+          kSecAttrAccount as String: "\(id)",
+          kSecClass: kSecClassGenericPassword,
+          kSecAttrAccessGroup as String: KcM.sharedKeychainGroup as AnyObject,
+          kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlock,
+        ] as CFDictionary
+        _ = SecItemDelete(query)
+    }
+
     private static func createRandomPassword() -> String {
         let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY1234567890"
         return String((0..<36).map { _ in letters.randomElement()! })

+ 1 - 0
deltachat-ios/Controller/SettingsController.swift

@@ -572,6 +572,7 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
                     guard let self = self else { return }
                     appDelegate.locationManager.disableLocationStreamingInAllChats()
                     _ = self.dcAccounts.remove(id: selectedAccountId)
+                    KeychainManager.deleteAccountSecret(id: selectedAccountId)
                     INInteraction.delete(with: "\(selectedAccountId)", completion: nil)
                     if self.dcAccounts.getAll().isEmpty {
                         _ = self.dcAccounts.add()

+ 2 - 0
deltachat-ios/Controller/WelcomeViewController.swift

@@ -204,6 +204,7 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
         let lastContextId = dcAccounts.getSelected().id
         let newContextId = dcAccounts.addClosedAccount()
         _ = dcAccounts.remove(id: lastContextId)
+        KeychainManager.deleteAccountSecret(id: lastContextId)
         _ = dcAccounts.select(id: newContextId)
         dcContext = dcAccounts.getSelected()
         do {
@@ -238,6 +239,7 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
         let selectedAccount = dcAccounts.getSelected()
         if selectedAccount.isOpen() && !selectedAccount.isConfigured() {
             _ = dcAccounts.remove(id: selectedAccount.id)
+            KeychainManager.deleteAccountSecret(id: selectedAccount.id)
             if self.dcAccounts.getAll().isEmpty {
                 _ = self.dcAccounts.add()
             }