Fixed that in iOS setting authentication type to biometric wasn't possible anymore as () tried to open database without changing its password before (also renamed methods in Android to be symmetrically to iOS)

This commit is contained in:
dankito 2020-10-16 15:18:48 +02:00
parent 81bc6a94bb
commit 3da594b846
5 changed files with 29 additions and 22 deletions

View File

@ -50,7 +50,7 @@ open class LoginActivity : BaseActivity() {
lytPasswordAuthentication.visibility = View.GONE lytPasswordAuthentication.visibility = View.GONE
btnBiometricAuthentication.customButtonClickHandler = { btnBiometricAuthentication.customButtonClickHandler = {
authenticationService.authenticateUserWithBiometric { result -> authenticationService.loginUserWithBiometric { result ->
if (result) { if (result) {
btnStartBiometricAuthentication.isEnabled = false btnStartBiometricAuthentication.isEnabled = false
@ -67,7 +67,7 @@ open class LoginActivity : BaseActivity() {
protected open fun checkEnteredPasswordAndLogIn() { protected open fun checkEnteredPasswordAndLogIn() {
btnLogin.isEnabled = false btnLogin.isEnabled = false
if (authenticationService.authenticateUserWithPassword(edtxtLoginPassword.chars)) { if (authenticationService.loginUserWithPassword(edtxtLoginPassword.chars)) {
navigateToMainActivity() navigateToMainActivity()
} }
else { else {

View File

@ -55,7 +55,7 @@ open class AuthenticationService(
} }
open fun authenticateUserWithPassword(enteredPassword: CharArray): Boolean { open fun loginUserWithPassword(enteredPassword: CharArray): Boolean {
if (isCorrectUserPassword(enteredPassword)) { if (isCorrectUserPassword(enteredPassword)) {
loadAuthenticationSettings()?.let { settings -> loadAuthenticationSettings()?.let { settings ->
return openDatabase(settings, enteredPassword) return openDatabase(settings, enteredPassword)
@ -92,7 +92,7 @@ open class AuthenticationService(
} }
} }
open fun authenticateUserWithBiometric(result: (Boolean) -> Unit) { open fun loginUserWithBiometric(result: (Boolean) -> Unit) {
// Biometric authentication is only supported on Android 6 and above // Biometric authentication is only supported on Android 6 and above
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
result(false) result(false)

View File

@ -90,21 +90,7 @@ class AuthenticationService {
} }
// TODO: big bug, in this way it's not possible to set a new password with biometrics func loginUserWithPassword(_ enteredPassword: String, _ authenticationResult: @escaping (Bool, String?) -> Void) {
func authenticateUserWithBiometric(_ prompt: String, _ authenticationResult: @escaping (Bool, String?) -> Void) {
biometricAuthenticationService.authenticate(prompt) { successful, error in
var decryptDatabaseResult = false
if successful {
decryptDatabaseResult = self.openDatabase(true, nil)
}
authenticationResult(successful && decryptDatabaseResult, error)
}
}
// TODO: implement authenticateUserWithBiometricToSetAsNewAuthenticationMethod()
func authenticateUserWithPassword(_ enteredPassword: String, _ authenticationResult: @escaping (Bool, String?) -> Void) {
if let storedHash = readLoginPasswordHash() { if let storedHash = readLoginPasswordHash() {
if let salt = readLoginPasswordSalt() { if let salt = readLoginPasswordSalt() {
if let hashOfEnteredPassword = hashLoginPassword(enteredPassword, salt) { if let hashOfEnteredPassword = hashLoginPassword(enteredPassword, salt) {
@ -121,6 +107,27 @@ class AuthenticationService {
authenticationResult(false, "Incorrect password entered".localize()) authenticationResult(false, "Incorrect password entered".localize())
} }
func loginUserWithBiometric(_ prompt: String, _ authenticationResult: @escaping (Bool, String?) -> Void) {
authenticateUserWithBiometric(prompt) { successful, error in
var decryptDatabaseResult = false
if successful {
decryptDatabaseResult = self.openDatabase(true, nil)
}
authenticationResult(successful && decryptDatabaseResult, error)
}
}
func authenticateUserWithBiometricToSetAsNewAuthenticationMethod(_ prompt: String, _ authenticationResult: @escaping (Bool, String?) -> Void) {
authenticateUserWithBiometric(prompt, authenticationResult)
}
private func authenticateUserWithBiometric(_ prompt: String, _ authenticationResult: @escaping (Bool, String?) -> Void) {
biometricAuthenticationService.authenticate(prompt) { successful, error in
authenticationResult(successful, error)
}
}
@discardableResult @discardableResult
private func openDatabase(_ useBiometricAuthentication: Bool, _ userLoginPassword: String?) -> Bool { private func openDatabase(_ useBiometricAuthentication: Bool, _ userLoginPassword: String?) -> Bool {
if var databasePassword = readDefaultPassword(useBiometricAuthentication) { if var databasePassword = readDefaultPassword(useBiometricAuthentication) {

View File

@ -94,7 +94,7 @@ struct LoginDialog: View {
private func loginWithBiometricAuthentication() { private func loginWithBiometricAuthentication() {
authenticationService.authenticateUserWithBiometric("Authenticate with biometrics to unlock app reason", self.handleAuthenticationResult) authenticationService.loginUserWithBiometric("Authenticate with biometrics to unlock app reason", self.handleAuthenticationResult)
} }
private func loginWithPasswordOnReturnKeyPress() -> Bool { private func loginWithPasswordOnReturnKeyPress() -> Bool {
@ -104,7 +104,7 @@ struct LoginDialog: View {
} }
private func loginWithPassword() { private func loginWithPassword() {
authenticationService.authenticateUserWithPassword(enteredPassword, self.handleAuthenticationResult) authenticationService.loginUserWithPassword(enteredPassword, self.handleAuthenticationResult)
} }
private func handleAuthenticationResult(success: Bool, errorMessage: String?) { private func handleAuthenticationResult(success: Bool, errorMessage: String?) {

View File

@ -184,7 +184,7 @@ struct ProtectAppSettingsDialog: View {
} }
private func doBiometricAuthentication() { private func doBiometricAuthentication() {
authenticationService.authenticateUserWithBiometric("Authenticate to encrypt data with %@".localize(supportedBiometricAuthenticationLocalizedName)) { success, errorMessage in authenticationService.authenticateUserWithBiometricToSetAsNewAuthenticationMethod("Authenticate to encrypt data with %@".localize(supportedBiometricAuthenticationLocalizedName)) { success, errorMessage in
self.successfullyAuthenticatedWithBiometricAuthentication = success self.successfullyAuthenticatedWithBiometricAuthentication = success
} }
} }