Skip to content

Commit

Permalink
[trello.com/c/siM0qcTZ] fix: clear keychain on reinstall
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavDevIOS committed Sep 21, 2024
1 parent 9f7db49 commit 158ce01
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
12 changes: 0 additions & 12 deletions Adamant/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions CommonKit/Sources/CommonKit/Core/SecuredStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,4 @@ public protocol SecuredStore: AnyObject {
func set<T: Encodable>(_ value: T, for key: String)

func remove(_ key: String)

/// Remove everything
func purgeStore()
}
23 changes: 18 additions & 5 deletions CommonKit/Sources/CommonKit/Services/KeychainStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class KeychainStore: SecuredStore {
public init(secureStorage: SecureStorageProtocol) {
self.secureStorage = secureStorage

clearIfNeeded()
configure()
migrateIfNeeded()
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -201,3 +212,5 @@ private extension KeychainStore {
}
}
}

private let firstRun = "app.firstRun"

0 comments on commit 158ce01

Please sign in to comment.