Saving now also AuthenticationType in Keychain

This commit is contained in:
dankito 2020-10-02 04:33:26 +02:00
parent b40eb25b70
commit a903745b8c
1 changed files with 39 additions and 7 deletions

View File

@ -4,7 +4,7 @@ import LocalAuthentication
class AuthenticationService { class AuthenticationService {
static private let AuthenticationTypeUserDefaultsKey = "AuthenticationType" static private let AuthenticationTypeKeychainAccountName = "AuthenticationType"
static private let DefaultPasswordKeychainAccountName = "DefaultPassword" static private let DefaultPasswordKeychainAccountName = "DefaultPassword"
@ -13,13 +13,18 @@ class AuthenticationService {
private let biometricAuthenticationService = BiometricAuthenticationService() private let biometricAuthenticationService = BiometricAuthenticationService()
init() {
var authenticationType: AuthenticationType { if let type = readAuthenticationType() {
let authenticationTypeString = UserDefaults.standard.string(forKey: Self.AuthenticationTypeUserDefaultsKey, defaultValue: AuthenticationType.none.rawValue) self.authenticationType = type
}
return AuthenticationType.init(rawValue: authenticationTypeString) ?? .none else {
removeAppProtection()
}
} }
private (set) var authenticationType: AuthenticationType = .none
var needsAuthenticationToUnlockApp: Bool { var needsAuthenticationToUnlockApp: Bool {
let authenticationType = self.authenticationType let authenticationType = self.authenticationType
@ -73,12 +78,39 @@ class AuthenticationService {
setDefaultPassword(false) setDefaultPassword(false)
} }
private func readAuthenticationType() -> AuthenticationType? {
do {
let authenticationTypeItem = createAuthenticationTypeKeychainItem()
let authenticationTypeString = try authenticationTypeItem.readPassword()
return AuthenticationType.init(rawValue: authenticationTypeString)
} catch {
NSLog("Could not read AuthenticationType: \(error)")
}
return nil
}
private func setAuthenticationType(_ type: AuthenticationType) { private func setAuthenticationType(_ type: AuthenticationType) {
if needsPasswordToUnlockApp { if needsPasswordToUnlockApp {
deleteLoginPassword() deleteLoginPassword()
} }
UserDefaults.standard.set(type.rawValue, forKey: Self.AuthenticationTypeUserDefaultsKey) do {
let authenticationTypeItem = createAuthenticationTypeKeychainItem()
try authenticationTypeItem.savePassword(type.rawValue)
} catch {
NSLog("Could not save AuthenticationType: \(error)")
}
self.authenticationType = type
}
private func createAuthenticationTypeKeychainItem() -> KeychainPasswordItem {
return KeychainPasswordItem(Self.AuthenticationTypeKeychainAccountName)
} }