feat: Implement BiometricAuthManager for biometric authentication and password encryption/decryption
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.rosetta.messenger.biometric
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
private val Context.biometricDataStore: DataStore<Preferences> by preferencesDataStore(name = "biometric_prefs")
|
||||
|
||||
/**
|
||||
* Управление настройками и данными биометрической аутентификации
|
||||
*/
|
||||
class BiometricPreferences(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
private val BIOMETRIC_ENABLED = booleanPreferencesKey("biometric_enabled")
|
||||
private val ENCRYPTED_PASSWORD_PREFIX = "encrypted_password_"
|
||||
}
|
||||
|
||||
/**
|
||||
* Включена ли биометрическая аутентификация
|
||||
*/
|
||||
val isBiometricEnabled: Flow<Boolean> = context.biometricDataStore.data
|
||||
.map { preferences ->
|
||||
preferences[BIOMETRIC_ENABLED] ?: false
|
||||
}
|
||||
|
||||
/**
|
||||
* Включить биометрическую аутентификацию
|
||||
*/
|
||||
suspend fun enableBiometric() {
|
||||
context.biometricDataStore.edit { preferences ->
|
||||
preferences[BIOMETRIC_ENABLED] = true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Отключить биометрическую аутентификацию
|
||||
*/
|
||||
suspend fun disableBiometric() {
|
||||
context.biometricDataStore.edit { preferences ->
|
||||
preferences[BIOMETRIC_ENABLED] = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Сохранить зашифрованный пароль для аккаунта
|
||||
*/
|
||||
suspend fun saveEncryptedPassword(publicKey: String, encryptedPassword: String) {
|
||||
val key = stringPreferencesKey("$ENCRYPTED_PASSWORD_PREFIX$publicKey")
|
||||
context.biometricDataStore.edit { preferences ->
|
||||
preferences[key] = encryptedPassword
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить зашифрованный пароль для аккаунта
|
||||
*/
|
||||
suspend fun getEncryptedPassword(publicKey: String): String? {
|
||||
val key = stringPreferencesKey("$ENCRYPTED_PASSWORD_PREFIX$publicKey")
|
||||
return context.biometricDataStore.data.first()[key]
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить зашифрованный пароль для аккаунта
|
||||
*/
|
||||
suspend fun removeEncryptedPassword(publicKey: String) {
|
||||
val key = stringPreferencesKey("$ENCRYPTED_PASSWORD_PREFIX$publicKey")
|
||||
context.biometricDataStore.edit { preferences ->
|
||||
preferences.remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить все биометрические данные
|
||||
*/
|
||||
suspend fun clearAll() {
|
||||
context.biometricDataStore.edit { preferences ->
|
||||
preferences.clear()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверить, есть ли сохраненный зашифрованный пароль для аккаунта
|
||||
*/
|
||||
suspend fun hasEncryptedPassword(publicKey: String): Boolean {
|
||||
return getEncryptedPassword(publicKey) != null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user