Skip to content

Commit

Permalink
key mapping solution
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbzurovski committed Dec 11, 2024
1 parent bbbf801 commit 66c16b7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions apple/Sources/Sargon/Drivers/TestDrivers/TestDrivers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extension BIOS {
BIOS(
bundle: bundle,
userDefaultsSuite: userDefaultsSuite,
userDefaultsKeyMapping: [:],
secureStorageDriver: Insecure︕!TestOnly︕!Ephemeral︕!SecureStorage(
keychainService: "test"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ 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
)
}

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
)
)
}
Expand Down
2 changes: 2 additions & 0 deletions apple/Sources/Sargon/SargonOS/TestOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@ 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(),
}
}
)
}
}

#[cfg(test)]
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"
);
}
}

0 comments on commit 66c16b7

Please sign in to comment.