From 66c16b7a20e8cc1dfebcef70d69411d4f1fcc861 Mon Sep 17 00:00:00 2001 From: Matias Bzurovski Date: Wed, 11 Dec 2024 18:05:08 +0100 Subject: [PATCH] key mapping solution --- ...ecureTestOnlyEphemeral_SecureStorage.swift | 4 +++ .../Drivers/TestDrivers/TestDrivers.swift | 1 + .../UnsafeStorageDriver+UserDefaults.swift | 22 ++++++++++--- .../System/BIOS/BIOS+Swiftified.swift | 2 ++ .../System/Drivers/Drivers+Swiftified.swift | 6 +++- apple/Sources/Sargon/SargonOS/TestOS.swift | 2 ++ .../support/unsafe_storage_key.rs | 31 ++++++++----------- 7 files changed, 45 insertions(+), 23 deletions(-) diff --git a/apple/Sources/Sargon/Drivers/TestDrivers/SecureStorage/SecureStorageDriver+InsecureTestOnlyEphemeral_SecureStorage.swift b/apple/Sources/Sargon/Drivers/TestDrivers/SecureStorage/SecureStorageDriver+InsecureTestOnlyEphemeral_SecureStorage.swift index 8289be270..9e6de1857 100644 --- a/apple/Sources/Sargon/Drivers/TestDrivers/SecureStorage/SecureStorageDriver+InsecureTestOnlyEphemeral_SecureStorage.swift +++ b/apple/Sources/Sargon/Drivers/TestDrivers/SecureStorage/SecureStorageDriver+InsecureTestOnlyEphemeral_SecureStorage.swift @@ -25,5 +25,9 @@ extension Insecure︕!TestOnly︕!Ephemeral︕!SecureStorage: SecureStorag public func deleteDataForKey(key: SecureStorageKey) async throws { dictionary.removeValue(forKey: key) } + + public func containsDataForKey(key: SecureStorageKey) async throws -> Bool { + dictionary.keys.contains(key) + } } #endif diff --git a/apple/Sources/Sargon/Drivers/TestDrivers/TestDrivers.swift b/apple/Sources/Sargon/Drivers/TestDrivers/TestDrivers.swift index e8888b383..558573218 100644 --- a/apple/Sources/Sargon/Drivers/TestDrivers/TestDrivers.swift +++ b/apple/Sources/Sargon/Drivers/TestDrivers/TestDrivers.swift @@ -11,6 +11,7 @@ extension BIOS { BIOS( bundle: bundle, userDefaultsSuite: userDefaultsSuite, + userDefaultsKeyMapping: [:], secureStorageDriver: Insecure︕!TestOnly︕!Ephemeral︕!SecureStorage( keychainService: "test" ) diff --git a/apple/Sources/Sargon/Drivers/UnsafeStorage/UnsafeStorageDriver+UserDefaults.swift b/apple/Sources/Sargon/Drivers/UnsafeStorage/UnsafeStorageDriver+UserDefaults.swift index 00f29187a..4da6a5657 100644 --- a/apple/Sources/Sargon/Drivers/UnsafeStorage/UnsafeStorageDriver+UserDefaults.swift +++ b/apple/Sources/Sargon/Drivers/UnsafeStorage/UnsafeStorageDriver+UserDefaults.swift @@ -18,8 +18,14 @@ extension UnsafeStorageDriver where Self == UnsafeStorage { public final class UnsafeStorage: Sendable { public typealias Key = UnsafeStorageKey fileprivate let userDefaults: UserDefaults - public init(userDefaults: UserDefaults = .standard) { + + /// A dictionary containing the custom String value used for a given `UnsafeStorageKey`. + /// This is necessary since some UserDefaults were saved by the Host apps prior to Sargon. + fileprivate let keyMapping: [Key: String] + + public init(userDefaults: UserDefaults = .standard, keyMapping: [Key: String] = [:]) { self.userDefaults = userDefaults + self.keyMapping = keyMapping } /// Singleton `UnsafeStorageDriver` of type `UnsafeStorage, @@ -38,14 +44,22 @@ extension UnsafeStorageKey { // MARK: - UnsafeStorage + UnsafeStorageDriver extension UnsafeStorage: UnsafeStorageDriver { public func loadData(key: Key) -> Data? { - userDefaults.data(forKey: key.identifier) + userDefaults.data(forKey: identifier(for: key)) } public func saveData(key: Key, data: Data) { - userDefaults.setValue(data, forKey: key.identifier) + userDefaults.setValue(data, forKey: identifier(for: key)) } public func deleteDataForKey(key: Key) { - userDefaults.removeObject(forKey: key.identifier) + userDefaults.removeObject(forKey: identifier(for: key)) + } + + private func identifier(for key: Key) -> String { + if let mapped = keyMapping[key] { + mapped + } else { + key.identifier + } } } diff --git a/apple/Sources/Sargon/Extensions/Swiftified/System/BIOS/BIOS+Swiftified.swift b/apple/Sources/Sargon/Extensions/Swiftified/System/BIOS/BIOS+Swiftified.swift index 82ba53526..5a265a769 100644 --- a/apple/Sources/Sargon/Extensions/Swiftified/System/BIOS/BIOS+Swiftified.swift +++ b/apple/Sources/Sargon/Extensions/Swiftified/System/BIOS/BIOS+Swiftified.swift @@ -10,11 +10,13 @@ extension BIOS { public convenience init( bundle: Bundle, userDefaultsSuite: String, + userDefaultsKeyMapping: [UnsafeStorageKey: String], secureStorageDriver: SecureStorageDriver ) { let drivers = Drivers( bundle: bundle, userDefaultsSuite: userDefaultsSuite, + userDefaultsKeyMapping: userDefaultsKeyMapping, secureStorageDriver: secureStorageDriver ) // https://en.wikipedia.org/wiki/Power-on_self-test diff --git a/apple/Sources/Sargon/Extensions/Swiftified/System/Drivers/Drivers+Swiftified.swift b/apple/Sources/Sargon/Extensions/Swiftified/System/Drivers/Drivers+Swiftified.swift index 5bfe9c472..83f0eabc3 100644 --- a/apple/Sources/Sargon/Extensions/Swiftified/System/Drivers/Drivers+Swiftified.swift +++ b/apple/Sources/Sargon/Extensions/Swiftified/System/Drivers/Drivers+Swiftified.swift @@ -8,11 +8,13 @@ extension Drivers { public convenience init( bundle: Bundle, userDefaultsSuite: String, + userDefaultsKeyMapping: [UnsafeStorageKey: String], secureStorageDriver: SecureStorageDriver ) { self.init( appVersion: (bundle.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "Unknown", userDefaultsSuite: userDefaultsSuite, + userDefaultsKeyMapping: userDefaultsKeyMapping, secureStorageDriver: secureStorageDriver ) } @@ -20,13 +22,15 @@ extension Drivers { public convenience init( appVersion: String, userDefaultsSuite: String, + userDefaultsKeyMapping: [UnsafeStorageKey: String], secureStorageDriver: SecureStorageDriver ) { self.init( secureStorage: secureStorageDriver, hostInfo: AppleHostInfoDriver(appVersion: appVersion), unsafeStorage: UnsafeStorage( - userDefaults: .init(suiteName: userDefaultsSuite)! + userDefaults: .init(suiteName: userDefaultsSuite)!, + keyMapping: userDefaultsKeyMapping ) ) } diff --git a/apple/Sources/Sargon/SargonOS/TestOS.swift b/apple/Sources/Sargon/SargonOS/TestOS.swift index 4ec6d4c8d..f026ee3a4 100644 --- a/apple/Sources/Sargon/SargonOS/TestOS.swift +++ b/apple/Sources/Sargon/SargonOS/TestOS.swift @@ -7,11 +7,13 @@ extension BIOS { public static func test( bundle: Bundle = .main, userDefaultsSuite: String = "Test", + userDefaultsKeyMapping: [UnsafeStorageKey: String] = [:], secureStorageDriver: SecureStorageDriver ) -> BIOS { BIOS( bundle: bundle, userDefaultsSuite: userDefaultsSuite, + userDefaultsKeyMapping: userDefaultsKeyMapping, secureStorageDriver: secureStorageDriver ) } diff --git a/crates/sargon/src/system/drivers/unsafe_storage_driver/support/unsafe_storage_key.rs b/crates/sargon/src/system/drivers/unsafe_storage_driver/support/unsafe_storage_key.rs index 1ccddea43..2ae998a3d 100644 --- a/crates/sargon/src/system/drivers/unsafe_storage_driver/support/unsafe_storage_key.rs +++ b/crates/sargon/src/system/drivers/unsafe_storage_driver/support/unsafe_storage_key.rs @@ -7,18 +7,13 @@ pub enum UnsafeStorageKey { impl UnsafeStorageKey { pub fn identifier(&self) -> String { - // format!( - // "unsafe_storage_key_{}", - // match self { - // UnsafeStorageKey::FactorSourceUserHasWrittenDown => - // "factor_source_user_has_written_down".to_owned(), - // } - // ) - match self { - UnsafeStorageKey::FactorSourceUserHasWrittenDown => { - "mnemonicsUserClaimsToHaveBackedUp".to_owned() + format!( + "unsafe_storage_key_{}", + match self { + UnsafeStorageKey::FactorSourceUserHasWrittenDown => + "factor_source_user_has_written_down".to_owned(), } - } + ) } } @@ -26,11 +21,11 @@ impl UnsafeStorageKey { mod tests { use crate::prelude::*; - // #[test] - // fn identifier() { - // assert_eq!( - // UnsafeStorageKey::FactorSourceUserHasWrittenDown.identifier(), - // "unsafe_storage_key_factor_source_user_has_written_down" - // ); - // } + #[test] + fn identifier() { + assert_eq!( + UnsafeStorageKey::FactorSourceUserHasWrittenDown.identifier(), + "unsafe_storage_key_factor_source_user_has_written_down" + ); + } }