diff --git a/Adamant/App/AppDelegate.swift b/Adamant/App/AppDelegate.swift index 16452eb44..32a247d4d 100644 --- a/Adamant/App/AppDelegate.swift +++ b/Adamant/App/AppDelegate.swift @@ -38,7 +38,6 @@ extension StoreKey { struct application { static let welcomeScreensIsShown = "app.welcomeScreensIsShown" static let eulaAccepted = "app.eulaAccepted" - static let firstRun = "app.firstRun" private init() {} } @@ -79,17 +78,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { .resolve(CrashlyticsService.self)? .configureIfNeeded() - // MARK: 1.2 First run flag - let firstRun = UserDefaults.standard.bool(forKey: StoreKey.application.firstRun) - - if !firstRun { - UserDefaults.standard.set(true, forKey: StoreKey.application.firstRun) - - if let securedStore = container.resolve(SecuredStore.self) { - securedStore.purgeStore() - } - } - // MARK: 2. Init UI let window = UIWindow(frame: UIScreen.main.bounds) self.window = window diff --git a/CommonKit/Sources/CommonKit/Core/SecuredStore.swift b/CommonKit/Sources/CommonKit/Core/SecuredStore.swift index acd27ca24..0dfab93d1 100644 --- a/CommonKit/Sources/CommonKit/Core/SecuredStore.swift +++ b/CommonKit/Sources/CommonKit/Core/SecuredStore.swift @@ -73,7 +73,4 @@ public protocol SecuredStore: AnyObject { func set(_ value: T, for key: String) func remove(_ key: String) - - /// Remove everything - func purgeStore() } diff --git a/CommonKit/Sources/CommonKit/Services/KeychainStore.swift b/CommonKit/Sources/CommonKit/Services/KeychainStore.swift index 5d781c5eb..e6fbd85dd 100644 --- a/CommonKit/Sources/CommonKit/Services/KeychainStore.swift +++ b/CommonKit/Sources/CommonKit/Services/KeychainStore.swift @@ -27,6 +27,7 @@ public final class KeychainStore: SecuredStore { public init(secureStorage: SecureStorageProtocol) { self.secureStorage = secureStorage + clearIfNeeded() configure() migrateIfNeeded() } @@ -57,11 +58,6 @@ public final class KeychainStore: SecuredStore { public func remove(_ key: String) { try? KeychainStore.keychain.remove(key) } - - public func purgeStore() { - try? KeychainStore.keychain.removeAll() - NotificationCenter.default.post(name: Notification.Name.SecuredStore.securedStorePurged, object: self) - } } private extension KeychainStore { @@ -93,6 +89,16 @@ private extension KeychainStore { setData(encryptedData, for: keychainStoreIdAlias) } + func clearIfNeeded() { + let isFirstRun = !UserDefaults.standard.bool(forKey: firstRun) + + guard isFirstRun else { return } + + UserDefaults.standard.set(true, forKey: firstRun) + + purgeStore() + } + func getValue(_ key: String) -> Data? { guard let keychainPassword = keychainPassword, let data = getData(for: key) @@ -151,6 +157,11 @@ private extension KeychainStore { } return try? RNCryptor.decrypt(data: encryptedData, withPassword: password) } + + func purgeStore() { + try? KeychainStore.keychain.removeAll() + NotificationCenter.default.post(name: Notification.Name.SecuredStore.securedStorePurged, object: self) + } } private extension KeychainStore { @@ -201,3 +212,5 @@ private extension KeychainStore { } } } + +private let firstRun = "app.firstRun"