Skip to content

Commit

Permalink
Documentation for ConnectionHelper and removed the AsyncSequences for…
Browse files Browse the repository at this point in the history
… any connections since they didn't make any sense.
  • Loading branch information
jensutbult committed Nov 27, 2023
1 parent 0a6082c commit 78c6b8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
59 changes: 30 additions & 29 deletions YubiKit/YubiKit/Utilities/ConnectionHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,35 @@ import Foundation
import CoreNFC
#endif

/// ConnectionHelper simplifies the creation of different connections to the YubiKey by automate
/// much of the work involved in handling multiple different connection at the same time.
/// ConnectionHelper simplifies the creation of different connections to the YubiKey. It can either return
/// a connection of any type from a single function or provide a AsyncSequence implementation allowing
/// you to use a for loop awaiting YubiKeys to be inserted and removed from a device.
///
/// The ConnectionHelper provides functions to simplify implementation supporting different types of
/// connections.
///
/// ```swift
///// Get a Lightning or a SmartCard connection to the YubiKey
///let wiredConnection = try await ConnectionHelper.anyWiredConnection()
///
///// Get a wired or an NFC connection to the YubiKey. If no wired YubiKey
///// is present and the device supports NFC the SDK will start a NFC scan.
///let anyConnection = try await ConnectionHelper.anyConnection()
///
///// Create a AsyncSequence of wired connections to the YubiKey. Every
///// time a YubiKey is inserted it will return a new Connection.
///for try await connection in ConnectionHelper.wiredConnections() {
/// let session = try await OATHSession.session(withConnection: connection)
/// // Use session to calculate codes
/// await connection.connectionDidClose()
/// // Clean up when YubiKey is removed from device
///}
/// ```
public enum ConnectionHelper {

/// Returns a Connection of any type. If a USB-C or Lightning YubiKey is present a connection to that key will be returned, otherwise
/// the NFCConnection will start scanning for a YubiKey.
/// >Note: LightningConnection and NFCConnection are only available on iOS.
public static func anyConnection(nfcAlertMessage: String? = nil) async throws -> Connection {
let connection = try await withThrowingTaskGroup(of: Connection.self) { group -> Connection in
#if os(iOS)
Expand All @@ -45,7 +70,8 @@ public enum ConnectionHelper {
return connection
}


/// Returns either a LightningConnection or a SmartCardConnection. If a YubiKey is present it will return the connection
/// immediately. If no key is present it will wait and return the connection once a YubiKey has been inserted into the device.
public static func anyWiredConnection() async throws -> Connection {
let connection = try await withThrowingTaskGroup(of: Connection.self) { group -> Connection in
#if os(iOS)
Expand All @@ -63,19 +89,11 @@ public enum ConnectionHelper {
return connection
}

/// Returns an AsyncSequence of wired connections.
public static func wiredConnections() -> ConnectionHelper.AnyWiredConnections {
return AnyWiredConnections()
}

public static func anyConnections() -> ConnectionHelper.AnyConnections {
return AnyConnections()
}
#if os(iOS)
public static func startNFC() async throws {
let _ = try await NFCConnection.connection()
}
#endif

public struct AnyWiredConnections: AsyncSequence {
public typealias Element = Connection
var current: Connection? = nil
Expand All @@ -91,21 +109,4 @@ public enum ConnectionHelper {
AsyncIterator()
}
}

public struct AnyConnections: AsyncSequence {
public typealias Element = Connection
var current: Connection? = nil
public struct AsyncIterator: AsyncIteratorProtocol {
mutating public func next() async -> Element? {
while true {
return try? await ConnectionHelper.anyConnection()
}
}
}

public func makeAsyncIterator() -> AsyncIterator {
AsyncIterator()
}
}

}
2 changes: 2 additions & 0 deletions YubiKit/YubiKit/YubiKit.docc/Resources/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ To add support for the 5Ci YubiKey that connect to the iPhone via the Lightning

As a final step build the documentation for the SDK by selecting "Product" -> "Build Documention" in Xcode. This will give you
access to the YubiKit Framework documentation from the Developer Documentation window in Xcode.

> Note: Make sure to select an iOS target when building the documentation. If you build it with macOS as the target destination LightningConnection and NFCConnection will not be included in the documentation.

0 comments on commit 78c6b8e

Please sign in to comment.