Implemented that in case of AuthenticationType.Password as database password <user_password> + "_" + <default_password> is used

This commit is contained in:
dankito 2020-10-13 17:26:58 +02:00
parent af67f4d404
commit bc853b03e3
1 changed files with 22 additions and 16 deletions

View File

@ -56,7 +56,9 @@ open class AuthenticationService(
open fun authenticateUserWithPassword(enteredPassword: String): Boolean { open fun authenticateUserWithPassword(enteredPassword: String): Boolean {
if (isCorrectUserPassword(enteredPassword)) { if (isCorrectUserPassword(enteredPassword)) {
return openDatabase(enteredPassword) loadAuthenticationSettings()?.let { settings ->
return openDatabase(settings, enteredPassword)
}
} }
return false return false
@ -117,19 +119,24 @@ open class AuthenticationService(
?: run { result(false) } ?: run { result(false) }
} }
protected open fun openDatabase(settings: AuthenticationSettings) { protected open fun openDatabase(settings: AuthenticationSettings, userPassword: String? = null): Boolean {
if (settings.type == AuthenticationType.None) { settings.defaultPassword?.let { encryptedPassword ->
settings.defaultPassword?.let { encryptedPassword -> settings.initializationVector?.let { iv ->
settings.initializationVector?.let { iv -> settings.salt?.let { salt ->
settings.salt?.let { salt -> val defaultPassword = cryptographyManager.decryptDataWithPbe(decodeFromBase64(encryptedPassword), DefaultPasswordEncryptionKey,
val decrypted = cryptographyManager.decryptDataWithPbe(decodeFromBase64(encryptedPassword), DefaultPasswordEncryptionKey, decodeFromBase64(iv), decodeFromBase64(salt))
decodeFromBase64(iv), decodeFromBase64(salt))
openDatabase(decrypted) if (userPassword != null) {
return openDatabase(userPassword + "_" + defaultPassword)
}
else {
return openDatabase(defaultPassword)
} }
} }
} }
} }
return false
} }
protected open fun openDatabase(password: String?): Boolean { protected open fun openDatabase(password: String?): Boolean {
@ -167,18 +174,17 @@ open class AuthenticationService(
settings.initializationVector = encodeToBase64(encryptionCipher.iv) settings.initializationVector = encodeToBase64(encryptionCipher.iv)
} }
} }
else if (type == AuthenticationType.Password) { else {
if (newUserPassword != null) {
settings.hashedUserPassword = BCrypt.withDefaults().hashToString(12, newUserPassword.toCharArray())
newDatabasePassword = newUserPassword
}
}
else if (type == AuthenticationType.None) {
val salt = cryptographyManager.generateRandomBytes(8) val salt = cryptographyManager.generateRandomBytes(8)
val (encryptedPassword, iv) = cryptographyManager.encryptDataWithPbe(newDefaultPassword, DefaultPasswordEncryptionKey, salt) val (encryptedPassword, iv) = cryptographyManager.encryptDataWithPbe(newDefaultPassword, DefaultPasswordEncryptionKey, salt)
settings.defaultPassword = encodeToBase64(encryptedPassword) settings.defaultPassword = encodeToBase64(encryptedPassword)
settings.initializationVector = encodeToBase64(iv) settings.initializationVector = encodeToBase64(iv)
settings.salt = encodeToBase64(salt) settings.salt = encodeToBase64(salt)
if (newUserPassword != null) {
settings.hashedUserPassword = BCrypt.withDefaults().hashToString(12, newUserPassword.toCharArray())
newDatabasePassword = newUserPassword + "_" + newDefaultPassword
}
} }
if (persistence.changePassword(newDatabasePassword)) { if (persistence.changePassword(newDatabasePassword)) {