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 Result
|
|
|
|
|
import CoreData
|
|
|
|
|
import UIKit
|
2017-01-22 01:42:36 +08:00
|
|
|
import SwiftyUserDefaults
|
2017-01-23 16:29:36 +08:00
|
|
|
import ObjectiveGit
|
2017-01-19 21:15:47 +08:00
|
|
|
|
2017-01-24 01:49:55 +08:00
|
|
|
struct GitCredential {
|
|
|
|
|
|
|
|
|
|
enum Credential {
|
|
|
|
|
case http(userName: String, password: String)
|
|
|
|
|
case ssh(userName: String, password: String, publicKeyFile: URL, privateKeyFile: URL)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var credential: Credential
|
|
|
|
|
|
|
|
|
|
func credentialProvider() throws -> GTCredentialProvider {
|
|
|
|
|
return GTCredentialProvider { (_, _, _) -> (GTCredential) in
|
|
|
|
|
let credential: GTCredential?
|
|
|
|
|
switch self.credential {
|
|
|
|
|
case let .http(userName, password):
|
|
|
|
|
print("username \(userName), password \(password)")
|
|
|
|
|
credential = try? GTCredential(userName: userName, password: password)
|
|
|
|
|
case let .ssh(userName, password, publicKeyFile, privateKeyFile):
|
2017-01-31 22:54:58 +08:00
|
|
|
print("username \(userName), password \(password), publicKeyFile \(publicKeyFile), privateKeyFile \(privateKeyFile)")
|
2017-01-24 01:49:55 +08:00
|
|
|
credential = try? GTCredential(userName: userName, publicKeyURL: publicKeyFile, privateKeyURL: privateKeyFile, passphrase: password)
|
|
|
|
|
}
|
|
|
|
|
return credential ?? GTCredential()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 21:15:47 +08:00
|
|
|
class PasswordStore {
|
|
|
|
|
static let shared = PasswordStore()
|
|
|
|
|
|
2017-01-22 01:42:36 +08:00
|
|
|
let storeURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/password-store")
|
2017-01-23 16:29:36 +08:00
|
|
|
var storeRepository: GTRepository?
|
2017-02-02 15:02:57 +08:00
|
|
|
var gitCredential: GitCredential?
|
2017-01-23 16:29:36 +08:00
|
|
|
|
2017-01-22 01:42:36 +08:00
|
|
|
let pgp: ObjectivePGP = ObjectivePGP()
|
2017-01-19 21:15:47 +08:00
|
|
|
|
|
|
|
|
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private init() {
|
2017-01-23 16:29:36 +08:00
|
|
|
do {
|
|
|
|
|
try storeRepository = GTRepository.init(url: storeURL)
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2017-01-22 01:42:36 +08:00
|
|
|
if Defaults[.pgpKeyID] != "" {
|
|
|
|
|
pgp.importKeys(fromFile: Globals.shared.secringPath, allowDuplicates: false)
|
|
|
|
|
}
|
2017-01-31 22:00:50 +08:00
|
|
|
if Defaults[.gitRepositoryAuthenticationMethod] == "Password" {
|
|
|
|
|
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: Defaults[.gitRepositoryUsername], password: Defaults[.gitRepositoryPassword]))
|
2017-02-02 15:02:57 +08:00
|
|
|
} else if Defaults[.gitRepositoryAuthenticationMethod] == "SSH Key"{
|
2017-01-31 23:11:04 +08:00
|
|
|
gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: Defaults[.gitRepositoryUsername], password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.shared.sshPublicKeyPath, privateKeyFile: Globals.shared.sshPrivateKeyPath))
|
2017-02-02 15:02:57 +08:00
|
|
|
} else {
|
|
|
|
|
gitCredential = nil
|
2017-01-31 22:00:50 +08:00
|
|
|
}
|
2017-01-24 01:49:55 +08:00
|
|
|
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
2017-01-22 01:42:36 +08:00
|
|
|
func initPGP(pgpKeyURL: URL, pgpKeyLocalPath: String) -> Bool {
|
|
|
|
|
do {
|
|
|
|
|
let pgpData = try Data(contentsOf: pgpKeyURL)
|
|
|
|
|
try pgpData.write(to: URL(fileURLWithPath: pgpKeyLocalPath), options: .atomic)
|
|
|
|
|
pgp.importKeys(fromFile: pgpKeyLocalPath, allowDuplicates: false)
|
|
|
|
|
let key = pgp.keys[0]
|
|
|
|
|
Defaults[.pgpKeyID] = key.keyID!.shortKeyString
|
|
|
|
|
if let gpgUser = key.users[0] as? PGPUser {
|
|
|
|
|
Defaults[.pgpKeyUserID] = gpgUser.userID
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
} catch {
|
|
|
|
|
print("error")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-23 17:36:10 +08:00
|
|
|
func cloneRepository(remoteRepoURL: URL,
|
2017-01-24 01:49:55 +08:00
|
|
|
credential: GitCredential,
|
2017-01-23 17:36:10 +08:00
|
|
|
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,
|
|
|
|
|
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) -> Bool {
|
2017-01-24 01:49:55 +08:00
|
|
|
print("start cloning remote repo: \(remoteRepoURL)")
|
2017-01-19 21:15:47 +08:00
|
|
|
let fm = FileManager.default
|
2017-01-23 16:29:36 +08:00
|
|
|
if (storeRepository != nil) {
|
2017-01-19 21:15:47 +08:00
|
|
|
print("remove item")
|
|
|
|
|
do {
|
|
|
|
|
try fm.removeItem(at: storeURL)
|
|
|
|
|
} catch let error as NSError {
|
|
|
|
|
print(error.debugDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-23 16:29:36 +08:00
|
|
|
do {
|
2017-01-23 17:36:10 +08:00
|
|
|
print("start cloning...")
|
2017-01-24 01:49:55 +08:00
|
|
|
let credentialProvider = try credential.credentialProvider()
|
|
|
|
|
let options: [String: Any] = [
|
2017-01-24 16:57:16 +08:00
|
|
|
GTRepositoryCloneOptionsCredentialProvider: credentialProvider,
|
2017-01-24 01:49:55 +08:00
|
|
|
]
|
|
|
|
|
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: storeURL, options: options, transferProgressBlock:transferProgressBlock, checkoutProgressBlock: checkoutProgressBlock)
|
|
|
|
|
print("clone finish")
|
2017-01-23 16:29:36 +08:00
|
|
|
updatePasswordEntityCoreData()
|
2017-01-24 01:49:55 +08:00
|
|
|
gitCredential = credential
|
2017-01-23 16:29:36 +08:00
|
|
|
return true
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-24 16:57:16 +08:00
|
|
|
|
2017-01-23 17:36:10 +08:00
|
|
|
func pullRepository(transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) -> Bool {
|
2017-01-23 16:29:36 +08:00
|
|
|
print("pullRepoisitory")
|
|
|
|
|
do {
|
2017-01-24 16:57:16 +08:00
|
|
|
print("start pulling...")
|
2017-02-02 15:02:57 +08:00
|
|
|
let credentialProvider = try gitCredential!.credentialProvider()
|
2017-01-24 16:57:16 +08:00
|
|
|
let options: [String: Any] = [
|
|
|
|
|
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider
|
|
|
|
|
]
|
2017-01-23 16:29:36 +08:00
|
|
|
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
2017-01-24 16:57:16 +08:00
|
|
|
try storeRepository?.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock)
|
2017-01-19 21:15:47 +08:00
|
|
|
updatePasswordEntityCoreData()
|
|
|
|
|
return true
|
2017-01-23 16:29:36 +08:00
|
|
|
} catch {
|
2017-01-19 21:15:47 +08:00
|
|
|
print(error)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updatePasswordEntityCoreData() {
|
|
|
|
|
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "PasswordEntity")
|
|
|
|
|
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
|
|
|
|
|
do {
|
|
|
|
|
try context.execute(deleteRequest)
|
|
|
|
|
} catch let error as NSError {
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let fm = FileManager.default
|
|
|
|
|
fm.enumerator(atPath: storeURL.path)?.forEach({ (e) in
|
|
|
|
|
if let e = e as? String, let url = URL(string: e) {
|
|
|
|
|
if url.pathExtension == "gpg" {
|
|
|
|
|
let entity = PasswordEntity(context: context)
|
2017-01-23 17:53:49 +08:00
|
|
|
let endIndex = url.lastPathComponent.index(url.lastPathComponent.endIndex, offsetBy: -4)
|
|
|
|
|
entity.name = url.lastPathComponent.substring(to: endIndex)
|
2017-01-22 01:42:36 +08:00
|
|
|
entity.rawPath = "password-store/\(url.absoluteString)"
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
do {
|
|
|
|
|
try context.save()
|
|
|
|
|
} catch {
|
|
|
|
|
print("Error with save: \(error)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fetchPasswordEntityCoreData() -> [PasswordEntity] {
|
|
|
|
|
let passwordEntityFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "PasswordEntity")
|
|
|
|
|
do {
|
|
|
|
|
let fetchedPasswordEntities = try context.fetch(passwordEntityFetch) as! [PasswordEntity]
|
2017-01-23 17:49:06 +08:00
|
|
|
return fetchedPasswordEntities.sorted(by: { (p1, p2) -> Bool in
|
|
|
|
|
return p1.name! < p2.name!;
|
|
|
|
|
})
|
2017-01-19 21:15:47 +08:00
|
|
|
} catch {
|
|
|
|
|
fatalError("Failed to fetch employees: \(error)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateRemoteRepo() {
|
|
|
|
|
}
|
|
|
|
|
}
|