Merge pull request #292 from SimplyDanny/make-simulator-untouched-by-tests

Do not influence the Simulator by tests
This commit is contained in:
Yishi Lin 2019-07-21 13:39:53 +08:00 committed by GitHub
commit 032e442d78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 139 additions and 62 deletions

View file

@ -8,37 +8,39 @@
import KeychainAccess
public class AppKeychain {
public class AppKeychain: KeyStore {
public static let shared = AppKeychain()
private static let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
private let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
.accessibility(.whenUnlockedThisDeviceOnly)
.synchronizable(false)
public static func add(data: Data?, for key: String) {
public func add(data: Data?, for key: String) {
keychain[data: key] = data
}
public static func add(string: String?, for key: String) {
public func add(string: String?, for key: String) {
keychain[key] = string
}
public static func contains(key: String) -> Bool {
public func contains(key: String) -> Bool {
return (try? keychain.contains(key)) ?? false
}
public static func get(for key: String) -> Data? {
public func get(for key: String) -> Data? {
return try? keychain.getData(key)
}
public static func get(for key: String) -> String? {
public func get(for key: String) -> String? {
return try? keychain.getString(key)
}
public static func removeContent(for key: String) {
public func removeContent(for key: String) {
try? keychain.remove(key)
}
public static func removeAllContent() {
public func removeAllContent() {
try? keychain.removeAll()
}
}

View file

@ -15,19 +15,17 @@ public class KeyFileManager {
private let keyType: CryptographicKey
private let keyPath: String
private let keyHandler: KeyHandler
private convenience init(keyType: CryptographicKey) {
self.init(keyType: keyType, keyPath: keyType.getFileSharingPath())
}
public init(keyType: CryptographicKey, keyPath: String, keyHandler: @escaping KeyHandler = AppKeychain.add) {
public init(keyType: CryptographicKey, keyPath: String) {
self.keyType = keyType
self.keyPath = keyPath
self.keyHandler = keyHandler
}
public func importKeyAndDeleteFile() throws {
public func importKeyAndDeleteFile(keyHandler: KeyHandler = AppKeychain.shared.add) throws {
guard let keyFileContent = FileManager.default.contents(atPath: keyPath) else {
throw AppError.ReadingFile(URL(fileURLWithPath: keyPath).lastPathComponent)
}

View file

@ -0,0 +1,19 @@
//
// KeyStore.swift
// passKit
//
// Created by Danny Moesch on 20.07.19.
// Copyright © 2019 Bob Sun. All rights reserved.
//
import Foundation
public protocol KeyStore {
func add(data: Data?, for key: String)
func add(string: String?, for key: String)
func contains(key: String) -> Bool
func get(for key: String) -> Data?
func get(for key: String) -> String?
func removeContent(for key: String)
func removeAllContent()
}

View file

@ -12,15 +12,21 @@ import KeychainAccess
import Gopenpgpwrapper
public class PGPAgent {
private let keyStore: KeyStore
public init(keyStore: KeyStore = AppKeychain.shared) {
self.keyStore = keyStore
}
public var pgpKeyID: String?
// PGP passphrase
public var passphrase: String? {
set {
AppKeychain.add(string: newValue, for: "pgpKeyPassphrase")
keyStore.add(string: newValue, for: "pgpKeyPassphrase")
}
get {
return AppKeychain.get(for: "pgpKeyPassphrase")
return keyStore.get(for: "pgpKeyPassphrase")
}
}
@ -68,12 +74,12 @@ public class PGPAgent {
}
// Read the key data from keychain.
guard let pgpKeyData: Data = AppKeychain.get(for: keyType.getKeychainKey()) else {
guard let pgpKeyData: Data = keyStore.get(for: keyType.getKeychainKey()) else {
throw AppError.KeyImport
}
// Remove the key data from keychain temporary, in case the following step crashes repeatedly.
AppKeychain.removeContent(for: keyType.getKeychainKey())
keyStore.removeContent(for: keyType.getKeychainKey())
// Try GopenpgpwrapperReadKey first.
if let key = GopenpgpwrapperReadKey(pgpKeyData) {
@ -83,7 +89,7 @@ public class PGPAgent {
case .PRIVATE:
self.privateKey = key
}
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
keyStore.add(data: pgpKeyData, for: keyType.getKeychainKey())
return
}
@ -98,7 +104,7 @@ public class PGPAgent {
case .PRIVATE:
self.privateKeyV2 = key
}
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
keyStore.add(data: pgpKeyData, for: keyType.getKeychainKey())
return
}
@ -107,19 +113,19 @@ public class PGPAgent {
public func initPGPKey(from url: URL, keyType: PgpKey) throws {
let pgpKeyData = try Data(contentsOf: url)
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
keyStore.add(data: pgpKeyData, for: keyType.getKeychainKey())
try initPGPKey(keyType)
}
public func initPGPKey(with armorKey: String, keyType: PgpKey) throws {
let pgpKeyData = armorKey.data(using: .ascii)!
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
keyStore.add(data: pgpKeyData, for: keyType.getKeychainKey())
try initPGPKey(keyType)
}
public func initPGPKeyFromFileSharing() throws {
try KeyFileManager.PublicPgp.importKeyAndDeleteFile()
try KeyFileManager.PrivatePgp.importKeyAndDeleteFile()
try KeyFileManager.PublicPgp.importKeyAndDeleteFile(keyHandler: keyStore.add)
try KeyFileManager.PrivatePgp.importKeyAndDeleteFile(keyHandler: keyStore.add)
try initPGPKeys()
}
@ -167,8 +173,8 @@ public class PGPAgent {
}
public func removePGPKeys() {
AppKeychain.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
AppKeychain.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
keyStore.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
keyStore.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
passphrase = nil
publicKey = nil
privateKey = nil

View file

@ -12,7 +12,7 @@ public class PasscodeLock {
private static let identifier = Globals.bundleIdentifier + "passcode"
/// Cached passcode to avoid frequent access to Keychain
private var passcode: String? = AppKeychain.get(for: PasscodeLock.identifier)
private var passcode: String? = AppKeychain.shared.get(for: PasscodeLock.identifier)
/// Constructor used to migrate passcode from SharedDefaults to Keychain
private init() {
@ -27,7 +27,7 @@ public class PasscodeLock {
}
public func save(passcode: String) {
AppKeychain.add(string: passcode, for: PasscodeLock.identifier)
AppKeychain.shared.add(string: passcode, for: PasscodeLock.identifier)
self.passcode = passcode
}
@ -36,7 +36,7 @@ public class PasscodeLock {
}
public func delete() {
AppKeychain.removeContent(for: PasscodeLock.identifier)
AppKeychain.shared.removeContent(for: PasscodeLock.identifier)
passcode = nil
}
}

View file

@ -39,19 +39,19 @@ public class PasswordStore {
public var gitPassword: String? {
set {
AppKeychain.add(string: newValue, for: "gitPassword")
AppKeychain.shared.add(string: newValue, for: "gitPassword")
}
get {
return AppKeychain.get(for: "gitPassword")
return AppKeychain.shared.get(for: "gitPassword")
}
}
public var gitSSHPrivateKeyPassphrase: String? {
set {
AppKeychain.add(string: newValue, for: "gitSSHPrivateKeyPassphrase")
AppKeychain.shared.add(string: newValue, for: "gitSSHPrivateKeyPassphrase")
}
get {
return AppKeychain.get(for: "gitSSHPrivateKeyPassphrase")
return AppKeychain.shared.get(for: "gitSSHPrivateKeyPassphrase")
}
}
@ -128,7 +128,7 @@ public class PasswordStore {
}
public func initGitSSHKey(with armorKey: String) throws {
AppKeychain.add(string: armorKey, for: SshKey.PRIVATE.getKeychainKey())
AppKeychain.shared.add(string: armorKey, for: SshKey.PRIVATE.getKeychainKey())
}
public func repositoryExisted() -> Bool {
@ -640,7 +640,7 @@ public class PasswordStore {
self.pgpAgent.removePGPKeys()
AppKeychain.removeAllContent()
AppKeychain.shared.removeAllContent()
deleteCoreData(entityName: "PasswordEntity")
@ -720,7 +720,7 @@ public class PasswordStore {
Defaults.remove(.gitSSHKeySource)
Defaults.remove(.gitSSHPrivateKeyArmor)
Defaults.remove(.gitSSHPrivateKeyURL)
AppKeychain.removeContent(for: SshKey.PRIVATE.getKeychainKey())
AppKeychain.shared.removeContent(for: SshKey.PRIVATE.getKeychainKey())
gitSSHPrivateKeyPassphrase = nil
}