From 93254c1d2193f32fd9c78067b82651ef459e1468 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 13 Oct 2020 17:08:54 +0200 Subject: [PATCH] Implemented persisting new password only if changed database password has been successful --- .../banking/persistence/RoomBankingPersistence.kt | 8 +++++--- .../banking/persistence/BankingPersistenceJson.kt | 3 ++- .../android/authentication/AuthenticationService.kt | 11 ++++++----- .../banking/persistence/IBankingPersistence.kt | 2 +- .../banking/persistence/NoOpBankingPersistence.kt | 4 ++-- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt b/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt index a1e8e3c9..8d8115af 100644 --- a/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt +++ b/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt @@ -41,12 +41,14 @@ open class RoomBankingPersistence(protected open val applicationContext: Context return openDatabase(password) } - override fun changePassword(newPassword: String?) { + override fun changePassword(newPassword: String?): Boolean { if (this::database.isInitialized) { - database.query("PRAGMA rekey = '$newPassword';", emptyArray()) + val cursor = database.query("PRAGMA rekey = '$newPassword';", emptyArray()) + + return cursor.count == 1 // TODO: also check if first column content is 'ok' ? } else { // database hasn't been opened yet, that means we're on the first app run - openDatabase(newPassword) + return openDatabase(newPassword) } } diff --git a/persistence/json/BankingPersistenceJson/src/main/kotlin/net/dankito/banking/persistence/BankingPersistenceJson.kt b/persistence/json/BankingPersistenceJson/src/main/kotlin/net/dankito/banking/persistence/BankingPersistenceJson.kt index 22947d19..ff503b12 100644 --- a/persistence/json/BankingPersistenceJson/src/main/kotlin/net/dankito/banking/persistence/BankingPersistenceJson.kt +++ b/persistence/json/BankingPersistenceJson/src/main/kotlin/net/dankito/banking/persistence/BankingPersistenceJson.kt @@ -40,8 +40,9 @@ open class BankingPersistenceJson( return true } - override fun changePassword(newPassword: String?) { + override fun changePassword(newPassword: String?): Boolean { // TODO: may implement data decryption. But then we have to store newPassword to be able to encrypt data + return true } diff --git a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/authentication/AuthenticationService.kt b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/authentication/AuthenticationService.kt index 119e3fe1..a6c0d26f 100644 --- a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/authentication/AuthenticationService.kt +++ b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/authentication/AuthenticationService.kt @@ -173,12 +173,13 @@ open class AuthenticationService( settings.salt = encodeToBase64(salt) } - if (saveAuthenticationSettings(settings)) { - this.authenticationType = type - this.encryptionCipherForBiometric = null + if (persistence.changePassword(newPassword)) { + if (saveAuthenticationSettings(settings)) { + this.authenticationType = type + this.encryptionCipherForBiometric = null - persistence.changePassword(newPassword) // TODO: actually this is bad. If changing password fails then password is saved in AuthenticationSettings but DB has a different password - return true + return true + } } return false diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/IBankingPersistence.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/IBankingPersistence.kt index 778c67bb..793067c0 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/IBankingPersistence.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/IBankingPersistence.kt @@ -8,7 +8,7 @@ interface IBankingPersistence { fun decryptData(password: String?): Boolean - fun changePassword(newPassword: String?) + fun changePassword(newPassword: String?): Boolean fun saveOrUpdateBank(bank: TypedBankData, allBanks: List) diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/NoOpBankingPersistence.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/NoOpBankingPersistence.kt index 9b2718cc..da816fda 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/NoOpBankingPersistence.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/NoOpBankingPersistence.kt @@ -10,8 +10,8 @@ open class NoOpBankingPersistence : IBankingPersistence { return true } - override fun changePassword(newPassword: String?) { - + override fun changePassword(newPassword: String?): Boolean { + return true }