2017-01-19 21:15:47 +08:00
|
|
|
//
|
|
|
|
|
// PasswordStore.swift
|
2021-08-28 07:32:31 +02:00
|
|
|
// passKit
|
2017-01-19 21:15:47 +08:00
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 19/1/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import CoreData
|
2020-06-28 21:25:40 +02:00
|
|
|
import Foundation
|
2017-06-14 00:25:38 +08:00
|
|
|
import KeychainAccess
|
2020-06-28 21:25:40 +02:00
|
|
|
import ObjectiveGit
|
|
|
|
|
import SwiftyUserDefaults
|
|
|
|
|
import UIKit
|
2017-01-19 21:15:47 +08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public class PasswordStore {
|
|
|
|
|
public static let shared = PasswordStore()
|
2019-02-17 11:51:14 +01:00
|
|
|
private static let dateFormatter: DateFormatter = {
|
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
|
dateFormatter.dateStyle = .short
|
|
|
|
|
dateFormatter.timeStyle = .short
|
|
|
|
|
return dateFormatter
|
|
|
|
|
}()
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-04-11 23:23:38 -07:00
|
|
|
public var storeURL: URL
|
2019-09-08 23:00:46 +02:00
|
|
|
|
2025-02-02 22:18:16 -08:00
|
|
|
public var gitRepository: GitRepository?
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2019-06-09 22:18:54 -07:00
|
|
|
public var gitSignatureForNow: GTSignature? {
|
2020-06-28 21:25:40 +02:00
|
|
|
let gitSignatureName = Defaults.gitSignatureName ?? Globals.gitSignatureDefaultName
|
|
|
|
|
let gitSignatureEmail = Defaults.gitSignatureEmail ?? Globals.gitSignatureDefaultEmail
|
|
|
|
|
return GTSignature(name: gitSignatureName, email: gitSignatureEmail, time: Date())
|
2017-03-16 00:38:38 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public var gitPassword: String? {
|
2017-02-20 11:48:39 +08:00
|
|
|
get {
|
2020-06-28 21:25:40 +02:00
|
|
|
AppKeychain.shared.get(for: Globals.gitPassword)
|
2017-04-02 11:21:24 -07:00
|
|
|
}
|
2020-08-10 23:14:12 +02:00
|
|
|
set {
|
|
|
|
|
AppKeychain.shared.add(string: newValue, for: Globals.gitPassword)
|
|
|
|
|
}
|
2017-04-02 11:21:24 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public var gitSSHPrivateKeyPassphrase: String? {
|
2017-04-02 11:21:24 -07:00
|
|
|
get {
|
2020-06-28 21:25:40 +02:00
|
|
|
AppKeychain.shared.get(for: Globals.gitSSHPrivateKeyPassphrase)
|
2017-02-19 22:10:36 +08:00
|
|
|
}
|
2020-08-10 23:14:12 +02:00
|
|
|
set {
|
|
|
|
|
AppKeychain.shared.add(string: newValue, for: Globals.gitSSHPrivateKeyPassphrase)
|
|
|
|
|
}
|
2017-02-19 22:10:36 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-09-20 15:07:18 +02:00
|
|
|
private let fileManager = FileManager.default
|
2025-02-02 22:18:16 -08:00
|
|
|
private let notificationCenter = NotificationCenter.default
|
2025-01-25 15:40:12 -08:00
|
|
|
private lazy var context: NSManagedObjectContext = PersistenceController.shared.viewContext()
|
2020-06-28 21:25:40 +02:00
|
|
|
|
|
|
|
|
public var numberOfPasswords: Int {
|
2025-01-25 15:40:12 -08:00
|
|
|
PasswordEntity.totalNumber(in: context)
|
2017-03-24 22:05:09 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
|
|
|
|
public var sizeOfRepositoryByteCount: UInt64 {
|
2020-09-20 15:07:18 +02:00
|
|
|
(try? fileManager.allocatedSizeOfDirectoryAtURL(directoryURL: storeURL)) ?? 0
|
2018-11-16 22:28:19 -08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2019-11-30 15:11:28 -08:00
|
|
|
public var numberOfLocalCommits: Int {
|
2020-06-28 21:25:40 +02:00
|
|
|
(try? getLocalCommits()).map(\.count) ?? 0
|
2018-11-16 22:28:19 -08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2018-11-16 22:28:19 -08:00
|
|
|
public var lastSyncedTime: Date? {
|
2020-06-28 21:25:40 +02:00
|
|
|
Defaults.lastSyncedTime
|
2017-03-24 22:05:09 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2021-01-17 19:49:05 -08:00
|
|
|
public var lastSyncedTimeString: String {
|
|
|
|
|
guard let date = lastSyncedTime else {
|
|
|
|
|
return "SyncAgain?".localize()
|
|
|
|
|
}
|
|
|
|
|
let formatter = DateFormatter()
|
|
|
|
|
formatter.dateStyle = .medium
|
|
|
|
|
formatter.timeStyle = .short
|
|
|
|
|
return formatter.string(from: date)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-02 22:18:16 -08:00
|
|
|
public var numberOfCommits: Int? {
|
|
|
|
|
gitRepository?.numberOfCommits()
|
2019-01-06 19:59:16 +01:00
|
|
|
}
|
2020-04-11 23:23:38 -07:00
|
|
|
|
2025-01-25 15:40:12 -08:00
|
|
|
init(url: URL = Globals.repositoryURL) {
|
2020-06-28 21:25:40 +02:00
|
|
|
self.storeURL = url
|
2020-04-11 23:23:38 -07:00
|
|
|
|
2019-07-19 01:46:56 +08:00
|
|
|
// Migration
|
2019-06-26 21:49:29 +02:00
|
|
|
importExistingKeysIntoKeychain()
|
2020-04-11 23:23:38 -07:00
|
|
|
|
2017-01-23 16:29:36 +08:00
|
|
|
do {
|
2020-09-20 15:07:18 +02:00
|
|
|
if fileManager.fileExists(atPath: storeURL.path) {
|
2025-02-02 22:18:16 -08:00
|
|
|
try self.gitRepository = GitRepository(with: storeURL)
|
2017-02-06 11:17:56 +08:00
|
|
|
}
|
2017-01-23 16:29:36 +08:00
|
|
|
} catch {
|
|
|
|
|
print(error)
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2017-04-02 11:21:24 -07:00
|
|
|
}
|
2019-06-26 21:49:29 +02:00
|
|
|
|
|
|
|
|
private func importExistingKeysIntoKeychain() {
|
2019-07-19 01:46:56 +08:00
|
|
|
// App Store update: v0.5.1 -> v0.6.0
|
2021-12-28 02:57:11 +01:00
|
|
|
try? KeyFileManager(keyType: PGPKey.PUBLIC, keyPath: Globals.pgpPublicKeyPath).importKeyFromFileSharing()
|
|
|
|
|
try? KeyFileManager(keyType: PGPKey.PRIVATE, keyPath: Globals.pgpPrivateKeyPath).importKeyFromFileSharing()
|
|
|
|
|
try? KeyFileManager(keyType: SSHKey.PRIVATE, keyPath: Globals.gitSSHPrivateKeyPath).importKeyFromFileSharing()
|
2020-01-02 00:48:00 +01:00
|
|
|
Defaults.remove(\.pgpPublicKeyArmor)
|
|
|
|
|
Defaults.remove(\.pgpPrivateKeyArmor)
|
|
|
|
|
Defaults.remove(\.gitSSHPrivateKeyArmor)
|
2019-06-26 21:49:29 +02:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-03-04 20:27:23 +01:00
|
|
|
public func repositoryExists() -> Bool {
|
2025-01-25 15:40:12 -08:00
|
|
|
fileManager.fileExists(atPath: Globals.repositoryURL.path)
|
2017-02-27 17:30:33 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-07-05 00:33:01 +02:00
|
|
|
public func cloneRepository(
|
|
|
|
|
remoteRepoURL: URL,
|
|
|
|
|
branchName: String,
|
2025-02-02 22:18:16 -08:00
|
|
|
options: CloneOptions = [:],
|
|
|
|
|
transferProgressBlock: @escaping TransferProgressHandler = { _, _ in },
|
|
|
|
|
checkoutProgressBlock: @escaping CheckoutProgressHandler = { _, _, _ in }
|
2020-07-05 00:33:01 +02:00
|
|
|
) throws {
|
2020-09-20 15:07:18 +02:00
|
|
|
try? fileManager.removeItem(at: storeURL)
|
2020-06-28 21:25:40 +02:00
|
|
|
gitPassword = nil
|
|
|
|
|
gitSSHPrivateKeyPassphrase = nil
|
2017-02-04 14:59:55 +08:00
|
|
|
do {
|
2025-02-02 22:18:16 -08:00
|
|
|
gitRepository = try GitRepository(from: remoteRepoURL, to: storeURL, branchName: branchName, options: options, transferProgressBlock: transferProgressBlock, checkoutProgressBlock: checkoutProgressBlock)
|
2017-02-04 14:59:55 +08:00
|
|
|
} catch {
|
2020-01-02 00:48:00 +01:00
|
|
|
Defaults.lastSyncedTime = nil
|
2017-08-12 21:41:34 +08:00
|
|
|
DispatchQueue.main.async {
|
2025-01-25 15:40:12 -08:00
|
|
|
self.deleteCoreData()
|
2025-02-02 22:18:16 -08:00
|
|
|
self.notificationCenter.post(name: .passwordStoreUpdated, object: nil)
|
2017-08-12 21:41:34 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
throw (error)
|
2017-02-04 14:59:55 +08:00
|
|
|
}
|
2020-01-02 00:48:00 +01:00
|
|
|
Defaults.lastSyncedTime = Date()
|
2017-03-29 22:59:30 -07:00
|
|
|
DispatchQueue.main.async {
|
2025-01-25 15:40:12 -08:00
|
|
|
self.deleteCoreData()
|
|
|
|
|
self.initPasswordEntityCoreData()
|
2025-02-02 22:18:16 -08:00
|
|
|
self.notificationCenter.post(name: .passwordStoreUpdated, object: nil)
|
2019-01-06 20:10:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-07-05 00:33:01 +02:00
|
|
|
public func pullRepository(
|
2025-02-02 22:18:16 -08:00
|
|
|
options: PullOptions,
|
|
|
|
|
progressBlock: @escaping TransferProgressHandler = { _, _ in }
|
2020-07-05 00:33:01 +02:00
|
|
|
) throws {
|
2025-02-02 22:18:16 -08:00
|
|
|
guard let gitRepository else {
|
2020-09-20 15:07:18 +02:00
|
|
|
throw AppError.repositoryNotSet
|
2017-02-09 19:44:12 +08:00
|
|
|
}
|
2025-02-02 22:18:16 -08:00
|
|
|
try gitRepository.pull(options: options, transferProgressBlock: progressBlock)
|
2020-01-02 00:48:00 +01:00
|
|
|
Defaults.lastSyncedTime = Date()
|
2020-06-28 21:25:40 +02:00
|
|
|
setAllSynced()
|
2017-03-29 22:59:30 -07:00
|
|
|
DispatchQueue.main.async {
|
2025-01-25 15:40:12 -08:00
|
|
|
self.deleteCoreData()
|
|
|
|
|
self.initPasswordEntityCoreData()
|
2025-02-02 22:18:16 -08:00
|
|
|
self.notificationCenter.post(name: .passwordStoreUpdated, object: nil)
|
2017-03-29 22:59:30 -07:00
|
|
|
}
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2025-01-25 15:40:12 -08:00
|
|
|
private func initPasswordEntityCoreData() {
|
|
|
|
|
PasswordEntity.initPasswordEntityCoreData(url: storeURL, in: context)
|
2020-06-28 21:25:40 +02:00
|
|
|
saveUpdatedContext()
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public func getRecentCommits(count: Int) throws -> [GTCommit] {
|
2025-02-02 22:18:16 -08:00
|
|
|
guard let gitRepository else {
|
|
|
|
|
throw AppError.repositoryNotSet
|
2017-02-22 18:43:19 +08:00
|
|
|
}
|
2025-02-02 22:18:16 -08:00
|
|
|
return try gitRepository.getRecentCommits(count: count)
|
2017-02-22 18:43:19 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public func fetchPasswordEntityCoreData(parent: PasswordEntity?) -> [PasswordEntity] {
|
2025-01-25 15:40:12 -08:00
|
|
|
PasswordEntity.fetch(by: parent, in: context)
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2025-01-25 15:40:12 -08:00
|
|
|
public func fetchPasswordEntityCoreData(withDir _: Bool) -> [PasswordEntity] {
|
|
|
|
|
PasswordEntity.fetchAllPassword(in: context)
|
2017-02-12 01:59:40 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public func fetchUnsyncedPasswords() -> [PasswordEntity] {
|
2025-01-25 15:40:12 -08:00
|
|
|
PasswordEntity.fetchUnsynced(in: context)
|
2017-02-12 01:59:40 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2021-01-03 15:08:15 -08:00
|
|
|
public func fetchPasswordEntity(with path: String) -> PasswordEntity? {
|
2025-01-25 15:40:12 -08:00
|
|
|
PasswordEntity.fetch(by: path, in: context)
|
2021-01-03 15:08:15 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public func setAllSynced() {
|
2025-01-25 15:40:12 -08:00
|
|
|
_ = PasswordEntity.updateAllToSynced(in: context)
|
|
|
|
|
saveUpdatedContext()
|
2017-02-12 01:59:40 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2025-01-25 15:40:12 -08:00
|
|
|
public func getLatestUpdateInfo(path: String) -> String {
|
2025-02-02 22:18:16 -08:00
|
|
|
guard let gitRepository else {
|
2020-07-05 00:16:22 +02:00
|
|
|
return "Unknown".localize()
|
|
|
|
|
}
|
2025-02-02 22:18:16 -08:00
|
|
|
guard let lastCommitDate = try? gitRepository.lastCommitDate(path: path) else {
|
2020-06-28 21:25:40 +02:00
|
|
|
return "Unknown".localize()
|
2017-02-27 15:21:25 +08:00
|
|
|
}
|
2019-02-17 11:51:14 +01:00
|
|
|
if Date().timeIntervalSince(lastCommitDate) <= 60 {
|
|
|
|
|
return "JustNow".localize()
|
|
|
|
|
}
|
2021-10-03 05:46:07 +02:00
|
|
|
return Self.dateFormatter.string(from: lastCommitDate)
|
2017-02-27 15:21:25 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-04-26 20:28:15 -07:00
|
|
|
private func deleteDirectoryTree(at url: URL) throws {
|
2025-01-25 15:40:12 -08:00
|
|
|
var tempURL = url.deletingLastPathComponent()
|
|
|
|
|
while try fileManager.contentsOfDirectory(atPath: tempURL.path).isEmpty {
|
2020-09-20 15:07:18 +02:00
|
|
|
try fileManager.removeItem(at: tempURL)
|
2017-04-26 20:28:15 -07:00
|
|
|
tempURL.deleteLastPathComponent()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-04-26 20:28:15 -07:00
|
|
|
private func createDirectoryTree(at url: URL) throws {
|
2025-01-25 15:40:12 -08:00
|
|
|
let tempURL = url.deletingLastPathComponent()
|
|
|
|
|
try fileManager.createDirectory(at: tempURL, withIntermediateDirectories: true)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-08-23 01:15:23 +02:00
|
|
|
public func pushRepository(
|
2025-02-02 22:18:16 -08:00
|
|
|
options: PushOptions,
|
|
|
|
|
transferProgressBlock: @escaping PushProgressHandler = { _, _, _, _ in }
|
2020-08-23 01:15:23 +02:00
|
|
|
) throws {
|
2025-02-02 22:18:16 -08:00
|
|
|
guard let gitRepository else {
|
2020-09-20 15:07:18 +02:00
|
|
|
throw AppError.repositoryNotSet
|
2017-04-30 16:16:52 -05:00
|
|
|
}
|
2025-02-02 22:18:16 -08:00
|
|
|
try gitRepository.push(options: options, transferProgressBlock: transferProgressBlock)
|
2017-02-10 22:15:01 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-04-25 13:01:17 -07:00
|
|
|
private func addPasswordEntities(password: Password) throws -> PasswordEntity? {
|
2025-02-08 14:26:45 -08:00
|
|
|
guard !PasswordEntity.exists(password: password, in: context) else {
|
2020-09-20 15:07:18 +02:00
|
|
|
throw AppError.passwordDuplicated
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-04-23 10:03:09 -07:00
|
|
|
var paths: [String] = []
|
2025-01-25 15:40:12 -08:00
|
|
|
var path = password.path
|
|
|
|
|
while !path.isEmpty {
|
|
|
|
|
paths.append(path)
|
|
|
|
|
path = (path as NSString).deletingLastPathComponent
|
2017-03-02 17:26:12 +08:00
|
|
|
}
|
2025-01-25 15:40:12 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
var parentPasswordEntity: PasswordEntity?
|
2025-01-25 15:40:12 -08:00
|
|
|
for (index, path) in paths.reversed().enumerated() {
|
|
|
|
|
if index == paths.count - 1 {
|
|
|
|
|
let passwordEntity = PasswordEntity.insert(name: password.name, path: path, isDir: false, into: context)
|
|
|
|
|
passwordEntity.parent = parentPasswordEntity
|
2019-11-19 02:19:53 +08:00
|
|
|
parentPasswordEntity = passwordEntity
|
2017-04-23 10:03:09 -07:00
|
|
|
} else {
|
2025-01-25 15:40:12 -08:00
|
|
|
if let passwordEntity = PasswordEntity.fetch(by: path, isDir: true, in: context) {
|
|
|
|
|
passwordEntity.isSynced = false
|
|
|
|
|
parentPasswordEntity = passwordEntity
|
2017-04-23 10:03:09 -07:00
|
|
|
} else {
|
2025-01-25 15:40:12 -08:00
|
|
|
let name = (path as NSString).lastPathComponent
|
|
|
|
|
let passwordEntity = PasswordEntity.insert(name: name, path: path, isDir: true, into: context)
|
|
|
|
|
passwordEntity.parent = parentPasswordEntity
|
|
|
|
|
parentPasswordEntity = passwordEntity
|
2017-04-23 10:03:09 -07:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-10 22:15:01 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
saveUpdatedContext()
|
2019-11-19 02:19:53 +08:00
|
|
|
return parentPasswordEntity
|
2017-02-15 20:01:17 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-04-17 23:56:14 -07:00
|
|
|
public func add(password: Password, keyID: String? = nil) throws -> PasswordEntity? {
|
2025-02-02 22:18:16 -08:00
|
|
|
let saveURL = password.fileURL(in: storeURL)
|
2025-01-25 15:40:12 -08:00
|
|
|
try createDirectoryTree(at: saveURL)
|
2020-06-28 21:25:40 +02:00
|
|
|
try encrypt(password: password, keyID: keyID).write(to: saveURL)
|
2025-01-25 15:40:12 -08:00
|
|
|
try gitAdd(path: password.path)
|
2025-02-02 22:18:16 -08:00
|
|
|
try gitCommit(message: "AddPassword.".localize(password.path))
|
2020-04-17 23:56:14 -07:00
|
|
|
let newPasswordEntity = try addPasswordEntities(password: password)
|
2025-02-02 22:18:16 -08:00
|
|
|
notificationCenter.post(name: .passwordStoreUpdated, object: nil)
|
2017-04-23 10:03:09 -07:00
|
|
|
return newPasswordEntity
|
|
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-04-26 20:28:15 -07:00
|
|
|
public func delete(passwordEntity: PasswordEntity) throws {
|
2025-02-02 22:18:16 -08:00
|
|
|
let deletedFileURL = passwordEntity.fileURL(in: storeURL)
|
|
|
|
|
let deletedFilePath = passwordEntity.path
|
2025-01-25 15:40:12 -08:00
|
|
|
try gitRm(path: passwordEntity.path)
|
2018-04-12 00:52:10 +08:00
|
|
|
try deletePasswordEntities(passwordEntity: passwordEntity)
|
|
|
|
|
try deleteDirectoryTree(at: deletedFileURL)
|
2025-02-02 22:18:16 -08:00
|
|
|
try gitCommit(message: "RemovePassword.".localize(deletedFilePath))
|
|
|
|
|
notificationCenter.post(name: .passwordStoreUpdated, object: nil)
|
2017-04-26 20:28:15 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-04-17 23:56:14 -07:00
|
|
|
public func edit(passwordEntity: PasswordEntity, password: Password, keyID: String? = nil) throws -> PasswordEntity? {
|
2017-04-25 13:01:17 -07:00
|
|
|
var newPasswordEntity: PasswordEntity? = passwordEntity
|
2025-02-02 22:18:16 -08:00
|
|
|
let url = passwordEntity.fileURL(in: storeURL)
|
2020-06-28 21:25:40 +02:00
|
|
|
|
|
|
|
|
if password.changed & PasswordChange.content.rawValue != 0 {
|
2025-01-25 15:40:12 -08:00
|
|
|
try encrypt(password: password, keyID: keyID).write(to: url)
|
|
|
|
|
try gitAdd(path: password.path)
|
2025-02-02 22:18:16 -08:00
|
|
|
try gitCommit(message: "EditPassword.".localize(passwordEntity.path))
|
2017-04-26 20:28:15 -07:00
|
|
|
newPasswordEntity = passwordEntity
|
2025-01-25 15:40:12 -08:00
|
|
|
newPasswordEntity?.isSynced = false
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
|
|
|
|
if password.changed & PasswordChange.path.rawValue != 0 {
|
2019-06-09 22:18:54 -07:00
|
|
|
let deletedFileURL = url
|
2017-04-26 20:28:15 -07:00
|
|
|
// add
|
2025-02-02 22:18:16 -08:00
|
|
|
let newFileURL = password.fileURL(in: storeURL)
|
2025-01-25 15:40:12 -08:00
|
|
|
try createDirectoryTree(at: newFileURL)
|
2017-04-26 20:28:15 -07:00
|
|
|
newPasswordEntity = try addPasswordEntities(password: password)
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-04-26 20:28:15 -07:00
|
|
|
// mv
|
2025-01-25 15:40:12 -08:00
|
|
|
try gitMv(from: passwordEntity.path, to: password.path)
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-04-26 20:28:15 -07:00
|
|
|
// delete
|
|
|
|
|
try deleteDirectoryTree(at: deletedFileURL)
|
2025-02-02 22:18:16 -08:00
|
|
|
let deletedFilePath = passwordEntity.path
|
2017-04-26 20:28:15 -07:00
|
|
|
try deletePasswordEntities(passwordEntity: passwordEntity)
|
2025-02-02 22:18:16 -08:00
|
|
|
try gitCommit(message: "RenamePassword.".localize(deletedFilePath, password.path))
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2025-01-25 15:40:12 -08:00
|
|
|
saveUpdatedContext()
|
2025-02-02 22:18:16 -08:00
|
|
|
notificationCenter.post(name: .passwordStoreUpdated, object: nil)
|
2017-04-25 13:01:17 -07:00
|
|
|
return newPasswordEntity
|
2017-04-23 10:03:09 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-04-25 13:01:17 -07:00
|
|
|
private func deletePasswordEntities(passwordEntity: PasswordEntity) throws {
|
2025-01-25 15:40:12 -08:00
|
|
|
PasswordEntity.deleteRecursively(entity: passwordEntity, in: context)
|
2020-06-28 21:25:40 +02:00
|
|
|
saveUpdatedContext()
|
2017-03-21 13:16:25 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2019-11-19 02:19:53 +08:00
|
|
|
public func saveUpdatedContext() {
|
2025-01-25 15:40:12 -08:00
|
|
|
PersistenceController.shared.save()
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2025-01-25 15:40:12 -08:00
|
|
|
public func deleteCoreData() {
|
|
|
|
|
PasswordEntity.deleteAll(in: context)
|
|
|
|
|
PersistenceController.shared.save()
|
2017-02-16 13:24:41 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2021-12-28 02:38:33 +01:00
|
|
|
public func eraseStoreData() {
|
2019-10-01 00:40:33 +08:00
|
|
|
// Delete files.
|
2020-09-20 15:07:18 +02:00
|
|
|
try? fileManager.removeItem(at: storeURL)
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2019-10-01 00:40:33 +08:00
|
|
|
// Delete core data.
|
2025-01-25 15:40:12 -08:00
|
|
|
deleteCoreData()
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2019-10-01 00:40:33 +08:00
|
|
|
// Clean up variables inside PasswordStore.
|
2025-02-02 22:18:16 -08:00
|
|
|
gitRepository = nil
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2019-10-01 00:40:33 +08:00
|
|
|
// Broadcast.
|
2025-02-02 22:18:16 -08:00
|
|
|
notificationCenter.post(name: .passwordStoreUpdated, object: nil)
|
|
|
|
|
notificationCenter.post(name: .passwordStoreErased, object: nil)
|
2017-02-07 16:45:14 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2021-12-28 02:38:33 +01:00
|
|
|
public func erase() {
|
|
|
|
|
eraseStoreData()
|
|
|
|
|
|
|
|
|
|
// Delete PGP key, SSH key and other secrets from the keychain.
|
|
|
|
|
AppKeychain.shared.removeAllContent()
|
|
|
|
|
|
|
|
|
|
// Delete default settings.
|
|
|
|
|
Defaults.removeAll()
|
|
|
|
|
|
|
|
|
|
// Delete cache explicitly.
|
|
|
|
|
PasscodeLock.shared.delete()
|
|
|
|
|
PGPAgent.shared.uninitKeys()
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-09 16:59:07 -08:00
|
|
|
// return the number of discarded commits
|
2017-06-13 11:42:49 +08:00
|
|
|
public func reset() throws -> Int {
|
2025-02-02 22:18:16 -08:00
|
|
|
guard let gitRepository else {
|
2020-09-20 15:07:18 +02:00
|
|
|
throw AppError.repositoryNotSet
|
2017-04-30 18:29:47 -05:00
|
|
|
}
|
2025-02-02 22:18:16 -08:00
|
|
|
let localCommitsCount = try getLocalCommits().count
|
|
|
|
|
try gitRepository.reset()
|
2020-07-04 22:19:01 +02:00
|
|
|
setAllSynced()
|
2025-01-25 15:40:12 -08:00
|
|
|
deleteCoreData()
|
|
|
|
|
initPasswordEntityCoreData()
|
2020-07-04 22:19:01 +02:00
|
|
|
|
2025-02-02 22:18:16 -08:00
|
|
|
notificationCenter.post(name: .passwordStoreUpdated, object: nil)
|
|
|
|
|
notificationCenter.post(name: .passwordStoreChangeDiscarded, object: nil)
|
|
|
|
|
return localCommitsCount
|
2017-03-04 01:15:28 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2020-07-04 22:19:01 +02:00
|
|
|
private func getLocalCommits() throws -> [GTCommit] {
|
2025-02-02 22:18:16 -08:00
|
|
|
guard let gitRepository else {
|
2020-09-20 15:07:18 +02:00
|
|
|
throw AppError.repositoryNotSet
|
2017-04-30 16:16:52 -05:00
|
|
|
}
|
2025-02-02 22:18:16 -08:00
|
|
|
return try gitRepository.getLocalCommits()
|
2017-03-22 19:07:41 -07:00
|
|
|
}
|
2020-04-13 01:30:00 -07:00
|
|
|
|
2020-04-18 22:35:17 -07:00
|
|
|
public func decrypt(passwordEntity: PasswordEntity, keyID: String? = nil, requestPGPKeyPassphrase: @escaping (String) -> String) throws -> Password {
|
2025-02-02 22:18:16 -08:00
|
|
|
let url = passwordEntity.fileURL(in: storeURL)
|
2025-01-25 15:40:12 -08:00
|
|
|
let encryptedData = try Data(contentsOf: url)
|
2021-01-07 21:58:38 -08:00
|
|
|
let data: Data? = try {
|
2021-01-10 20:28:20 -08:00
|
|
|
if Defaults.isEnableGPGIDOn {
|
2025-01-25 15:40:12 -08:00
|
|
|
let keyID = keyID ?? findGPGID(from: url)
|
2021-01-07 21:58:38 -08:00
|
|
|
return try PGPAgent.shared.decrypt(encryptedData: encryptedData, keyID: keyID, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
|
|
|
|
}
|
2021-01-31 13:34:37 +01:00
|
|
|
return try PGPAgent.shared.decrypt(encryptedData: encryptedData, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
2021-01-07 21:58:38 -08:00
|
|
|
}()
|
|
|
|
|
guard let decryptedData = data else {
|
2020-09-20 15:07:18 +02:00
|
|
|
throw AppError.decryption
|
2018-11-10 22:38:12 -08:00
|
|
|
}
|
2019-07-17 02:58:01 +08:00
|
|
|
let plainText = String(data: decryptedData, encoding: .utf8) ?? ""
|
2025-01-25 15:40:12 -08:00
|
|
|
return Password(name: passwordEntity.name, path: passwordEntity.path, plainText: plainText)
|
2017-04-23 10:03:09 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2021-01-03 15:08:15 -08:00
|
|
|
public func decrypt(path: String, keyID: String? = nil, requestPGPKeyPassphrase: @escaping (String) -> String) throws -> Password {
|
|
|
|
|
guard let passwordEntity = fetchPasswordEntity(with: path) else {
|
|
|
|
|
throw AppError.decryption
|
|
|
|
|
}
|
2021-01-10 20:28:20 -08:00
|
|
|
if Defaults.isEnableGPGIDOn {
|
2021-01-07 21:58:38 -08:00
|
|
|
return try decrypt(passwordEntity: passwordEntity, keyID: keyID, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
|
|
|
|
}
|
2021-01-31 13:34:37 +01:00
|
|
|
return try decrypt(passwordEntity: passwordEntity, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
2021-01-03 15:08:15 -08:00
|
|
|
}
|
|
|
|
|
|
2020-04-17 23:56:14 -07:00
|
|
|
public func encrypt(password: Password, keyID: String? = nil) throws -> Data {
|
2025-02-02 22:18:16 -08:00
|
|
|
let encryptedDataPath = password.fileURL(in: storeURL)
|
2020-04-17 23:56:14 -07:00
|
|
|
let keyID = keyID ?? findGPGID(from: encryptedDataPath)
|
2021-01-10 20:28:20 -08:00
|
|
|
if Defaults.isEnableGPGIDOn {
|
2021-01-10 15:27:50 -08:00
|
|
|
return try PGPAgent.shared.encrypt(plainData: password.plainData, keyID: keyID)
|
|
|
|
|
}
|
2021-01-31 13:34:37 +01:00
|
|
|
return try PGPAgent.shared.encrypt(plainData: password.plainData)
|
2017-06-03 18:12:33 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public func removeGitSSHKeys() {
|
2020-09-20 15:07:18 +02:00
|
|
|
try? fileManager.removeItem(atPath: Globals.gitSSHPrivateKeyPath)
|
2020-01-02 00:48:00 +01:00
|
|
|
Defaults.remove(\.gitSSHKeySource)
|
|
|
|
|
Defaults.remove(\.gitSSHPrivateKeyArmor)
|
|
|
|
|
Defaults.remove(\.gitSSHPrivateKeyURL)
|
2021-12-28 02:57:11 +01:00
|
|
|
AppKeychain.shared.removeContent(for: SSHKey.PRIVATE.getKeychainKey())
|
2019-07-02 20:28:47 +02:00
|
|
|
gitSSHPrivateKeyPassphrase = nil
|
2017-06-07 16:54:54 +08:00
|
|
|
}
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2020-04-13 01:30:00 -07:00
|
|
|
|
2025-02-02 22:18:16 -08:00
|
|
|
extension PasswordStore {
|
|
|
|
|
private func gitAdd(path: String) throws {
|
|
|
|
|
guard let gitRepository else {
|
|
|
|
|
throw AppError.repositoryNotSet
|
|
|
|
|
}
|
|
|
|
|
try gitRepository.add(path: path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func gitRm(path: String) throws {
|
|
|
|
|
guard let gitRepository else {
|
|
|
|
|
throw AppError.repositoryNotSet
|
|
|
|
|
}
|
|
|
|
|
try gitRepository.rm(path: path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func gitMv(from: String, to: String) throws {
|
|
|
|
|
guard let gitRepository else {
|
|
|
|
|
throw AppError.repositoryNotSet
|
|
|
|
|
}
|
|
|
|
|
try gitRepository.mv(from: from, to: to)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
private func gitCommit(message: String) throws -> GTCommit {
|
|
|
|
|
guard let gitRepository, let gitSignatureForNow else {
|
|
|
|
|
throw AppError.repositoryNotSet
|
|
|
|
|
}
|
|
|
|
|
return try gitRepository.commit(signature: gitSignatureForNow, message: message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-31 07:35:17 +01:00
|
|
|
func findGPGID(from url: URL) -> String {
|
2020-04-13 01:30:00 -07:00
|
|
|
var path = url
|
2020-06-28 21:25:40 +02:00
|
|
|
while !FileManager.default.fileExists(atPath: path.appendingPathComponent(".gpg-id").path),
|
2020-11-07 12:06:28 +01:00
|
|
|
path.path != "file:///" {
|
2020-04-13 01:30:00 -07:00
|
|
|
path = path.deletingLastPathComponent()
|
|
|
|
|
}
|
|
|
|
|
path = path.appendingPathComponent(".gpg-id")
|
|
|
|
|
|
2020-08-23 01:15:23 +02:00
|
|
|
return (try? String(contentsOf: path))?.trimmed ?? ""
|
2020-04-13 01:30:00 -07:00
|
|
|
}
|