Throw error in case PGP key files cannot be read

This commit is contained in:
Danny Moesch 2019-06-29 10:28:16 +02:00 committed by Mingshen Sun
parent f0003227d5
commit b806175842
4 changed files with 13 additions and 10 deletions

View file

@ -60,6 +60,7 @@
"FailureToSaveContext" = "Fehler beim Speichern des Kontexts: %@"; "FailureToSaveContext" = "Fehler beim Speichern des Kontexts: %@";
"RepositoryRemoteMasterNotFoundError." = "Remote-Branch origin/master wurde nicht gefunden."; "RepositoryRemoteMasterNotFoundError." = "Remote-Branch origin/master wurde nicht gefunden.";
"KeyImportError." = "Schlüssel kann nicht importiert werden."; "KeyImportError." = "Schlüssel kann nicht importiert werden.";
"FileNotFoundError." = "Die Datei '%@' kann nicht gelesen werden.";
"PasswordDuplicatedError." = "Passwort kann nicht hinzugefügt werden; es existiert bereits."; "PasswordDuplicatedError." = "Passwort kann nicht hinzugefügt werden; es existiert bereits.";
"GitResetError." = "Der zuletzt synchronisierte Commit kann nicht identifiziert werden."; "GitResetError." = "Der zuletzt synchronisierte Commit kann nicht identifiziert werden.";
"PGPPublicKeyNotExistError." = "Der öffentliche PGP-Schlüssen existiert nicht"; "PGPPublicKeyNotExistError." = "Der öffentliche PGP-Schlüssen existiert nicht";

View file

@ -61,6 +61,7 @@
"RepositoryRemoteBranchNotFoundError." = "Cannot find remote branch %@."; "RepositoryRemoteBranchNotFoundError." = "Cannot find remote branch %@.";
"RepositoryBranchNotFoundError." = "Branch %@ not found in repository."; "RepositoryBranchNotFoundError." = "Branch %@ not found in repository.";
"KeyImportError." = "Cannot import the key."; "KeyImportError." = "Cannot import the key.";
"FileNotFoundError." = "File '%@' cannot be read.";
"PasswordDuplicatedError." = "Cannot add the password; password is duplicated."; "PasswordDuplicatedError." = "Cannot add the password; password is duplicated.";
"GitResetError." = "Cannot identify the latest synced commit."; "GitResetError." = "Cannot identify the latest synced commit.";
"PgpPublicKeyNotExistError." = "PGP public key doesn't exist."; "PgpPublicKeyNotExistError." = "PGP public key doesn't exist.";

View file

@ -11,6 +11,7 @@ public enum AppError: Error {
case RepositoryRemoteBranchNotFound(_: String) case RepositoryRemoteBranchNotFound(_: String)
case RepositoryBranchNotFound(_: String) case RepositoryBranchNotFound(_: String)
case KeyImport case KeyImport
case ReadingFile(_: String)
case PasswordDuplicated case PasswordDuplicated
case GitReset case GitReset
case GitCommit case GitCommit
@ -26,7 +27,7 @@ extension AppError: LocalizedError {
public var errorDescription: String? { public var errorDescription: String? {
let localizationKey = "\(String(describing: self).prefix(while: { $0 != "(" }))Error." let localizationKey = "\(String(describing: self).prefix(while: { $0 != "(" }))Error."
switch self { switch self {
case let .RepositoryRemoteBranchNotFound(name), let .RepositoryBranchNotFound(name): case let .RepositoryRemoteBranchNotFound(name), let .RepositoryBranchNotFound(name), let .ReadingFile(name):
return localizationKey.localize(name) return localizationKey.localize(name)
default: default:
return localizationKey.localize() return localizationKey.localize()

View file

@ -883,16 +883,16 @@ public class PasswordStore {
} }
public func pgpKeyImportFromFileSharing() throws { public func pgpKeyImportFromFileSharing() throws {
let publicKeyFileUrl = URL(fileURLWithPath: Globals.iTunesFileSharingPGPPublic) guard let publicKeyFileContent = fm.contents(atPath: Globals.iTunesFileSharingPGPPublic) else {
let privateKeyFileUrl = URL(fileURLWithPath: Globals.iTunesFileSharingPGPPrivate) throw AppError.ReadingFile(Globals.iTunesFileSharingPGPPublic)
}
let publicKeyFileContent = try Data(contentsOf: publicKeyFileUrl)
let privateKeyFileContent = try Data(contentsOf: privateKeyFileUrl)
AppKeychain.add(data: publicKeyFileContent, for: PGPKeyType.PUBLIC.rawValue) AppKeychain.add(data: publicKeyFileContent, for: PGPKeyType.PUBLIC.rawValue)
AppKeychain.add(data: privateKeyFileContent, for: PGPKeyType.PRIVATE.rawValue) try fm.removeItem(atPath: Globals.iTunesFileSharingPGPPublic)
try fm.removeItem(at: publicKeyFileUrl) guard let privateKeyFileContent = fm.contents(atPath: Globals.iTunesFileSharingPGPPrivate) else {
try fm.removeItem(at: privateKeyFileUrl) throw AppError.ReadingFile(Globals.iTunesFileSharingPGPPrivate)
}
AppKeychain.add(data: privateKeyFileContent, for: PGPKeyType.PRIVATE.rawValue)
try fm.removeItem(atPath: Globals.iTunesFileSharingPGPPrivate)
} }
} }