Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties

@HiltViewModel
class ProfileSettingsViewModel
@Inject
Expand Down Expand Up @@ -114,15 +121,38 @@ class ProfileSettingsViewModel

activity?.let {
val executor = ContextCompat.getMainExecutor(context)

// Ensure key exists. Safe to call each time.
generateSecretKey()
val cipher = try {
getCipher()
} catch (e: Exception) {
_isBiometricEnabled.value = false
return
}

val biometricPrompt =
BiometricPrompt(
it,
executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
launchCatching {
accountService.updateBiometricEnabled(true)
_isBiometricEnabled.value = true
// Use the cipher to prove authentication!
val cryptoObject = result.cryptoObject
val cipher = cryptoObject?.cipher
if (cipher != null) {
try {
// Encrypt some dummy data as a proof of auth; discard result
cipher.doFinal(ByteArray(16))
launchCatching {
accountService.updateBiometricEnabled(true)
_isBiometricEnabled.value = true
}
} catch (e: Exception) {
_isBiometricEnabled.value = false
}
} else {
_isBiometricEnabled.value = false
}
}

Expand All @@ -147,7 +177,10 @@ class ProfileSettingsViewModel
.setNegativeButtonText(context.getString(R.string.cancel))
.build()

biometricPrompt.authenticate(promptInfo)
biometricPrompt.authenticate(
BiometricPrompt.CryptoObject(cipher),
promptInfo
)
} ?: run {
_isBiometricEnabled.value = false
}
Expand Down
Loading