Add search and copy password in extension

- no lock screen for now
- share keychain between app and extension
This commit is contained in:
Yishi Lin 2017-06-14 00:25:38 +08:00
parent abe1c46d83
commit 8e5a824cca
11 changed files with 362 additions and 132 deletions

View file

@ -8,7 +8,7 @@
import Foundation
enum AppError: Error {
public enum AppError: Error {
case RepositoryNotSetError
case RepositoryRemoteMasterNotFoundError
case KeyImportError

View file

@ -63,7 +63,7 @@ public class Utils {
}
public static func getPasswordFromKeychain(name: String) -> String? {
let keychain = Keychain(service: Globals.bundleIdentifier)
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
do {
return try keychain.getString(name)
} catch {
@ -73,19 +73,21 @@ public class Utils {
}
public static func addPasswordToKeychain(name: String, password: String?) {
let keychain = Keychain(service: Globals.bundleIdentifier)
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
keychain[name] = password
}
public static func removeKeychain(name: String) {
let keychain = Keychain(service: Globals.bundleIdentifier)
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
do {
try keychain.remove(name)
} catch {
print(error)
}
}
public static func removeAllKeychain() {
let keychain = Keychain(service: Globals.bundleIdentifier)
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
do {
try keychain.removeAll()
} catch {

View file

@ -12,6 +12,7 @@ import UIKit
import SwiftyUserDefaults
import ObjectiveGit
import ObjectivePGP
import KeychainAccess
public class PasswordStore {
public static let shared = PasswordStore()
@ -116,7 +117,8 @@ public class PasswordStore {
print(Globals.libraryPath)
print(Globals.documentPathLegacy)
print(Globals.libraryPathLegacy)
migration()
migrateIfNeeded()
do {
if fm.fileExists(atPath: storeURL.path) {
try storeRepository = GTRepository.init(url: storeURL)
@ -127,15 +129,29 @@ public class PasswordStore {
}
}
private func migration() {
private func migrateIfNeeded() {
let needMigration = fm.fileExists(atPath: Globals.documentPathLegacy) && !fm.fileExists(atPath: Globals.documentPath) && fm.fileExists(atPath: Globals.libraryPathLegacy) && !fm.fileExists(atPath: Globals.libraryPath)
guard needMigration == true else {
return
}
do {
// migrate files
try fm.moveItem(atPath: Globals.documentPathLegacy, toPath: Globals.documentPath)
try fm.moveItem(atPath: Globals.libraryPathLegacy, toPath: Globals.libraryPath)
// migrate Defaults
SharedDefaults = Defaults
// migrate Keychain
let keychainLegacy = Keychain(service: Globals.bundleIdentifier)
if let pgpPassphrase = try keychainLegacy.getString("pgpKeyPassphrase") {
Utils.addPasswordToKeychain(name: "pgpKeyPassphrase", password: pgpPassphrase)
}
if let gitSSHPrivateKeyPassphrase = try keychainLegacy.getString("gitSSHPrivateKeyPassphrase") {
Utils.addPasswordToKeychain(name: "gitSSHPrivateKeyPassphrase", password: gitSSHPrivateKeyPassphrase)
}
if let gitPassword = try keychainLegacy.getString("gitPassword") {
Utils.addPasswordToKeychain(name: "gitPassword", password: gitPassword)
}
try keychainLegacy.removeAll()
} catch {
print("Cannot migrate: \(error)")
}