2022-01-09 21:38:39 -08:00
|
|
|
//
|
|
|
|
|
// YubiKeyConnection.swift
|
|
|
|
|
// passKit
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 2022 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import YubiKit
|
|
|
|
|
|
|
|
|
|
public class YubiKeyConnection: NSObject {
|
|
|
|
|
public static let shared = YubiKeyConnection()
|
|
|
|
|
|
|
|
|
|
var accessoryConnection: YKFAccessoryConnection?
|
|
|
|
|
var nfcConnection: YKFNFCConnection?
|
|
|
|
|
var connectionCallback: ((_ connection: YKFConnectionProtocol) -> Void)?
|
|
|
|
|
var cancellationCallback: ((_ error: Error) -> Void)?
|
|
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
|
super.init()
|
2023-03-17 22:20:50 -07:00
|
|
|
if YubiKitDeviceCapabilities.supportsISO7816NFCTags {
|
2023-03-12 14:46:37 -07:00
|
|
|
YubiKitManager.shared.delegate = self
|
|
|
|
|
YubiKitManager.shared.startAccessoryConnection()
|
|
|
|
|
}
|
2022-01-09 21:38:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func connection(cancellation: @escaping (_ error: Error) -> Void, completion: @escaping (_ connection: YKFConnectionProtocol) -> Void) {
|
|
|
|
|
if let connection = accessoryConnection {
|
|
|
|
|
completion(connection)
|
|
|
|
|
} else {
|
|
|
|
|
connectionCallback = completion
|
2023-02-04 15:27:37 -08:00
|
|
|
YubiKitManager.shared.startNFCConnection()
|
2022-01-09 21:38:39 -08:00
|
|
|
}
|
|
|
|
|
cancellationCallback = cancellation
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension YubiKeyConnection: YKFManagerDelegate {
|
|
|
|
|
public func didConnectNFC(_ connection: YKFNFCConnection) {
|
|
|
|
|
nfcConnection = connection
|
|
|
|
|
if let callback = connectionCallback {
|
|
|
|
|
callback(connection)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func didDisconnectNFC(_: YKFNFCConnection, error _: Error?) {
|
|
|
|
|
nfcConnection = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func didConnectAccessory(_ connection: YKFAccessoryConnection) {
|
|
|
|
|
accessoryConnection = connection
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func didDisconnectAccessory(_: YKFAccessoryConnection, error _: Error?) {
|
|
|
|
|
accessoryConnection = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func didFailConnectingNFC(_ error: Error) {
|
|
|
|
|
if let callback = cancellationCallback {
|
|
|
|
|
callback(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|