Polish initial PGP key error messages

This commit is contained in:
Yishi Lin 2017-04-09 00:25:04 +08:00
parent bef9e0251c
commit c769c4126b

View file

@ -220,27 +220,21 @@ class PasswordStore {
} }
public func initPGPKey(_ keyType: PGPKeyType) throws { public func initPGPKey(_ keyType: PGPKeyType) throws {
var keyPath = ""
switch keyType { switch keyType {
case .public: case .public:
keyPath = Globals.pgpPublicKeyPath let keyPath = Globals.pgpPublicKeyPath
case .secret: self.publicKey = importKey(from: keyPath)
keyPath = Globals.pgpPrivateKeyPath if self.publicKey == nil {
default: throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import the public PGP key."])
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key."])
}
if let key = importKey(from: keyPath) {
switch keyType {
case .public:
self.publicKey = key
case .secret:
self.privateKey = key
default:
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key."])
} }
} else { case .secret:
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key."]) let keyPath = Globals.pgpPrivateKeyPath
self.privateKey = importKey(from: keyPath)
if self.privateKey == nil {
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import the private PGP key."])
}
default:
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key: unknown PGP key type."])
} }
} }
@ -272,9 +266,7 @@ class PasswordStore {
let fm = FileManager.default let fm = FileManager.default
if fm.fileExists(atPath: keyPath) { if fm.fileExists(atPath: keyPath) {
if let keys = pgp.importKeys(fromFile: keyPath, allowDuplicates: false) as? [PGPKey] { if let keys = pgp.importKeys(fromFile: keyPath, allowDuplicates: false) as? [PGPKey] {
if keys.count > 0 { return keys.first
return keys[0]
}
} }
} }
return nil return nil