secretive/Sources/SecretAgent/AppDelegate.swift

87 lines
3.1 KiB
Swift
Raw Normal View History

2020-03-04 07:14:38 +00:00
import Cocoa
2020-03-22 01:43:26 +00:00
import OSLog
import Combine
2020-03-04 07:14:38 +00:00
import SecretKit
import SecureEnclaveSecretKit
import SmartCardSecretKit
import SecretAgentKit
2020-03-22 01:43:26 +00:00
import Brief
2020-03-04 07:14:38 +00:00
2022-01-03 06:15:18 +00:00
import SecretKit
import SecretAgentKitProtocol
2020-03-04 07:14:38 +00:00
@NSApplicationMain
2022-01-03 06:15:18 +00:00
class AppDelegate: NSObject, NSApplicationDelegate, AgentProtocol {
2020-03-04 07:14:38 +00:00
2020-09-22 06:12:50 +00:00
private let storeList: SecretStoreList = {
2020-03-09 04:11:59 +00:00
let list = SecretStoreList()
list.add(store: SecureEnclave.Store())
list.add(store: SmartCard.Store())
return list
}()
2020-09-22 06:12:50 +00:00
private let updater = Updater(checkOnLaunch: false)
private let notifier = Notifier()
2022-01-03 00:07:33 +00:00
private let publicKeyFileStoreController = PublicKeyFileStoreController()
2020-09-22 06:12:50 +00:00
private lazy var agent: Agent = {
2020-03-17 07:56:55 +00:00
Agent(storeList: storeList, witness: notifier)
2020-03-04 07:14:38 +00:00
}()
2020-09-22 06:12:50 +00:00
private lazy var socketController: SocketController = {
2020-03-04 07:14:38 +00:00
let path = (NSHomeDirectory() as NSString).appendingPathComponent("socket.ssh") as String
return SocketController(path: path)
}()
2020-05-16 06:19:00 +00:00
private var updateSink: AnyCancellable?
2022-01-03 06:15:18 +00:00
private let logger = Logger()
var delegate: ServiceDelegate? = nil
let listener = NSXPCListener(machServiceName: Bundle.main.bundleIdentifier!)
2020-03-04 07:14:38 +00:00
func applicationDidFinishLaunching(_ aNotification: Notification) {
2022-01-03 06:15:18 +00:00
logger.debug("SecretAgent finished launching")
2020-03-04 07:14:38 +00:00
DispatchQueue.main.async {
2020-03-24 06:22:22 +00:00
self.socketController.handler = self.agent.handle(reader:writer:)
2020-03-04 07:14:38 +00:00
}
2022-01-03 00:15:22 +00:00
try? publicKeyFileStoreController.generatePublicKeys(for: storeList.stores.flatMap({ $0.secrets }), clear: true)
2020-03-04 07:14:38 +00:00
notifier.prompt()
updateSink = updater.$update.sink { update in
guard let update = update else { return }
self.notifier.notify(update: update, ignore: self.updater.ignore(release:))
2020-03-22 01:43:26 +00:00
}
2022-01-03 06:52:53 +00:00
// TODO: REMOVE
notifier.notify(update: Release(name: "Test", prerelease: false, html_url: URL(string: "https://example.com")!, body: ""), ignore: nil)
2022-01-03 06:15:18 +00:00
connect()
2020-03-04 07:14:38 +00:00
}
2022-01-03 06:15:18 +00:00
func connect() {
delegate = ServiceDelegate(exportedObject: self)
listener.delegate = delegate
listener.resume()
}
func updatedStore(withID id: UUID) async throws {
2022-01-03 06:52:53 +00:00
// TODO: REMOVE
notifier.notify(update: Release(name: "UPDATESTORE", prerelease: false, html_url: URL(string: "https://example.com")!, body: ""), ignore: nil)
2022-01-03 06:15:18 +00:00
logger.debug("Reloading keys for store with id: \(id)")
guard let store = storeList.modifiableStore, store.id == id else { throw AgentProtocolStoreNotFoundError() }
try store.reload()
try publicKeyFileStoreController.generatePublicKeys(for: storeList.stores.flatMap({ $0.secrets }), clear: true)
2022-01-03 00:07:33 +00:00
}
2020-03-04 07:14:38 +00:00
}
2022-01-03 06:52:53 +00:00
// TODO: MOVE
class ServiceDelegate: NSObject, NSXPCListenerDelegate {
2022-01-03 06:15:18 +00:00
2022-01-03 06:52:53 +00:00
let exported: AgentProtocol
2022-01-03 06:15:18 +00:00
2022-01-03 06:52:53 +00:00
init(exportedObject: AgentProtocol) {
self.exported = exportedObject
}
2022-01-03 06:15:18 +00:00
2022-01-03 06:52:53 +00:00
func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
newConnection.exportedInterface = NSXPCInterface(with: AgentProtocol.self)
newConnection.exportedObject = exported
newConnection.resume()
return true
2022-01-03 06:15:18 +00:00
}
2022-01-03 06:52:53 +00:00
}