diff --git a/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/CryptoService.swift b/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/CryptoService.swift index 1a5f6258d..4556a006f 100644 --- a/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/CryptoService.swift +++ b/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/CryptoService.swift @@ -70,6 +70,10 @@ public class CryptoService{ } return data } + + static func clearKey() { + KeychainHelper.standard.delete(CryptoService.ENCRYPTION_KEY) + } } public extension Data { diff --git a/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/EnvironmentService.swift b/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/EnvironmentService.swift index 09d29ec0f..96dea1cb3 100644 --- a/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/EnvironmentService.swift +++ b/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/EnvironmentService.swift @@ -42,4 +42,9 @@ class EnvironmentService{ } KeychainHelper.standard.save(url.data(using: .utf8)!, ICONS_URL_KEY) } + + func clear() { + baseUrl = nil + setIconsUrl(url: nil) + } } diff --git a/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/StateService.swift b/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/StateService.swift index 97f008520..a670a9a6e 100644 --- a/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/StateService.swift +++ b/src/watchOS/bitwarden/bitwarden WatchKit Extension/Services/StateService.swift @@ -3,11 +3,14 @@ import Foundation class StateService { static let shared: StateService = StateService() + let HAS_RUN_BEFORE_KEY = "has_run_before_key" let CURRENT_STATE_KEY = "current_state_key" let CURRENT_USER_KEY = "current_user_key" // let TIMEOUT_MINUTES_KEY = "timeout_minutes_key" // let TIMEOUT_ACTION_KEY = "timeout_action_key" + var hasRunBefore = false + private init(){} var currentState:BWState { @@ -40,6 +43,29 @@ class StateService { KeychainHelper.standard.save(user, key: CURRENT_USER_KEY) } + /// Checks the state integrity of the app. If the app has been reinstalled then there are some keychain things that need to be reset. + func checkIntegrity() { + if hasRunBefore { + return + } + + let userDefaults = UserDefaults.standard + hasRunBefore = userDefaults.bool(forKey: HAS_RUN_BEFORE_KEY) + + if !hasRunBefore { + clear() + EnvironmentService.shared.clear() + CryptoService.clearKey() + + userDefaults.set(true, forKey: HAS_RUN_BEFORE_KEY) + } + } + + func clear() { + currentState = .needSetup + setUser(user: nil) + } + // var vaultTimeoutInMinutes: Int? { // guard let timeoutData = KeychainHelper.standard.read(TIMEOUT_MINUTES_KEY), // let strData = String(data: timeoutData, encoding: .utf8), diff --git a/src/watchOS/bitwarden/bitwarden WatchKit Extension/ViewModels/CipherListViewModel.swift b/src/watchOS/bitwarden/bitwarden WatchKit Extension/ViewModels/CipherListViewModel.swift index cd494db92..0cafc784e 100644 --- a/src/watchOS/bitwarden/bitwarden WatchKit Extension/ViewModels/CipherListViewModel.swift +++ b/src/watchOS/bitwarden/bitwarden WatchKit Extension/ViewModels/CipherListViewModel.swift @@ -47,6 +47,8 @@ class CipherListViewModel : ObservableObject { } func checkStateAndFetch(_ state: BWState? = nil) { + StateService.shared.checkIntegrity() + user = StateService.shared.getUser() if user == nil && !watchConnectivityManager.isSessionActivated {