From 555ddace308dba938047ec29ff8182f0f8be8414 Mon Sep 17 00:00:00 2001 From: Yogesh Choudhary Paliyal Date: Sun, 23 Jul 2023 11:16:57 +0530 Subject: [PATCH] Added support for Encrypted database, migration from non-encrypted from encrypted DB (#626) * security in db * feat: add sqlite cipher * feat: added support for db encryption * feat: run spotless --- .../ui/nav/DashboardComposeActivity.kt | 13 ++-- app/src/main/res/layout/activity_crash.xml | 17 +++-- common/build.gradle.kts | 5 +- common/proguard-rules.pro | 5 +- .../com/yogeshpaliyal/common/AppDatabase.kt | 3 +- .../yogeshpaliyal/common/data/UserSettings.kt | 1 + .../common/di/module/AppModule.kt | 67 +++++++++++++++++-- .../common/utils/SharedPreferenceUtils.kt | 6 ++ .../common/utils/UserSettingsDataStore.kt | 7 +- .../common/utils/UserSettingsSerializer.kt | 3 - .../utils/UserSettingsSerializerLegacy.kt | 40 ----------- .../common/worker/AutoBackupWorker.kt | 2 +- 12 files changed, 100 insertions(+), 69 deletions(-) delete mode 100644 common/src/main/java/com/yogeshpaliyal/common/utils/UserSettingsSerializerLegacy.kt diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt b/app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt index 37c7c709..5d48e860 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt @@ -148,7 +148,7 @@ fun Dashboard() { Surface(modifier = Modifier.padding(paddingValues)) { CurrentPage() - OptionBottomBar() + BottomSheet() } } } @@ -195,15 +195,20 @@ fun CurrentPage() { } @Composable -fun OptionBottomBar( - viewModel: BottomNavViewModel = androidx.lifecycle.viewmodel.compose.viewModel() -) { +fun BottomSheet() { val bottomSheetState by selectState { this.bottomSheet } if (bottomSheetState?.isBottomSheetOpen != true) { return } + OptionBottomBar() +} + +@Composable +fun OptionBottomBar( + viewModel: BottomNavViewModel = androidx.lifecycle.viewmodel.compose.viewModel() +) { val dispatchAction = rememberDispatcher() val navigationItems by viewModel.navigationList.observeAsState() diff --git a/app/src/main/res/layout/activity_crash.xml b/app/src/main/res/layout/activity_crash.xml index 4d8b0c7e..367042f3 100644 --- a/app/src/main/res/layout/activity_crash.xml +++ b/app/src/main/res/layout/activity_crash.xml @@ -12,18 +12,25 @@ android:layout_height="match_parent" tools:context=".ui.CrashActivity"> - + + + + { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - UserSettingsSerializer(CryptoManager()) - } else { - UserSettingsSerializerLegacy(context) - } + return UserSettingsSerializer(CryptoManager()) } private val Context.dataStoreSerializer by dataStore( diff --git a/common/src/main/java/com/yogeshpaliyal/common/utils/UserSettingsSerializer.kt b/common/src/main/java/com/yogeshpaliyal/common/utils/UserSettingsSerializer.kt index 8debe076..e87df356 100644 --- a/common/src/main/java/com/yogeshpaliyal/common/utils/UserSettingsSerializer.kt +++ b/common/src/main/java/com/yogeshpaliyal/common/utils/UserSettingsSerializer.kt @@ -1,7 +1,5 @@ package com.yogeshpaliyal.common.utils -import android.os.Build -import androidx.annotation.RequiresApi import androidx.datastore.core.Serializer import com.yogeshpaliyal.common.data.UserSettings import kotlinx.serialization.SerializationException @@ -9,7 +7,6 @@ import kotlinx.serialization.json.Json import java.io.InputStream import java.io.OutputStream -@RequiresApi(Build.VERSION_CODES.M) class UserSettingsSerializer( private val cryptoManager: CryptoManager ) : Serializer { diff --git a/common/src/main/java/com/yogeshpaliyal/common/utils/UserSettingsSerializerLegacy.kt b/common/src/main/java/com/yogeshpaliyal/common/utils/UserSettingsSerializerLegacy.kt deleted file mode 100644 index 525645d5..00000000 --- a/common/src/main/java/com/yogeshpaliyal/common/utils/UserSettingsSerializerLegacy.kt +++ /dev/null @@ -1,40 +0,0 @@ -package com.yogeshpaliyal.common.utils - -import android.content.Context -import androidx.datastore.core.Serializer -import com.yogeshpaliyal.common.data.DEFAULT_PASSWORD_LENGTH -import com.yogeshpaliyal.common.data.UserSettings -import java.io.InputStream -import java.io.OutputStream - -class UserSettingsSerializerLegacy constructor( - private val applicationContext: Context -) : Serializer { - - override val defaultValue: UserSettings - get() = UserSettings() - - override suspend fun readFrom(input: InputStream): UserSettings { - return UserSettings( - autoBackupEnable = applicationContext.isAutoBackupEnabledLegacy(), - backupDirectory = applicationContext.getBackupDirectoryLegacy(), - backupTime = applicationContext.getBackupTimeLegacy(), - defaultPasswordLength = applicationContext.getKeyPassPasswordLengthLegacy() ?: DEFAULT_PASSWORD_LENGTH, - isBiometricEnable = applicationContext.isBiometricEnableLegacy(), - keyPassPassword = applicationContext.getKeyPassPasswordLegacy(), - overrideAutoBackup = applicationContext.overrideAutoBackupLegacy(), - backupKey = applicationContext.getOrCreateBackupKey(false).second - ) - } - - override suspend fun writeTo(t: UserSettings, output: OutputStream) { - applicationContext.setBiometricEnableLegacy(t.isBiometricEnable) - applicationContext.setAutoBackupEnabledLegacy(t.autoBackupEnable) - applicationContext.setBackupDirectoryLegacy(t.backupDirectory ?: "") - applicationContext.setBackupTimeLegacy(t.backupTime ?: 0L) - applicationContext.setKeyPassPasswordLengthLegacy(t.defaultPasswordLength) - applicationContext.setKeyPassPasswordLegacy(t.keyPassPassword) - applicationContext.setOverrideAutoBackupLegacy(t.overrideAutoBackup) - applicationContext.saveKeyphraseLegacy(t.backupKey ?: "") - } -} diff --git a/common/src/main/java/com/yogeshpaliyal/common/worker/AutoBackupWorker.kt b/common/src/main/java/com/yogeshpaliyal/common/worker/AutoBackupWorker.kt index f3a6fe71..05d29fe8 100644 --- a/common/src/main/java/com/yogeshpaliyal/common/worker/AutoBackupWorker.kt +++ b/common/src/main/java/com/yogeshpaliyal/common/worker/AutoBackupWorker.kt @@ -24,7 +24,7 @@ class AutoBackupWorker @AssistedInject constructor( override suspend fun doWork(): Result { return withContext(Dispatchers.IO) { if (appContext.canUserAccessBackupDirectory()) { - val userSettings = appContext.getUserSettings() ?: return@withContext Result.failure() + val userSettings = appContext.getUserSettings() val selectedDirectory = Uri.parse(userSettings.backupDirectory) appContext.backupAccounts( appDatabase,