2019-06-29 23:09:24 +02:00
|
|
|
//
|
|
|
|
|
// KeyFileManager.swift
|
|
|
|
|
// passKit
|
|
|
|
|
//
|
|
|
|
|
// Created by Danny Moesch on 29.06.19.
|
|
|
|
|
// Copyright © 2019 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
public class KeyFileManager {
|
|
|
|
|
public typealias KeyHandler = (Data, String) -> ()
|
|
|
|
|
|
2019-07-02 20:20:56 +02:00
|
|
|
public static let PublicPgp = KeyFileManager(keyType: PgpKey.PUBLIC)
|
|
|
|
|
public static let PrivatePgp = KeyFileManager(keyType: PgpKey.PRIVATE)
|
2019-07-02 20:28:47 +02:00
|
|
|
public static let PrivateSsh = KeyFileManager(keyType: SshKey.PRIVATE)
|
2019-06-29 23:09:24 +02:00
|
|
|
|
2019-07-02 20:20:56 +02:00
|
|
|
private let keyType: CryptographicKey
|
2019-06-29 23:09:24 +02:00
|
|
|
private let keyPath: String
|
|
|
|
|
|
2019-07-02 20:20:56 +02:00
|
|
|
private convenience init(keyType: CryptographicKey) {
|
2019-06-29 23:09:24 +02:00
|
|
|
self.init(keyType: keyType, keyPath: keyType.getFileSharingPath())
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-20 23:02:38 +02:00
|
|
|
public init(keyType: CryptographicKey, keyPath: String) {
|
2019-06-29 23:09:24 +02:00
|
|
|
self.keyType = keyType
|
|
|
|
|
self.keyPath = keyPath
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-20 23:02:38 +02:00
|
|
|
public func importKeyAndDeleteFile(keyHandler: KeyHandler = AppKeychain.shared.add) throws {
|
2019-06-29 23:09:24 +02:00
|
|
|
guard let keyFileContent = FileManager.default.contents(atPath: keyPath) else {
|
|
|
|
|
throw AppError.ReadingFile(URL(fileURLWithPath: keyPath).lastPathComponent)
|
|
|
|
|
}
|
|
|
|
|
keyHandler(keyFileContent, keyType.getKeychainKey())
|
|
|
|
|
try FileManager.default.removeItem(atPath: keyPath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func doesKeyFileExist() -> Bool {
|
|
|
|
|
return FileManager.default.fileExists(atPath: keyPath)
|
|
|
|
|
}
|
|
|
|
|
}
|