Lock vault and clear data when logging out (#288)

This commit is contained in:
Brian Yencho
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent de6f707964
commit e41e681700
5 changed files with 112 additions and 0 deletions
@@ -202,6 +202,7 @@ class AuthRepositoryImpl constructor(
override fun logout(userId: String) {
val currentUserState = authDiskSource.userState ?: return
val wasActiveUser = userId == activeUserId
// Remove the active user from the accounts map
val updatedAccounts = currentUserState
@@ -228,6 +229,12 @@ class AuthRepositoryImpl constructor(
// Update the user information and log out
authDiskSource.userState = null
}
// Lock the vault for the logged out user
vaultRepository.lockVaultIfNecessary(userId)
// Clear the current vault data if the logged out user was the active one.
if (wasActiveUser) vaultRepository.clearUnlockedData()
}
@Suppress("ReturnCount", "LongMethod")
@@ -52,6 +52,11 @@ interface VaultRepository {
*/
fun getVaultFolderStateFlow(folderId: String): StateFlow<DataState<FolderView?>>
/**
* Locks the vault for the user with the given [userId] if necessary.
*/
fun lockVaultIfNecessary(userId: String)
/**
* Attempt to unlock the vault and sync the vault data for the currently active user.
*/
@@ -155,6 +155,10 @@ class VaultRepositoryImpl constructor(
initialValue = DataState.Loading,
)
override fun lockVaultIfNecessary(userId: String) {
setVaultToLocked(userId = userId)
}
@Suppress("ReturnCount")
override suspend fun unlockVaultAndSyncForCurrentUser(
masterPassword: String,
@@ -233,6 +237,16 @@ class VaultRepositoryImpl constructor(
}
}
// TODO: This is temporary. Eventually this needs to be based on the presence of various
// user keys but this will likely require SDK updates to support this (BIT-1190).
private fun setVaultToLocked(userId: String) {
vaultMutableStateFlow.update {
it.copy(
unlockedVaultUserIds = it.unlockedVaultUserIds - userId,
)
}
}
private fun storeUserKeyAndPrivateKey(
userKey: String?,
privateKey: String?,