Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
import Foundation
import Security
import VaultUtils
/// Extension for the VaultStore class to handle cache management
extension VaultStore {
/// Clear the memory - remove the Account Key and decrypted database from memory
public func clearCache() {
print("Clearing cache - removing Account Key and decrypted database from memory")
self.accountKey = nil
self.dbConnection = nil
clearLastSuccessfulAuth()
}
/// Clear the vault for a forced logout (e.g. 401, token revocation), keeping only the username for the login prefill.
public func clearSession() throws {
let username = getUsername()
try clearVault()
if let username {
setUsername(username)
}
}
/// Clear the vault storage - remove the encryption key and encrypted database from the device
/// This is used for user-initiated logout where they explicitly choose to clear all local data.
public func clearVault() throws {
print("Clearing vault - removing all stored data")
do {
try removeKeyFromKeychain()
print("Successfully removed encryption key from keychain")
} catch {
print("Failed to remove encryption key from keychain: \(error)")
}
do {
try removeEncryptedDatabase()
print("Successfully removed encrypted database file")
} catch {
print("Failed to remove encrypted database file: \(error)")
throw error
}
// Clear the UserDefaults to remove all locally persisted data
self.userDefaults.removeObject(forKey: VaultConstants.vaultMetadataKey)
self.userDefaults.removeObject(forKey: VaultConstants.authMethodsKey)
self.userDefaults.removeObject(forKey: VaultConstants.autoLockTimeoutKey)
self.userDefaults.removeObject(forKey: VaultConstants.unlockKeyDerivationParamsKey)
self.userDefaults.removeObject(forKey: VaultConstants.accountKeyChainKey)
self.userDefaults.removeObject(forKey: VaultConstants.usernameKey)
self.userDefaults.removeObject(forKey: VaultConstants.offlineModeKey)
self.userDefaults.removeObject(forKey: VaultConstants.pinEnabledKey)
self.userDefaults.removeObject(forKey: VaultConstants.serverVersionKey)
self.userDefaults.removeObject(forKey: VaultConstants.capabilitiesKey)
// Clear sync state
self.userDefaults.removeObject(forKey: VaultConstants.isDirtyKey)
self.userDefaults.removeObject(forKey: VaultConstants.mutationSequenceKey)
self.userDefaults.removeObject(forKey: VaultConstants.isSyncingKey)
VaultSyncEngine.clearPersistedState(in: self.userDefaults)
// Clear WebApiService keys
self.userDefaults.removeObject(forKey: "accessToken")
self.userDefaults.removeObject(forKey: "refreshToken")
self.userDefaults.synchronize()
print("Cleared UserDefaults")
// Clear the cache to remove all in-memory data
self.accountKey = nil
self.dbConnection = nil
self.enabledAuthMethods = []
self.autoLockTimeout = VaultConstants.defaultAutoLockTimeout
self.keyDerivationParams = nil
clearLastSuccessfulAuth()
}
/// Clear engine state.
public func clearSyncEngineState() {
VaultSyncEngine.clearPersistedState(in: self.userDefaults)
storeAccountKeyChain(nil)
}
/// Set the auto-lock timeout - the number of seconds after which the vault will be locked automatically
public func setAutoLockTimeout(_ timeout: Int) {
print("Setting auto-lock timeout to \(timeout) seconds")
self.autoLockTimeout = timeout
self.userDefaults.set(timeout, forKey: VaultConstants.autoLockTimeoutKey)
self.userDefaults.synchronize()
}
/// Get the auto-lock timeout - the number of seconds after which the vault will be locked automatically
public func getAutoLockTimeout() -> Int {
return self.autoLockTimeout
}
}