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
btnBiometricAuthentication.customButtonClickHandler = {
authenticationService.authenticateUserWithBiometric { result ->
authenticationService.loginUserWithBiometric { result ->
if (result) {
btnStartBiometricAuthentication.isEnabled = false
@ -67,7 +67,7 @@ open class LoginActivity : BaseActivity() {
protected open fun checkEnteredPasswordAndLogIn() {
btnLogin.isEnabled = false
if (authenticationService.authenticateUserWithPassword(edtxtLoginPassword.chars)) {
if (authenticationService.loginUserWithPassword(edtxtLoginPassword.chars)) {
navigateToMainActivity()
}
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)) {
loadAuthenticationSettings()?.let { settings ->
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
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
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 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) {
func loginUserWithPassword(_ enteredPassword: String, _ authenticationResult: @escaping (Bool, String?) -> Void) {
if let storedHash = readLoginPasswordHash() {
if let salt = readLoginPasswordSalt() {
if let hashOfEnteredPassword = hashLoginPassword(enteredPassword, salt) {
@ -121,6 +107,27 @@ class AuthenticationService {
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
private func openDatabase(_ useBiometricAuthentication: Bool, _ userLoginPassword: String?) -> Bool {
if var databasePassword = readDefaultPassword(useBiometricAuthentication) {

View File

@ -94,7 +94,7 @@ struct LoginDialog: View {
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 {
@ -104,7 +104,7 @@ struct LoginDialog: View {
}
private func loginWithPassword() {
authenticationService.authenticateUserWithPassword(enteredPassword, self.handleAuthenticationResult)
authenticationService.loginUserWithPassword(enteredPassword, self.handleAuthenticationResult)
}
private func handleAuthenticationResult(success: Bool, errorMessage: String?) {

View File

@ -184,7 +184,7 @@ struct ProtectAppSettingsDialog: View {
}
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
}
}