1package com.peachpasswords.ui.lock23import android.content.Context4import androidx.biometric.BiometricPrompt5import androidx.fragment.app.FragmentActivity6import androidx.lifecycle.ViewModel7import androidx.lifecycle.viewModelScope8import com.peachpasswords.vault.CreateVaultResult9import com.peachpasswords.ui.security.scorePassword10import com.peachpasswords.vault.DuressAction11import com.peachpasswords.vault.DuressConfigStore12import com.peachpasswords.vault.DuressMatch13import com.peachpasswords.vault.UnlockResult14import com.peachpasswords.vault.VaultCrypto15import com.peachpasswords.vault.VaultLockTimer16import com.peachpasswords.vault.VaultRepository17import dagger.hilt.android.lifecycle.HiltViewModel18import dagger.hilt.android.qualifiers.ApplicationContext19import kotlinx.coroutines.flow.MutableStateFlow20import kotlinx.coroutines.flow.StateFlow21import kotlinx.coroutines.flow.asStateFlow22import kotlinx.coroutines.flow.update23import kotlinx.coroutines.launch24import javax.inject.Inject2526data class LockUiState(27 val hasVault: Boolean = false,28 val loading: Boolean = false,29 val error: String? = null,30 val unlocked: Boolean = false,31 32 val created: Boolean = false,33 34 val shakeNonce: Int = 0,35 36 val duressDestroyed: Boolean = false,37)3839const val MIN_PASSWORD_LENGTH = 12404142const val MIN_CREATE_STRENGTH = 24344@HiltViewModel45class LockViewModel @Inject constructor(46 private val vault: VaultRepository,47 private val duress: DuressConfigStore,48 private val lockTimer: VaultLockTimer,49 @ApplicationContext private val appContext: Context,50) : ViewModel() {51 private val _state = MutableStateFlow(LockUiState(hasVault = vault.hasVault()))52 val state: StateFlow<LockUiState> = _state.asStateFlow()5354 fun unlock(password: String) {55 if (password.isBlank() || _state.value.loading) return56 _state.update { it.copy(loading = true, error = null) }57 viewModelScope.launch {58 59 when (val d = duress.verify(password)) {60 is DuressMatch.Match -> { handleDuress(d.action); return@launch }61 DuressMatch.None -> Unit62 }63 when (val r = vault.unlock(password)) {64 is UnlockResult.Success -> { lockTimer.start(); _state.update { it.copy(loading = false, unlocked = true) } }65 is UnlockResult.WrongPassword -> _state.update {66 it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)67 }68 is UnlockResult.Error -> _state.update {69 it.copy(loading = false, error = r.reason, shakeNonce = it.shakeNonce + 1)70 }71 }72 }73 }7475 76777879 private suspend fun handleDuress(action: DuressAction) {80 when (action) {81 DuressAction.DECOY -> when (vault.unlockDecoy()) {82 is UnlockResult.Success -> { lockTimer.start(); _state.update { it.copy(loading = false, unlocked = true) } }83 else -> _state.update {84 it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)85 }86 }87 DuressAction.DESTROY -> {88 vault.destroyVault()89 duress.clear()90 _state.update { it.copy(loading = false, hasVault = false, duressDestroyed = true) }91 }92 }93 }9495 fun create(password: String, confirm: String) {96 if (_state.value.loading) return97 when {98 password.length < MIN_PASSWORD_LENGTH ->99 return _state.update { it.copy(error = "password must be at least $MIN_PASSWORD_LENGTH characters") }100 scorePassword(password) < MIN_CREATE_STRENGTH ->101 return _state.update { it.copy(error = "Password too weak — try a longer or more varied password") }102 password != confirm ->103 return _state.update { it.copy(error = "passwords do not match") }104 }105 _state.update { it.copy(loading = true, error = null) }106 viewModelScope.launch {107 when (val r = vault.createVault(password)) {108 is CreateVaultResult.Success -> { lockTimer.start(); _state.update { it.copy(loading = false, unlocked = true, created = true) } }109 is CreateVaultResult.Error -> _state.update { it.copy(loading = false, error = r.reason) }110 }111 }112 }113114 fun clearError() = _state.update { it.copy(error = null) }115116 117118119120121 fun canTryBiometric(): Boolean =122 vault.hasVault() &&123 Biometrics.strongAvailable(appContext) &&124 BiometricKeyStore.hasWrappedKey(appContext)125126 127128129130131 fun biometricUnlock(activity: FragmentActivity) {132 if (_state.value.loading) return133 val cipher = BiometricKeyStore.decryptCipher(appContext) ?: return134 Biometrics.authenticateCrypto(135 activity = activity,136 title = "Unlock Peach",137 subtitle = "Use your biometric to unlock your vault",138 crypto = BiometricPrompt.CryptoObject(cipher),139 onSuccess = { result ->140 val used = result.cryptoObject?.cipher ?: return@authenticateCrypto141 val key = BiometricKeyStore.unwrap(appContext, used) ?: return@authenticateCrypto142 viewModelScope.launch {143 val r = vault.unlockWithKey(key)144 VaultCrypto.zero(key)145 when (r) {146 is UnlockResult.Success -> { lockTimer.start(); _state.update { it.copy(unlocked = true) } }147 else -> _state.update { it.copy(error = "biometric unlock failed", shakeNonce = it.shakeNonce + 1) }148 }149 }150 },151 )152 }153}