Files
2022-05-27 20:21:01 +03:00

66 lines
2.0 KiB
Swift

//
// AccountModel.swift
// List
//
// Created by Igor Danich on 24.07.2020.
// Copyright © 2020 List. All rights reserved.
//
import Foundation
extension String {
fileprivate static let privateKey = "privateKey"
}
extension Account {
struct Model: Codable {
var username: String
var publicKey: String
var keyType: KeyType
}
struct OldModel: Codable {
let username: String
let publicKey: String
}
enum KeyType: String, Codable {
case active
case owner
}
}
extension Account.Model {
init(username: String, publicKey: String, privateKey: String, keyType: Account.KeyType, password: String) {
self.username = username
self.publicKey = publicKey
self.keyType = keyType
updatePrivateKey(privateKey: privateKey, password: password)
}
func privateKey(password: String?) -> String? {
if let password = password {
return Keychain()[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: password]
} else {
return Keychain()[biometric: .key("\(username)@\(keyType.rawValue)", suffix: .privateKey)]
}
}
func updatePrivateKey(privateKey: String, password: String) {
Keychain()[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: password] = privateKey
}
func migrate(password: String) {
let privateKey = Keychain()[.key(username, suffix: .privateKey), password: password]
Keychain()[.key(username, suffix: .privateKey), password: ""] = nil
Keychain()[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: password] = privateKey
}
func update(password: String, old: String) {
Keychain()[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: password] = privateKey(password: old)
}
func clear() {
Keychain()[.key("\(username)@\(keyType.rawValue)", suffix: .privateKey), password: ""] = nil
}
}