|
| 1 | +package com.x8bit.bitwarden.data.auth.manager |
| 2 | + |
| 3 | +import com.bitwarden.data.manager.DispatcherManager |
| 4 | +import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource |
| 5 | +import com.x8bit.bitwarden.data.auth.datasource.disk.model.OnboardingStatus |
| 6 | +import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson |
| 7 | +import com.x8bit.bitwarden.data.auth.repository.model.UserAccountTokens |
| 8 | +import com.x8bit.bitwarden.data.auth.repository.model.UserKeyConnectorState |
| 9 | +import com.x8bit.bitwarden.data.auth.repository.model.UserOrganizations |
| 10 | +import com.x8bit.bitwarden.data.auth.repository.model.UserState |
| 11 | +import com.x8bit.bitwarden.data.auth.repository.model.VaultUnlockType |
| 12 | +import com.x8bit.bitwarden.data.auth.repository.util.currentOnboardingStatus |
| 13 | +import com.x8bit.bitwarden.data.auth.repository.util.onboardingStatusChangesFlow |
| 14 | +import com.x8bit.bitwarden.data.auth.repository.util.toUserState |
| 15 | +import com.x8bit.bitwarden.data.auth.repository.util.userAccountTokens |
| 16 | +import com.x8bit.bitwarden.data.auth.repository.util.userAccountTokensFlow |
| 17 | +import com.x8bit.bitwarden.data.auth.repository.util.userKeyConnectorStateFlow |
| 18 | +import com.x8bit.bitwarden.data.auth.repository.util.userKeyConnectorStateList |
| 19 | +import com.x8bit.bitwarden.data.auth.repository.util.userOrganizationsList |
| 20 | +import com.x8bit.bitwarden.data.auth.repository.util.userOrganizationsListFlow |
| 21 | +import com.x8bit.bitwarden.data.platform.manager.FirstTimeActionManager |
| 22 | +import com.x8bit.bitwarden.data.platform.manager.model.FirstTimeState |
| 23 | +import com.x8bit.bitwarden.data.vault.manager.VaultLockManager |
| 24 | +import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockData |
| 25 | +import kotlinx.coroutines.CoroutineScope |
| 26 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 27 | +import kotlinx.coroutines.flow.SharingStarted |
| 28 | +import kotlinx.coroutines.flow.StateFlow |
| 29 | +import kotlinx.coroutines.flow.combine |
| 30 | +import kotlinx.coroutines.flow.filterNot |
| 31 | +import kotlinx.coroutines.flow.merge |
| 32 | +import kotlinx.coroutines.flow.stateIn |
| 33 | +import kotlinx.coroutines.flow.update |
| 34 | + |
| 35 | +/** |
| 36 | + * The default implementation of the [UserStateManager]. |
| 37 | + */ |
| 38 | +class UserStateManagerImpl( |
| 39 | + private val authDiskSource: AuthDiskSource, |
| 40 | + firstTimeActionManager: FirstTimeActionManager, |
| 41 | + vaultLockManager: VaultLockManager, |
| 42 | + dispatcherManager: DispatcherManager, |
| 43 | +) : UserStateManager { |
| 44 | + private val unconfinedScope = CoroutineScope(dispatcherManager.unconfined) |
| 45 | + |
| 46 | + //region Pending Account Addition |
| 47 | + private val mutableHasPendingAccountAdditionStateFlow = MutableStateFlow(value = false) |
| 48 | + |
| 49 | + override val hasPendingAccountAdditionStateFlow: StateFlow<Boolean> |
| 50 | + get() = mutableHasPendingAccountAdditionStateFlow |
| 51 | + |
| 52 | + override var hasPendingAccountAddition: Boolean |
| 53 | + by mutableHasPendingAccountAdditionStateFlow::value |
| 54 | + //endregion Pending Account Addition |
| 55 | + |
| 56 | + //region Pending Account Deletion |
| 57 | + /** |
| 58 | + * If there is a pending account deletion, continue showing the original UserState until it |
| 59 | + * is confirmed. This is accomplished by blocking the emissions of the [userStateFlow] |
| 60 | + * whenever set to `true`. |
| 61 | + */ |
| 62 | + private val mutableHasPendingAccountDeletionStateFlow = MutableStateFlow(value = false) |
| 63 | + |
| 64 | + override var hasPendingAccountDeletion: Boolean |
| 65 | + by mutableHasPendingAccountDeletionStateFlow::value |
| 66 | + //endregion Pending Account Deletion |
| 67 | + |
| 68 | + /** |
| 69 | + * Whenever a function needs to update multiple underlying data-points that contribute to the |
| 70 | + * [UserState], we update this [MutableStateFlow] and continue to show the original `UserState` |
| 71 | + * until the transaction is complete. This is accomplished by blocking the emissions of the |
| 72 | + * [userStateFlow] whenever this is set to a value above 0 (a count is used if more than one |
| 73 | + * process is updating data simultaneously). |
| 74 | + */ |
| 75 | + private val mutableUserStateTransactionCountStateFlow = MutableStateFlow(0) |
| 76 | + |
| 77 | + @Suppress("UNCHECKED_CAST", "MagicNumber") |
| 78 | + override val userStateFlow: StateFlow<UserState?> = combine( |
| 79 | + authDiskSource.userStateFlow, |
| 80 | + authDiskSource.userAccountTokensFlow, |
| 81 | + authDiskSource.userOrganizationsListFlow, |
| 82 | + authDiskSource.userKeyConnectorStateFlow, |
| 83 | + authDiskSource.onboardingStatusChangesFlow, |
| 84 | + firstTimeActionManager.firstTimeStateFlow, |
| 85 | + vaultLockManager.vaultUnlockDataStateFlow, |
| 86 | + hasPendingAccountAdditionStateFlow, |
| 87 | + // Ignore the data in the merge, but trigger an update when they emit. |
| 88 | + merge( |
| 89 | + mutableHasPendingAccountDeletionStateFlow, |
| 90 | + mutableUserStateTransactionCountStateFlow, |
| 91 | + vaultLockManager.isActiveUserUnlockingFlow, |
| 92 | + ), |
| 93 | + ) { array -> |
| 94 | + val userStateJson = array[0] as UserStateJson? |
| 95 | + val userAccountTokens = array[1] as List<UserAccountTokens> |
| 96 | + val userOrganizationsList = array[2] as List<UserOrganizations> |
| 97 | + val userIsUsingKeyConnectorList = array[3] as List<UserKeyConnectorState> |
| 98 | + val onboardingStatus = array[4] as OnboardingStatus? |
| 99 | + val firstTimeState = array[5] as FirstTimeState |
| 100 | + val vaultState = array[6] as List<VaultUnlockData> |
| 101 | + val hasPendingAccountAddition = array[7] as Boolean |
| 102 | + userStateJson?.toUserState( |
| 103 | + vaultState = vaultState, |
| 104 | + userAccountTokens = userAccountTokens, |
| 105 | + userOrganizationsList = userOrganizationsList, |
| 106 | + userIsUsingKeyConnectorList = userIsUsingKeyConnectorList, |
| 107 | + hasPendingAccountAddition = hasPendingAccountAddition, |
| 108 | + onboardingStatus = onboardingStatus, |
| 109 | + isBiometricsEnabledProvider = ::isBiometricsEnabled, |
| 110 | + vaultUnlockTypeProvider = ::getVaultUnlockType, |
| 111 | + isDeviceTrustedProvider = ::isDeviceTrusted, |
| 112 | + firstTimeState = firstTimeState, |
| 113 | + ) |
| 114 | + } |
| 115 | + .filterNot { |
| 116 | + mutableHasPendingAccountDeletionStateFlow.value || |
| 117 | + mutableUserStateTransactionCountStateFlow.value > 0 || |
| 118 | + vaultLockManager.isActiveUserUnlockingFlow.value |
| 119 | + } |
| 120 | + .stateIn( |
| 121 | + scope = unconfinedScope, |
| 122 | + started = SharingStarted.Eagerly, |
| 123 | + initialValue = authDiskSource |
| 124 | + .userState |
| 125 | + ?.toUserState( |
| 126 | + vaultState = vaultLockManager.vaultUnlockDataStateFlow.value, |
| 127 | + userAccountTokens = authDiskSource.userAccountTokens, |
| 128 | + userOrganizationsList = authDiskSource.userOrganizationsList, |
| 129 | + userIsUsingKeyConnectorList = authDiskSource.userKeyConnectorStateList, |
| 130 | + hasPendingAccountAddition = mutableHasPendingAccountAdditionStateFlow.value, |
| 131 | + onboardingStatus = authDiskSource.currentOnboardingStatus, |
| 132 | + isBiometricsEnabledProvider = ::isBiometricsEnabled, |
| 133 | + vaultUnlockTypeProvider = ::getVaultUnlockType, |
| 134 | + isDeviceTrustedProvider = ::isDeviceTrusted, |
| 135 | + firstTimeState = firstTimeActionManager.currentOrDefaultUserFirstTimeState, |
| 136 | + ), |
| 137 | + ) |
| 138 | + |
| 139 | + override suspend fun <T> userStateTransaction(block: suspend () -> T): T { |
| 140 | + mutableUserStateTransactionCountStateFlow.update { it.inc() } |
| 141 | + return try { |
| 142 | + block() |
| 143 | + } finally { |
| 144 | + mutableUserStateTransactionCountStateFlow.update { it.dec() } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private fun isBiometricsEnabled( |
| 149 | + userId: String, |
| 150 | + ): Boolean = authDiskSource.getUserBiometricUnlockKey(userId = userId) != null |
| 151 | + |
| 152 | + private fun isDeviceTrusted( |
| 153 | + userId: String, |
| 154 | + ): Boolean = authDiskSource.getDeviceKey(userId = userId) != null |
| 155 | + |
| 156 | + private fun getVaultUnlockType( |
| 157 | + userId: String, |
| 158 | + ): VaultUnlockType = authDiskSource |
| 159 | + .getPinProtectedUserKey(userId = userId) |
| 160 | + ?.let { VaultUnlockType.PIN } |
| 161 | + ?: VaultUnlockType.MASTER_PASSWORD |
| 162 | +} |
0 commit comments