Introduce KeyStore protocol in order to provide specialized key store implementations for tests
With the DictBasedKeychain the main AppKeychain is not influenced by tests. The previous implementation led to an empty Keychain requiring a new setup of the simulator.
This commit is contained in:
parent
b42401343d
commit
5c7d4e55a4
6 changed files with 93 additions and 24 deletions
|
|
@ -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.shared.add(string: newValue, for: "pgpKeyPassphrase")
|
||||
keyStore.add(string: newValue, for: "pgpKeyPassphrase")
|
||||
}
|
||||
get {
|
||||
return AppKeychain.shared.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.shared.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.shared.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.shared.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.shared.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.shared.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.shared.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.shared.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
|
||||
AppKeychain.shared.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
|
||||
keyStore.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
|
||||
keyStore.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
|
||||
passphrase = nil
|
||||
publicKey = nil
|
||||
privateKey = nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue