passforios/passKit/Models/PasswordStore.swift

872 lines
37 KiB
Swift
Raw Normal View History

2017-01-19 21:15:47 +08:00
//
// PasswordStore.swift
// pass
//
// Created by Mingshen Sun on 19/1/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import Foundation
import CoreData
import UIKit
import SwiftyUserDefaults
2017-01-23 16:29:36 +08:00
import ObjectiveGit
import KeychainAccess
import Gopenpgpwrapper
2017-01-19 21:15:47 +08:00
public class PasswordStore {
public static let shared = PasswordStore()
private static let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
return dateFormatter
}()
public let storeURL = URL(fileURLWithPath: "\(Globals.repositoryPath)")
public let tempStoreURL = URL(fileURLWithPath: "\(Globals.repositoryPath)-temp")
public var storeRepository: GTRepository?
public var pgpKeyID: String?
public var publicKey: GopenpgpwrapperKey? {
didSet {
2019-06-09 22:18:54 -07:00
pgpKeyID = publicKey?.getID()
}
}
public var privateKey: GopenpgpwrapperKey?
2019-06-09 22:18:54 -07:00
public var gitSignatureForNow: GTSignature? {
2017-03-16 00:38:38 +08:00
get {
let gitSignatureName = SharedDefaults[.gitSignatureName] ?? Globals.gitSignatureDefaultName
let gitSignatureEmail = SharedDefaults[.gitSignatureEmail] ?? Globals.gitSignatureDefaultEmail
2019-06-09 22:18:54 -07:00
return GTSignature(name: gitSignatureName, email: gitSignatureEmail, time: Date())
2017-03-16 00:38:38 +08:00
}
}
2019-06-09 22:18:54 -07:00
public var pgpKeyPassphrase: String? {
set {
if newValue != nil {
AppKeychain.add(string: newValue!, for: "pgpKeyPassphrase")
}
}
get {
return AppKeychain.get(for: "pgpKeyPassphrase")
}
}
public var gitPassword: String? {
set {
if newValue != nil {
AppKeychain.add(string: newValue!, for: "gitPassword")
}
}
get {
return AppKeychain.get(for: "gitPassword")
}
}
public var gitSSHPrivateKeyPassphrase: String? {
set {
if newValue != nil {
AppKeychain.add(string: newValue!, for: "gitSSHPrivateKeyPassphrase")
}
}
get {
return AppKeychain.get(for: "gitSSHPrivateKeyPassphrase")
}
}
private let fm = FileManager.default
lazy private var context: NSManagedObjectContext = {
let modelURL = Bundle(identifier: Globals.passKitBundleIdentifier)!.url(forResource: "pass", withExtension: "momd")!
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
let container = NSPersistentContainer(name: "pass", managedObjectModel: managedObjectModel!)
2017-06-15 17:30:57 +08:00
if FileManager.default.fileExists(atPath: Globals.documentPath) {
try! FileManager.default.createDirectory(atPath: Globals.documentPath, withIntermediateDirectories: true, attributes: nil)
}
2017-06-14 21:43:33 +08:00
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: URL(fileURLWithPath: Globals.dbPath))]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
2019-01-14 20:57:45 +01:00
fatalError("UnresolvedError".localize("\(error.localizedDescription), \(error.userInfo)"))
}
})
return container.viewContext
}()
public var numberOfPasswords : Int {
2018-12-09 16:59:07 -08:00
return self.fetchPasswordEntityCoreData(withDir: false).count
}
public var sizeOfRepositoryByteCount : UInt64 {
return (try? fm.allocatedSizeOfDirectoryAtURL(directoryURL: self.storeURL)) ?? 0
}
public var numberOfLocalCommits: Int? {
return (try? getLocalCommits())?.flatMap { $0.count }
}
public var lastSyncedTime: Date? {
return SharedDefaults[.lastSyncedTime]
}
public var numberOfCommits: UInt? {
return storeRepository?.numberOfCommits(inCurrentBranch: nil)
}
2017-01-19 21:15:47 +08:00
private init() {
// File migration to group
migrateIfNeeded()
backwardCompatibility()
importExistingKeysIntoKeychain()
2017-01-23 16:29:36 +08:00
do {
if fm.fileExists(atPath: storeURL.path) {
try storeRepository = GTRepository.init(url: storeURL)
}
try initPGPKeys()
2017-01-23 16:29:36 +08:00
} catch {
print(error)
2017-01-19 21:15:47 +08:00
}
}
private func migrateIfNeeded() {
// migrate happens only if the repository was cloned and pgp keys were set up using earlier versions
let needMigration = !pgpKeyExists() && !fm.fileExists(atPath: Globals.gitSSHPrivateKeyPath) && !fm.fileExists(atPath: Globals.repositoryPath) && fm.fileExists(atPath: Globals.repositoryPathLegacy)
guard needMigration == true else {
return
}
do {
// migrate Defaults
let userDefaults = UserDefaults()
for key in Defaults.dictionaryRepresentation().keys {
if SharedDefaults.value(forKey: key) == nil {
SharedDefaults.setValue(userDefaults.value(forKey: key), forKey: key)
}
}
// migrate files
try fm.createDirectory(atPath: Globals.documentPath, withIntermediateDirectories: true, attributes: nil)
try fm.createDirectory(atPath: Globals.libraryPath, withIntermediateDirectories: true, attributes: nil)
if fm.fileExists(atPath: Globals.pgpPublicKeyPathLegacy) {
try fm.moveItem(atPath: Globals.pgpPublicKeyPathLegacy, toPath: Globals.pgpPublicKeyPath)
}
if fm.fileExists(atPath: Globals.pgpPrivateKeyPathLegacy) {
try fm.moveItem(atPath: Globals.pgpPrivateKeyPathLegacy, toPath: Globals.pgpPrivateKeyPath)
}
if fm.fileExists(atPath: Globals.gitSSHPrivateKeyPathLegacy) {
try fm.moveItem(atPath: Globals.gitSSHPrivateKeyPathLegacy, toPath: Globals.gitSSHPrivateKeyPath)
}
try fm.moveItem(atPath: Globals.repositoryPathLegacy, toPath: Globals.repositoryPath)
} catch {
print("MigrationError".localize(error))
}
updatePasswordEntityCoreData()
}
private func backwardCompatibility() {
// For the newly-introduced isRememberGitCredentialPassphraseOn (20171008)
if (self.gitPassword != nil || self.gitSSHPrivateKeyPassphrase != nil) && SharedDefaults[.isRememberGitCredentialPassphraseOn] == false {
SharedDefaults[.isRememberGitCredentialPassphraseOn] = true
}
// For the renamed isRememberPGPPassphraseOn (20171008)
if self.pgpKeyPassphrase != nil && SharedDefaults[.isRememberPGPPassphraseOn] == false {
SharedDefaults[.isRememberPGPPassphraseOn] = true
}
}
private func importExistingKeysIntoKeychain() {
try? KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: Globals.pgpPublicKeyPath).importKeyAndDeleteFile()
try? KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: Globals.pgpPrivateKeyPath).importKeyAndDeleteFile()
try? KeyFileManager(keyType: SshKey.PRIVATE, keyPath: Globals.gitSSHPrivateKeyPath).importKeyAndDeleteFile()
SharedDefaults.remove(.pgpPublicKeyArmor)
SharedDefaults.remove(.pgpPrivateKeyArmor)
SharedDefaults.remove(.gitSSHPrivateKeyArmor)
SharedDefaults[.pgpKeySource] = "file"
SharedDefaults[.gitSSHKeySource] = "file"
}
public func initGitSSHKey(with armorKey: String) throws {
AppKeychain.add(string: armorKey, for: SshKey.PRIVATE.getKeychainKey())
2017-01-19 21:15:47 +08:00
}
public func initPGPKeys() throws {
try initPGPKey(.PUBLIC)
try initPGPKey(.PRIVATE)
}
2019-07-02 20:20:56 +02:00
private func initPGPKey(_ keyType: PgpKey) throws {
if let key = GopenpgpwrapperReadKey(AppKeychain.get(for: keyType.getKeychainKey())) {
2019-06-24 23:37:01 +02:00
switch keyType {
case .PUBLIC:
self.publicKey = key
case .PRIVATE:
self.privateKey = key
}
2019-06-24 23:37:01 +02:00
return
2017-02-23 16:53:50 +08:00
}
2019-06-24 23:37:01 +02:00
throw AppError.KeyImport
}
2019-07-02 20:20:56 +02:00
public func initPGPKey(from url: URL, keyType: PgpKey) throws {
let pgpKeyData = try Data(contentsOf: url)
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
try initPGPKey(keyType)
}
2019-07-02 20:20:56 +02:00
public func initPGPKey(with armorKey: String, keyType: PgpKey) throws {
2019-06-24 23:37:01 +02:00
let pgpKeyData = armorKey.data(using: .ascii)!
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
try initPGPKey(keyType)
}
public func repositoryExisted() -> Bool {
let fm = FileManager()
return fm.fileExists(atPath: Globals.repositoryPath)
}
public func passwordExisted(password: Password) -> Bool {
let passwordEntityFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "PasswordEntity")
do {
passwordEntityFetchRequest.predicate = NSPredicate(format: "name = %@ and path = %@", password.name, password.url.path)
let count = try context.count(for: passwordEntityFetchRequest)
if count > 0 {
return true
} else {
return false
}
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToFetchPasswordEntities".localize(error))
}
}
public func passwordEntityExisted(path: String) -> Bool {
let passwordEntityFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "PasswordEntity")
do {
passwordEntityFetchRequest.predicate = NSPredicate(format: "path = %@", path)
let count = try context.count(for: passwordEntityFetchRequest)
if count > 0 {
return true
} else {
return false
}
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToFetchPasswordEntities".localize(error))
}
}
public func getPasswordEntity(by path: String, isDir: Bool) -> PasswordEntity? {
let passwordEntityFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "PasswordEntity")
do {
passwordEntityFetchRequest.predicate = NSPredicate(format: "path = %@ and isDir = %@", path, isDir as NSNumber)
return try context.fetch(passwordEntityFetchRequest).first as? PasswordEntity
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToFetchPasswordEntities".localize(error))
}
}
public func cloneRepository(remoteRepoURL: URL,
credential: GitCredential,
branchName: String,
requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?,
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws {
try? fm.removeItem(at: storeURL)
try? fm.removeItem(at: tempStoreURL)
self.gitPassword = nil
self.gitSSHPrivateKeyPassphrase = nil
2017-02-04 14:59:55 +08:00
do {
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryCloneOptionsCredentialProvider: credentialProvider]
2017-04-28 20:33:41 -07:00
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: tempStoreURL, options: options, transferProgressBlock:transferProgressBlock)
2017-06-11 17:51:33 -07:00
try fm.moveItem(at: tempStoreURL, to: storeURL)
2017-04-28 20:33:41 -07:00
storeRepository = try GTRepository(url: storeURL)
2019-01-06 20:10:47 +01:00
try checkoutAndChangeBranch(withName: branchName)
2017-02-04 14:59:55 +08:00
} catch {
2017-04-28 20:33:41 -07:00
credential.delete()
DispatchQueue.main.async {
SharedDefaults[.lastSyncedTime] = nil
self.deleteCoreData(entityName: "PasswordEntity")
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
}
2017-04-28 20:33:41 -07:00
throw(error)
2017-02-04 14:59:55 +08:00
}
DispatchQueue.main.async {
SharedDefaults[.lastSyncedTime] = Date()
self.updatePasswordEntityCoreData()
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
}
2017-01-23 16:29:36 +08:00
}
2019-01-06 20:10:47 +01:00
private func checkoutAndChangeBranch(withName localBranchName: String) throws {
if (localBranchName == "master") {
return
}
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
2019-01-06 20:10:47 +01:00
}
let remoteBranchName = "origin/\(localBranchName)"
guard let remoteBranch = try? storeRepository.lookUpBranch(withName: remoteBranchName, type: .remote, success: nil) else {
throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
2019-01-06 20:10:47 +01:00
}
guard let remoteBranchOid = remoteBranch.oid else {
throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
2019-01-06 20:10:47 +01:00
}
let localBranch = try storeRepository.createBranchNamed(localBranchName, from: remoteBranchOid, message: nil)
try localBranch.updateTrackingBranch(remoteBranch)
let checkoutOptions = GTCheckoutOptions.init(strategy: .force)
try storeRepository.checkoutReference(localBranch.reference, options: checkoutOptions)
try storeRepository.moveHEAD(to: localBranch.reference)
}
public func pullRepository(credential: GitCredential, requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
}
2018-11-16 23:08:35 -08:00
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
let remote = try GTRemote(name: "origin", in: storeRepository)
try storeRepository.pull(storeRepository.currentBranch(), from: remote, withOptions: options, progress: transferProgressBlock)
DispatchQueue.main.async {
SharedDefaults[.lastSyncedTime] = Date()
self.setAllSynced()
self.updatePasswordEntityCoreData()
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
}
2017-01-19 21:15:47 +08:00
}
private func updatePasswordEntityCoreData() {
2017-02-07 16:45:14 +08:00
deleteCoreData(entityName: "PasswordEntity")
do {
var q = try fm.contentsOfDirectory(atPath: self.storeURL.path).filter{
!$0.hasPrefix(".")
}.map { (filename) -> PasswordEntity in
let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: context) as! PasswordEntity
if filename.hasSuffix(".gpg") {
passwordEntity.name = String(filename.prefix(upTo: filename.index(filename.endIndex, offsetBy: -4)))
} else {
passwordEntity.name = filename
}
passwordEntity.path = filename
passwordEntity.parent = nil
return passwordEntity
}
while q.count > 0 {
let e = q.first!
q.remove(at: 0)
guard !e.name!.hasPrefix(".") else {
continue
}
var isDirectory: ObjCBool = false
let filePath = storeURL.appendingPathComponent(e.path!).path
if fm.fileExists(atPath: filePath, isDirectory: &isDirectory) {
if isDirectory.boolValue {
e.isDir = true
let files = try fm.contentsOfDirectory(atPath: filePath).map { (filename) -> PasswordEntity in
let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: context) as! PasswordEntity
if filename.hasSuffix(".gpg") {
2017-09-23 16:43:14 +08:00
passwordEntity.name = String(filename.prefix(upTo: filename.index(filename.endIndex, offsetBy: -4)))
} else {
passwordEntity.name = filename
}
passwordEntity.path = "\(e.path!)/\(filename)"
passwordEntity.parent = e
return passwordEntity
}
q += files
} else {
e.isDir = false
2017-02-06 21:53:54 +08:00
}
2017-01-19 21:15:47 +08:00
}
}
} catch {
print(error)
}
2017-01-19 21:15:47 +08:00
do {
try context.save()
} catch {
2019-01-14 20:57:45 +01:00
print("ErrorSaving".localize(error))
2017-01-19 21:15:47 +08:00
}
}
public func getRecentCommits(count: Int) throws -> [GTCommit] {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
2017-03-19 10:36:59 -07:00
return []
}
2017-02-22 18:43:19 +08:00
var commits = [GTCommit]()
2017-04-30 18:29:47 -05:00
let enumerator = try GTEnumerator(repository: storeRepository)
if let targetOID = try storeRepository.headReference().targetOID {
try enumerator.pushSHA(targetOID.sha)
}
for _ in 0 ..< count {
2018-10-11 13:41:47 +08:00
if let commit = try? enumerator.nextObject(withSuccess: nil) {
commits.append(commit)
}
2017-02-22 18:43:19 +08:00
}
return commits
}
public func fetchPasswordEntityCoreData(parent: PasswordEntity?) -> [PasswordEntity] {
2017-01-19 21:15:47 +08:00
let passwordEntityFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "PasswordEntity")
do {
passwordEntityFetch.predicate = NSPredicate(format: "parent = %@", parent ?? 0)
2017-01-19 21:15:47 +08:00
let fetchedPasswordEntities = try context.fetch(passwordEntityFetch) as! [PasswordEntity]
return fetchedPasswordEntities.sorted { $0.name!.caseInsensitiveCompare($1.name!) == .orderedAscending }
2017-01-19 21:15:47 +08:00
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToFetchPasswords".localize(error))
2017-01-19 21:15:47 +08:00
}
}
public func fetchPasswordEntityCoreData(withDir: Bool) -> [PasswordEntity] {
let passwordEntityFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "PasswordEntity")
2017-02-06 21:53:54 +08:00
do {
if !withDir {
passwordEntityFetch.predicate = NSPredicate(format: "isDir = false")
}
let fetchedPasswordEntities = try context.fetch(passwordEntityFetch) as! [PasswordEntity]
return fetchedPasswordEntities.sorted { $0.name!.caseInsensitiveCompare($1.name!) == .orderedAscending }
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToFetchPasswords".localize(error))
}
}
public func fetchUnsyncedPasswords() -> [PasswordEntity] {
let passwordEntityFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "PasswordEntity")
passwordEntityFetchRequest.predicate = NSPredicate(format: "synced = %i", 0)
do {
let passwordEntities = try context.fetch(passwordEntityFetchRequest) as! [PasswordEntity]
return passwordEntities
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToFetchPasswords".localize(error))
}
}
public func setAllSynced() {
let passwordEntities = fetchUnsyncedPasswords()
for passwordEntity in passwordEntities {
passwordEntity.synced = true
}
do {
2017-02-15 21:48:06 +08:00
if context.hasChanges {
try context.save()
}
} catch {
2019-01-14 20:57:45 +01:00
fatalError("ErrorSaving".localize(error))
}
}
public func getLatestUpdateInfo(filename: String) -> String {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
2019-01-14 20:57:45 +01:00
return "Unknown".localize()
}
2017-04-30 18:29:47 -05:00
guard let blameHunks = try? storeRepository.blame(withFile: filename, options: nil).hunks,
let latestCommitTime = blameHunks.map({
$0.finalSignature?.time?.timeIntervalSince1970 ?? 0
}).max() else {
return "Unknown".localize()
}
let lastCommitDate = Date(timeIntervalSince1970: latestCommitTime)
if Date().timeIntervalSince(lastCommitDate) <= 60 {
return "JustNow".localize()
}
return PasswordStore.dateFormatter.string(from: lastCommitDate)
}
public func updateRemoteRepo() {
2017-01-19 21:15:47 +08:00
}
private func gitAdd(path: String) throws {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
}
2017-04-30 18:29:47 -05:00
try storeRepository.index().addFile(path)
try storeRepository.index().write()
}
private func gitRm(path: String) throws {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
2017-02-13 01:15:42 +08:00
}
2017-04-30 18:29:47 -05:00
let url = storeURL.appendingPathComponent(path)
if fm.fileExists(atPath: url.path) {
try fm.removeItem(at: url)
}
2017-04-30 18:29:47 -05:00
try storeRepository.index().removeFile(path)
try storeRepository.index().write()
2017-04-26 20:28:15 -07:00
}
2017-04-26 20:28:15 -07:00
private func deleteDirectoryTree(at url: URL) throws {
var tempURL = storeURL.appendingPathComponent(url.deletingLastPathComponent().path)
var count = try fm.contentsOfDirectory(atPath: tempURL.path).count
while count == 0 {
try fm.removeItem(at: tempURL)
tempURL.deleteLastPathComponent()
count = try fm.contentsOfDirectory(atPath: tempURL.path).count
}
}
2017-04-26 20:28:15 -07:00
private func createDirectoryTree(at url: URL) throws {
let tempURL = storeURL.appendingPathComponent(url.deletingLastPathComponent().path)
try fm.createDirectory(at: tempURL, withIntermediateDirectories: true, attributes: nil)
2017-02-13 01:15:42 +08:00
}
private func gitMv(from: String, to: String) throws {
2017-04-30 18:29:47 -05:00
let fromURL = storeURL.appendingPathComponent(from)
let toURL = storeURL.appendingPathComponent(to)
try fm.moveItem(at: fromURL, to: toURL)
try gitAdd(path: to)
try gitRm(path: from)
}
private func gitCommit(message: String) throws -> GTCommit? {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
}
2017-04-30 18:29:47 -05:00
let newTree = try storeRepository.index().writeTree()
let headReference = try storeRepository.headReference()
let commitEnum = try GTEnumerator(repository: storeRepository)
try commitEnum.pushSHA(headReference.targetOID!.sha)
let parent = commitEnum.nextObject() as! GTCommit
2019-06-09 22:18:54 -07:00
guard let signature = gitSignatureForNow else {
throw AppError.GitCommit
}
2017-04-30 18:29:47 -05:00
let commit = try storeRepository.createCommit(with: newTree, message: message, author: signature, committer: signature, parents: [parent], updatingReferenceNamed: headReference.name)
return commit
}
private func getLocalBranch(withName branchName: String) throws -> GTBranch? {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
}
let reference = GTBranch.localNamePrefix().appending(branchName)
2017-04-30 18:29:47 -05:00
let branches = try storeRepository.branches(withPrefix: reference)
return branches.first
}
public func pushRepository(credential: GitCredential, requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
}
2017-04-28 20:33:41 -07:00
do {
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
2019-05-01 18:28:22 +02:00
if let branch = try getLocalBranch(withName: SharedDefaults[.gitBranchName]) {
2017-04-30 18:29:47 -05:00
let remote = try GTRemote(name: "origin", in: storeRepository)
2019-01-06 20:10:47 +01:00
try storeRepository.push(branch, to: remote, withOptions: options, progress: transferProgressBlock)
}
2017-04-28 20:33:41 -07:00
} catch {
throw(error)
}
2017-02-10 22:15:01 +08:00
}
private func addPasswordEntities(password: Password) throws -> PasswordEntity? {
guard !passwordExisted(password: password) else {
throw AppError.PasswordDuplicated
}
var passwordURL = password.url
2017-10-16 04:14:11 +08:00
var previousPathLength = Int.max
var paths: [String] = []
while passwordURL.path != "." {
paths.append(passwordURL.path)
passwordURL = passwordURL.deletingLastPathComponent()
2017-10-15 21:37:00 +08:00
// better identify errors before saving a new password
2017-10-16 04:14:11 +08:00
if passwordURL.path != "." && passwordURL.path.count >= previousPathLength {
2017-10-15 21:37:00 +08:00
throw AppError.WrongPasswordFilename
}
2017-10-16 04:14:11 +08:00
previousPathLength = passwordURL.path.count
}
paths.reverse()
var parentPasswordEntity: PasswordEntity? = nil
for path in paths {
2017-04-26 23:02:05 -07:00
let isDir = !path.hasSuffix(".gpg")
if let passwordEntity = getPasswordEntity(by: path, isDir: isDir) {
parentPasswordEntity = passwordEntity
passwordEntity.synced = false
} else {
2017-04-26 23:02:05 -07:00
if !isDir {
2017-04-26 20:28:15 -07:00
return insertPasswordEntity(name: URL(string: path.stringByAddingPercentEncodingForRFC3986()!)!.deletingPathExtension().lastPathComponent, path: path, parent: parentPasswordEntity, synced: false, isDir: false)
} else {
2017-04-26 20:28:15 -07:00
parentPasswordEntity = insertPasswordEntity(name: URL(string: path.stringByAddingPercentEncodingForRFC3986()!)!.lastPathComponent, path: path, parent: parentPasswordEntity, synced: false, isDir: true)
}
}
2017-02-10 22:15:01 +08:00
}
return nil
2017-02-10 22:15:01 +08:00
}
private func insertPasswordEntity(name: String, path: String, parent: PasswordEntity?, synced: Bool = false, isDir: Bool = false) -> PasswordEntity? {
var ret: PasswordEntity? = nil
if let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: self.context) as? PasswordEntity {
passwordEntity.name = name
passwordEntity.path = path
passwordEntity.parent = parent
passwordEntity.synced = synced
passwordEntity.isDir = isDir
do {
try self.context.save()
ret = passwordEntity
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToInsertPasswordEntity".localize(error))
}
}
return ret
}
public func add(password: Password) throws -> PasswordEntity? {
try createDirectoryTree(at: password.url)
let newPasswordEntity = try addPasswordEntities(password: password)
let saveURL = storeURL.appendingPathComponent(password.url.path)
try self.encrypt(password: password).write(to: saveURL)
try gitAdd(path: password.url.path)
2019-01-14 20:57:45 +01:00
let _ = try gitCommit(message: "AddPassword.".localize(password.url.deletingPathExtension().path))
2017-03-21 13:16:25 -07:00
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
return newPasswordEntity
}
2017-04-26 20:28:15 -07:00
public func delete(passwordEntity: PasswordEntity) throws {
2019-06-09 22:18:54 -07:00
let deletedFileURL = try passwordEntity.getURL()
2017-04-26 20:28:15 -07:00
try gitRm(path: deletedFileURL.path)
2018-04-12 00:52:10 +08:00
try deletePasswordEntities(passwordEntity: passwordEntity)
try deleteDirectoryTree(at: deletedFileURL)
2019-01-14 20:57:45 +01:00
let _ = try gitCommit(message: "RemovePassword.".localize(deletedFileURL.deletingPathExtension().path.removingPercentEncoding!))
2017-04-26 20:28:15 -07:00
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
}
public func edit(passwordEntity: PasswordEntity, password: Password) throws -> PasswordEntity? {
var newPasswordEntity: PasswordEntity? = passwordEntity
2019-06-09 22:18:54 -07:00
let url = try passwordEntity.getURL()
if password.changed&PasswordChange.content.rawValue != 0 {
2019-06-09 22:18:54 -07:00
let saveURL = storeURL.appendingPathComponent(url.path)
try self.encrypt(password: password).write(to: saveURL)
2019-06-15 16:21:37 +08:00
try gitAdd(path: url.path)
2019-06-09 22:18:54 -07:00
let _ = try gitCommit(message: "EditPassword.".localize(url.deletingPathExtension().path.removingPercentEncoding!))
2017-04-26 20:28:15 -07:00
newPasswordEntity = passwordEntity
newPasswordEntity?.synced = false
}
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
try createDirectoryTree(at: password.url)
2017-04-26 20:28:15 -07:00
newPasswordEntity = try addPasswordEntities(password: password)
2017-04-26 20:28:15 -07:00
// mv
try gitMv(from: deletedFileURL.path, to: password.url.path)
2017-04-26 20:28:15 -07:00
// delete
try deleteDirectoryTree(at: deletedFileURL)
try deletePasswordEntities(passwordEntity: passwordEntity)
2019-01-14 20:57:45 +01:00
let _ = try gitCommit(message: "RenamePassword.".localize(deletedFileURL.deletingPathExtension().path.removingPercentEncoding!, password.url.deletingPathExtension().path.removingPercentEncoding!))
}
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
return newPasswordEntity
}
private func deletePasswordEntities(passwordEntity: PasswordEntity) throws {
var current: PasswordEntity? = passwordEntity
while current != nil && (current!.children!.count == 0 || !current!.isDir) {
let parent = current!.parent
self.context.delete(current!)
current = parent
do {
try self.context.save()
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToDeletePasswordEntity".localize(error))
}
}
2017-03-21 13:16:25 -07:00
}
public func saveUpdated(passwordEntity: PasswordEntity) {
do {
2017-02-13 01:15:42 +08:00
try context.save()
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailedToSavePasswordEntity".localize(error))
2017-02-13 01:15:42 +08:00
}
}
public func deleteCoreData(entityName: String) {
2017-02-07 16:45:14 +08:00
let deleteFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetchRequest)
2017-02-07 16:45:14 +08:00
do {
try context.execute(deleteRequest)
try context.save()
2017-02-15 21:37:02 +08:00
context.reset()
2017-02-07 16:45:14 +08:00
} catch let error as NSError {
print(error)
}
}
public func updateImage(passwordEntity: PasswordEntity, image: Data?) {
2017-04-30 18:29:47 -05:00
guard let image = image else {
return
}
let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
privateMOC.parent = context
privateMOC.perform {
passwordEntity.image = image
do {
try privateMOC.save()
self.context.performAndWait {
do {
try self.context.save()
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailureToSaveContext".localize(error))
}
}
} catch {
2019-01-14 20:57:45 +01:00
fatalError("FailureToSaveContext".localize(error))
}
}
}
public func erase() {
publicKey = nil
privateKey = nil
try? fm.removeItem(at: storeURL)
try? fm.removeItem(at: tempStoreURL)
try? fm.removeItem(atPath: Globals.pgpPublicKeyPath)
try? fm.removeItem(atPath: Globals.pgpPrivateKeyPath)
try? fm.removeItem(atPath: Globals.gitSSHPrivateKeyPath)
AppKeychain.removeAllContent()
2017-02-07 16:45:14 +08:00
deleteCoreData(entityName: "PasswordEntity")
SharedDefaults.removeAll()
2017-02-13 14:30:38 +08:00
storeRepository = nil
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
NotificationCenter.default.post(name: .passwordStoreErased, object: nil)
2017-02-07 16:45:14 +08:00
}
2018-12-09 16:59:07 -08:00
// return the number of discarded commits
public func reset() throws -> Int {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
2017-04-30 18:29:47 -05:00
}
// get a list of local commits
if let localCommits = try getLocalCommits(),
localCommits.count > 0 {
// get the oldest local commit
guard let firstLocalCommit = localCommits.last,
firstLocalCommit.parents.count == 1,
let newHead = firstLocalCommit.parents.first else {
throw AppError.GitReset
}
2017-04-30 18:29:47 -05:00
try storeRepository.reset(to: newHead, resetType: .hard)
self.setAllSynced()
self.updatePasswordEntityCoreData()
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
NotificationCenter.default.post(name: .passwordStoreChangeDiscarded, object: nil)
return localCommits.count
} else {
return 0 // no new commit
}
}
private func getLocalCommits() throws -> [GTCommit]? {
2017-04-30 18:29:47 -05:00
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSet
}
2019-01-06 20:10:47 +01:00
// get the remote branch
2019-05-01 18:28:22 +02:00
let remoteBranchName = SharedDefaults[.gitBranchName]
2019-01-06 20:10:47 +01:00
guard let remoteBranch = try storeRepository.remoteBranches().first(where: { $0.shortName == remoteBranchName }) else {
throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
}
// check oid before calling localCommitsRelative
2019-01-06 20:10:47 +01:00
guard remoteBranch.oid != nil else {
throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
}
// get a list of local commits
2019-01-06 20:10:47 +01:00
return try storeRepository.localCommitsRelative(toRemoteBranch: remoteBranch)
}
public func decrypt(passwordEntity: PasswordEntity, requestPGPKeyPassphrase: () -> String) throws -> Password? {
let encryptedDataPath = storeURL.appendingPathComponent(passwordEntity.getPath())
let encryptedData = try Data(contentsOf: encryptedDataPath)
var passphrase = self.pgpKeyPassphrase
if passphrase == nil {
passphrase = requestPGPKeyPassphrase()
}
2019-06-09 22:18:54 -07:00
guard let decryptedData = privateKey?.decrypt(encryptedData, passphrase: passphrase) else {
throw AppError.Decryption
}
2019-06-09 22:18:54 -07:00
let plainText = String(data: decryptedData, encoding: .utf8) ?? ""
let url = try passwordEntity.getURL()
return Password(name: passwordEntity.getName(), url: url, plainText: plainText)
}
public func encrypt(password: Password) throws -> Data {
guard publicKey != nil else {
throw AppError.PgpPublicKeyNotExist
2017-04-30 18:29:47 -05:00
}
let plainData = password.plainData
2019-06-09 22:18:54 -07:00
guard let encryptedData = publicKey?.encrypt(plainData, armor: SharedDefaults[.encryptInArmored]) else {
throw AppError.Encryption
}
2019-06-09 22:18:54 -07:00
return encryptedData
}
public func removePGPKeys() {
try? fm.removeItem(atPath: Globals.pgpPublicKeyPath)
try? fm.removeItem(atPath: Globals.pgpPrivateKeyPath)
SharedDefaults.remove(.pgpKeySource)
SharedDefaults.remove(.pgpPrivateKeyURL)
SharedDefaults.remove(.pgpPublicKeyURL)
SharedDefaults.remove(.pgpPublicKeyArmor)
SharedDefaults.remove(.pgpPrivateKeyArmor)
AppKeychain.removeContent(for: "pgpKeyPassphrase")
2019-07-02 20:20:56 +02:00
AppKeychain.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
AppKeychain.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
2017-06-03 18:12:33 -07:00
publicKey = nil
privateKey = nil
}
public func removeGitSSHKeys() {
try? fm.removeItem(atPath: Globals.gitSSHPrivateKeyPath)
Defaults.remove(.gitSSHKeySource)
2017-06-03 18:12:33 -07:00
Defaults.remove(.gitSSHPrivateKeyArmor)
Defaults.remove(.gitSSHPrivateKeyURL)
AppKeychain.removeContent(for: SshKey.PRIVATE.getKeychainKey())
gitSSHPrivateKeyPassphrase = nil
}
public func pgpKeyExists(inFileSharing: Bool = false) -> Bool {
if inFileSharing == false {
return fm.fileExists(atPath: Globals.pgpPublicKeyPath) && fm.fileExists(atPath: Globals.pgpPrivateKeyPath)
} else {
return KeyFileManager.PublicPgp.doesKeyFileExist() && KeyFileManager.PrivatePgp.doesKeyFileExist()
}
}
2018-11-17 21:41:28 -08:00
public func gitSSHKeyImportFromFileSharing() throws {
try KeyFileManager.PrivateSsh.importKeyAndDeleteFile()
}
2018-11-17 21:41:28 -08:00
public func pgpKeyImportFromFileSharing() throws {
try KeyFileManager.PublicPgp.importKeyAndDeleteFile()
try KeyFileManager.PrivatePgp.importKeyAndDeleteFile()
}
2017-01-19 21:15:47 +08:00
}