Centralized setting passwords in setPasswords()
This commit is contained in:
parent
79d746a395
commit
f0271a2f85
|
@ -17,7 +17,7 @@ class AuthenticationService {
|
||||||
if let type = readAuthenticationType() {
|
if let type = readAuthenticationType() {
|
||||||
self.authenticationType = type
|
self.authenticationType = type
|
||||||
}
|
}
|
||||||
else {
|
else { // first app run, no authentication type persisted yet -> set to .unprotected
|
||||||
removeAppProtection()
|
removeAppProtection()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,23 +68,22 @@ class AuthenticationService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func setAuthenticationMethodToPassword(_ newPassword: String) {
|
func setAuthenticationMethodToPassword(_ newLoginPassword: String) {
|
||||||
setAuthenticationType(.password)
|
setAuthenticationType(.password)
|
||||||
|
|
||||||
setLoginPassword(newPassword)
|
setPasswords(false, newLoginPassword)
|
||||||
setDefaultPassword(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setAuthenticationMethodToBiometric() {
|
func setAuthenticationMethodToBiometric() {
|
||||||
setAuthenticationType(.biometric)
|
setAuthenticationType(.biometric)
|
||||||
|
|
||||||
setDefaultPassword(true)
|
setPasswords(true, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeAppProtection() {
|
func removeAppProtection() {
|
||||||
setAuthenticationType(.none)
|
setAuthenticationType(.none)
|
||||||
|
|
||||||
setDefaultPassword(false)
|
setPasswords(false, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -124,7 +123,7 @@ class AuthenticationService {
|
||||||
|
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
private func setDefaultPassword(_ useBiometricAuthentication: Bool) -> Bool {
|
private func setPasswords(_ useBiometricAuthentication: Bool, _ newLoginPassword: String?) -> Bool {
|
||||||
do {
|
do {
|
||||||
let passwordItem = createDefaultPasswordKeychainItem(useBiometricAuthentication)
|
let passwordItem = createDefaultPasswordKeychainItem(useBiometricAuthentication)
|
||||||
|
|
||||||
|
@ -132,11 +131,20 @@ class AuthenticationService {
|
||||||
|
|
||||||
try? passwordItem.deleteItem()
|
try? passwordItem.deleteItem()
|
||||||
|
|
||||||
|
var databasePassword = currentPassword ?? ""
|
||||||
|
|
||||||
if let currentPassword = currentPassword {
|
if let currentPassword = currentPassword {
|
||||||
try passwordItem.savePassword(currentPassword)
|
try passwordItem.savePassword(currentPassword)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
createNewDefaultPassword(useBiometricAuthentication)
|
if let newDefaultPassword = createNewDefaultPassword(useBiometricAuthentication) {
|
||||||
|
databasePassword = newDefaultPassword
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let newLoginPassword = newLoginPassword {
|
||||||
|
setLoginPassword(newLoginPassword)
|
||||||
|
databasePassword = newLoginPassword + "_" + databasePassword
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -147,16 +155,21 @@ class AuthenticationService {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createNewDefaultPassword(_ useBiometricAuthentication: Bool) {
|
@discardableResult
|
||||||
|
private func createNewDefaultPassword(_ useBiometricAuthentication: Bool) -> String? {
|
||||||
do {
|
do {
|
||||||
let newDefaultPassword = generateRandomPassword(30)
|
let newDefaultPassword = generateRandomPassword(30)
|
||||||
|
|
||||||
let passwordItem = createDefaultPasswordKeychainItem(useBiometricAuthentication)
|
let passwordItem = createDefaultPasswordKeychainItem(useBiometricAuthentication)
|
||||||
|
|
||||||
try passwordItem.savePassword(newDefaultPassword)
|
try passwordItem.savePassword(newDefaultPassword)
|
||||||
|
|
||||||
|
return newDefaultPassword
|
||||||
} catch {
|
} catch {
|
||||||
NSLog("Could not create new default password: \(error)")
|
NSLog("Could not create new default password: \(error)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createDefaultPasswordKeychainItem(_ useBiometricAuthentication: Bool) -> KeychainPasswordItem {
|
private func createDefaultPasswordKeychainItem(_ useBiometricAuthentication: Bool) -> KeychainPasswordItem {
|
||||||
|
|
Loading…
Reference in New Issue