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.VaultRepository16import dagger.hilt.android.lifecycle.HiltViewModel17import dagger.hilt.android.qualifiers.ApplicationContext18import kotlinx.coroutines.flow.MutableStateFlow19import kotlinx.coroutines.flow.StateFlow20import kotlinx.coroutines.flow.asStateFlow21import kotlinx.coroutines.flow.update22import kotlinx.coroutines.launch23import javax.inject.Inject2425data class LockUiState(26 val hasVault: Boolean = false,27 val loading: Boolean = false,28 val error: String? = null,29 val unlocked: Boolean = false,30 31 val created: Boolean = false,32 33 val shakeNonce: Int = 0,34 35 val duressDestroyed: Boolean = false,36)3738const val MIN_PASSWORD_LENGTH = 12394041const val MIN_CREATE_STRENGTH = 24243@HiltViewModel44class LockViewModel @Inject constructor(45 private val vault: VaultRepository,46 private val duress: DuressConfigStore,47 @ApplicationContext private val appContext: Context,48) : ViewModel() {49 private val _state = MutableStateFlow(LockUiState(hasVault = vault.hasVault()))50 val state: StateFlow<LockUiState> = _state.asStateFlow()5152 fun unlock(password: String) {53 if (password.isBlank() || _state.value.loading) return54 _state.update { it.copy(loading = true, error = null) }55 viewModelScope.launch {56 57 when (val d = duress.verify(password)) {58 is DuressMatch.Match -> { handleDuress(d.action); return@launch }59 DuressMatch.None -> Unit60 }61 when (val r = vault.unlock(password)) {62 is UnlockResult.Success -> _state.update { it.copy(loading = false, unlocked = true) }63 is UnlockResult.WrongPassword -> _state.update {64 it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)65 }66 is UnlockResult.Error -> _state.update {67 it.copy(loading = false, error = r.reason, shakeNonce = it.shakeNonce + 1)68 }69 }70 }71 }7273 74757677 private suspend fun handleDuress(action: DuressAction) {78 when (action) {79 DuressAction.DECOY -> when (vault.unlockDecoy()) {80 is UnlockResult.Success -> _state.update { it.copy(loading = false, unlocked = true) }81 else -> _state.update {82 it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)83 }84 }85 DuressAction.DESTROY -> {86 vault.destroyVault()87 duress.clear()88 _state.update { it.copy(loading = false, hasVault = false, duressDestroyed = true) }89 }90 }91 }9293 fun create(password: String, confirm: String) {94 if (_state.value.loading) return95 when {96 password.length < MIN_PASSWORD_LENGTH ->97 return _state.update { it.copy(error = "password must be at least $MIN_PASSWORD_LENGTH characters") }98 scorePassword(password) < MIN_CREATE_STRENGTH ->99 return _state.update { it.copy(error = "Password too weak — try a longer or more varied password") }100 password != confirm ->101 return _state.update { it.copy(error = "passwords do not match") }102 }103 _state.update { it.copy(loading = true, error = null) }104 viewModelScope.launch {105 when (val r = vault.createVault(password)) {106 is CreateVaultResult.Success -> _state.update { it.copy(loading = false, unlocked = true, created = true) }107 is CreateVaultResult.Error -> _state.update { it.copy(loading = false, error = r.reason) }108 }109 }110 }111112 fun clearError() = _state.update { it.copy(error = null) }113114 115116117118119 fun canTryBiometric(): Boolean =120 vault.hasVault() &&121 Biometrics.strongAvailable(appContext) &&122 BiometricKeyStore.hasWrappedKey(appContext)123124 125126127128129 fun biometricUnlock(activity: FragmentActivity) {130 if (_state.value.loading) return131 val cipher = BiometricKeyStore.decryptCipher(appContext) ?: return132 Biometrics.authenticateCrypto(133 activity = activity,134 title = "Unlock Peach",135 subtitle = "Use your biometric to unlock your vault",136 crypto = BiometricPrompt.CryptoObject(cipher),137 onSuccess = { result ->138 val used = result.cryptoObject?.cipher ?: return@authenticateCrypto139 val key = BiometricKeyStore.unwrap(appContext, used) ?: return@authenticateCrypto140 viewModelScope.launch {141 val r = vault.unlockWithKey(key)142 VaultCrypto.zero(key)143 when (r) {144 is UnlockResult.Success -> _state.update { it.copy(unlocked = true) }145 else -> _state.update { it.copy(error = "biometric unlock failed", shakeNonce = it.shakeNonce + 1) }146 }147 }148 },149 )150 }151}