|
| 1 | +// |
| 2 | +// MigrateKeychain.swift |
| 3 | +// Simplenote |
| 4 | +// Migrates keychain items from previous keychain group |
| 5 | +// |
| 6 | +import Foundation |
| 7 | + |
| 8 | + |
| 9 | +// MARK: - KeychainMigrator |
| 10 | +// |
| 11 | +@objc |
| 12 | +class KeychainMigrator: NSObject { |
| 13 | + |
| 14 | + /// Keychain Constants |
| 15 | + /// |
| 16 | + private struct Constants { |
| 17 | + /// Legacy TeamID |
| 18 | + /// |
| 19 | + static let oldPrefix = "4ESDVWK654." |
| 20 | + |
| 21 | + /// New TeamID! |
| 22 | + /// |
| 23 | + static let newPrefix = "PZYM8XX95Q." |
| 24 | + |
| 25 | + /// Main App's Bundle ID |
| 26 | + /// |
| 27 | + static let bundleId = "com.codality.NotationalFlow" |
| 28 | + |
| 29 | + /// Share Extension's Bundle ID |
| 30 | + /// |
| 31 | + static let shareBundleId = bundleId + ".Share" |
| 32 | + |
| 33 | + /// Username's User Defaults Key |
| 34 | + /// |
| 35 | + static let usernameKey = "SPUsername" |
| 36 | + |
| 37 | + /// Legacy TeamID Access Group |
| 38 | + /// |
| 39 | + static let legacyAccessGroup = oldPrefix + bundleId |
| 40 | + |
| 41 | + /// New TeamID Access Group |
| 42 | + /// |
| 43 | + static let newAccessGroup = newPrefix + bundleId |
| 44 | + } |
| 45 | + |
| 46 | + /// Migrates the Legacy Keychain Entry over to the new Access Group |
| 47 | + /// |
| 48 | + @objc |
| 49 | + func migrateIfNecessary() { |
| 50 | + guard needsPasswordMigration() else { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + migrateLegacyPassword() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +// MARK: - Internal Helpers: This should actually be *private*, but for unit testing purposes, we're keeping them this way. |
| 60 | +// |
| 61 | +extension KeychainMigrator { |
| 62 | + |
| 63 | + /// Indicates if the Migration should take place. This is true whenever: |
| 64 | + /// |
| 65 | + /// - The Username is accessible |
| 66 | + /// - There is no current password |
| 67 | + /// |
| 68 | + func needsPasswordMigration() -> Bool { |
| 69 | + guard let username = self.username else { |
| 70 | + return false |
| 71 | + } |
| 72 | + |
| 73 | + let newKeychainEntry = try? loadKeychainEntry(accessGroup: .new, username: username) |
| 74 | + return newKeychainEntry == nil |
| 75 | + } |
| 76 | + |
| 77 | + /// Migrates the Keychain Entry associated with the Old TeamID Prefix |
| 78 | + /// |
| 79 | + func migrateLegacyPassword() { |
| 80 | + guard let username = self.username, |
| 81 | + let legacyPassword = try? loadKeychainEntry(accessGroup: .legacy, username: username) |
| 82 | + else { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Looks like we need to attempt a migration... |
| 87 | + do { |
| 88 | + try deleteKeychainEntry(accessGroup: .legacy, username: username) |
| 89 | + try saveKeychainEntry(accessGroup: .new, username: username, password: legacyPassword) |
| 90 | + |
| 91 | + SPTracker.trackKeychainMigrationSucceeded() |
| 92 | + } catch { |
| 93 | + // :( |
| 94 | + NSLog("Keychain Migration Error: \(error)") |
| 95 | + |
| 96 | + SPTracker.trackKeychainMigrationFailed() |
| 97 | + self.restoreLegacyPassword(password: legacyPassword, for: username) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// On error, we'll attempt to restore the legacy Password |
| 102 | + /// |
| 103 | + func restoreLegacyPassword(password: String, for username: String) { |
| 104 | + do { |
| 105 | + try saveKeychainEntry(accessGroup: .legacy, username: username, password: password) |
| 106 | + SPTracker.trackKeychainFailsafeSucceeded() |
| 107 | + } catch { |
| 108 | + NSLog("Keychain Failsafe Error: \(error)") |
| 109 | + |
| 110 | + SPTracker.trackKeychainFailsafeFailed() |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +// MARK: - User Defaults Wrappers |
| 117 | +// |
| 118 | +extension KeychainMigrator { |
| 119 | + /// Username |
| 120 | + /// |
| 121 | + var username: String? { |
| 122 | + set { |
| 123 | + UserDefaults.standard.set(newValue, forKey: Constants.usernameKey) |
| 124 | + UserDefaults.standard.synchronize() |
| 125 | + } |
| 126 | + get { |
| 127 | + return UserDefaults.standard.string(forKey: Constants.usernameKey) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +// MARK: - Keychain Wrappers: This should actually be *private*, but for unit testing purposes, we're keeping them this way. |
| 134 | +// |
| 135 | +extension KeychainMigrator { |
| 136 | + |
| 137 | + enum AccessGroup { |
| 138 | + case new |
| 139 | + case legacy |
| 140 | + |
| 141 | + var stringValue: String { |
| 142 | + switch self { |
| 143 | + case .new: |
| 144 | + return Constants.newAccessGroup |
| 145 | + case .legacy: |
| 146 | + return Constants.legacyAccessGroup |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + func loadKeychainEntry(accessGroup: AccessGroup, username: String) throws -> String { |
| 152 | + let passwordItem = KeychainPasswordItem( |
| 153 | + service: SPCredentials.simperiumAppID(), |
| 154 | + account: username, |
| 155 | + accessGroup: accessGroup.stringValue |
| 156 | + ) |
| 157 | + |
| 158 | + return try passwordItem.readPassword() |
| 159 | + } |
| 160 | + |
| 161 | + func deleteKeychainEntry(accessGroup: AccessGroup, username: String) throws { |
| 162 | + let passwordItem = KeychainPasswordItem( |
| 163 | + service: SPCredentials.simperiumAppID(), |
| 164 | + account: username, |
| 165 | + accessGroup: accessGroup.stringValue |
| 166 | + ) |
| 167 | + |
| 168 | + try passwordItem.deleteItem() |
| 169 | + } |
| 170 | + |
| 171 | + func saveKeychainEntry(accessGroup: AccessGroup, username: String, password: String) throws { |
| 172 | + let passwordItem = KeychainPasswordItem( |
| 173 | + service: SPCredentials.simperiumAppID(), |
| 174 | + account: username, |
| 175 | + accessGroup: accessGroup.stringValue |
| 176 | + ) |
| 177 | + |
| 178 | + try passwordItem.savePassword(password) |
| 179 | + } |
| 180 | + |
| 181 | +#if RELEASE |
| 182 | + /// This method tests the Migration Flow. This should only be executed in the *Release* target, since the AppID's won't |
| 183 | + /// match with the one(s) used in the other targets. |
| 184 | + /// |
| 185 | + /// For testing purposes only. |
| 186 | + /// |
| 187 | + @objc |
| 188 | + func testMigration() { |
| 189 | + let dummyUsername = "TestingUsername" |
| 190 | + let dummyPassword = "TestingPassword" |
| 191 | + |
| 192 | + // Recreate Pre-Migration Scenario |
| 193 | + username = dummyUsername |
| 194 | + try? deleteKeychainEntry(accessGroup: .new, username: dummyUsername) |
| 195 | + try? saveKeychainEntry(accessGroup: .legacy, username: dummyUsername, password: dummyPassword) |
| 196 | + |
| 197 | + // Migrate |
| 198 | + migrateIfNecessary() |
| 199 | + |
| 200 | + // Verify |
| 201 | + let migrated = try? loadKeychainEntry(accessGroup: .new, username: dummyUsername) |
| 202 | + assert(migrated == dummyPassword) |
| 203 | + } |
| 204 | +#endif |
| 205 | +} |
0 commit comments