diff --git a/Package.swift b/Package.swift index ab74f96..cd01699 100644 --- a/Package.swift +++ b/Package.swift @@ -17,16 +17,23 @@ let package = Package( name: "OAuthKit", targets: ["OAuthKit"]) ], + dependencies: [ + // Android / Linux Dependencies + .package(url: "https://github.com/apple/swift-crypto", from: .init(4, 5, 0)) + ], targets: [ .target( name: "OAuthKit", + dependencies: [ + .product(name: "Crypto", + package: "swift-crypto", + condition: .when(platforms: [.android, .linux]) + ) + ], linkerSettings: [ - .linkedFramework("CryptoKit"), - .linkedFramework("LocalAuthentication", .when( - platforms: [.iOS] - )), - .linkedFramework("Network"), - .linkedFramework("Security") + .linkedFramework("LocalAuthentication", + .when(platforms: [.iOS]) + ), ] ), .testTarget( diff --git a/Sources/OAuthKit/Extensions/Data+Extensions.swift b/Sources/OAuthKit/Extensions/Data+Extensions.swift index c1a022d..beed811 100644 --- a/Sources/OAuthKit/Extensions/Data+Extensions.swift +++ b/Sources/OAuthKit/Extensions/Data+Extensions.swift @@ -5,7 +5,11 @@ // Created by Kevin McKee // +#if canImport(CryptoKit) import CryptoKit +#else +import Crypto +#endif import Foundation extension Data { @@ -43,7 +47,16 @@ extension Data { /// - Returns: an array of cryptographically secure random bytes static func secureRandom(count: Int = 32) -> Data { var bytes = [UInt8](repeating: 0, count: count) + #if canImport(CryptoKit) + // Apple _ = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes) + #else + // Android / Linux + var generator = SystemRandomNumberGenerator() + for i in 0.. Bool { + + #if canImport(Security) let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: key ] let status = SecItemDelete(query as CFDictionary) return status == noErr + #else + // TODO: Android / Linux storage + return false + #endif } /// Builds the account key by prefixing the specified key with the application tag. diff --git a/Sources/OAuthKit/Network/NetworkMonitor.swift b/Sources/OAuthKit/Network/NetworkMonitor.swift index 6ec1113..f48207a 100644 --- a/Sources/OAuthKit/Network/NetworkMonitor.swift +++ b/Sources/OAuthKit/Network/NetworkMonitor.swift @@ -5,7 +5,9 @@ // Created by Kevin McKee // +#if canImport(Network) import Network +#endif import Observation /// An `Observable` type that publishes network reachability information. @@ -16,8 +18,10 @@ public final class NetworkMonitor: Sendable { // The shared singleton network monitor. public static let shared: NetworkMonitor = .init() + #if canImport(Network) @ObservationIgnored private let pathMonitor = NWPathMonitor() + #endif /// Flag indicating if monitoring is currently active or not. public private(set) var isMonitoring = false @@ -39,13 +43,17 @@ public final class NetworkMonitor: Sendable { /// Starts the network monitor (conforms to AsyncSequence). public func start() async { + #if canImport(Network) guard !isMonitoring else { return } isMonitoring.toggle() for await path in pathMonitor { handle(path: path) } + #endif } + #if canImport(Network) + /// Handles the snapshot view of the network path state. /// - Parameter path: the snapshot view of the network path state private func handle(path: NWPath) { @@ -53,4 +61,6 @@ public final class NetworkMonitor: Sendable { onCellular = path.usesInterfaceType(.cellular) onWiredEthernet = path.usesInterfaceType(.wiredEthernet) } + + #endif } diff --git a/Sources/OAuthKit/OAuth+Request.swift b/Sources/OAuthKit/OAuth+Request.swift index f850c39..ef36426 100644 --- a/Sources/OAuthKit/OAuth+Request.swift +++ b/Sources/OAuthKit/OAuth+Request.swift @@ -6,6 +6,9 @@ // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif private let httpPost = "POST" private let httpAcceptHeaderField = "Accept" diff --git a/Sources/OAuthKit/OAuth.swift b/Sources/OAuthKit/OAuth.swift index 178b995..c91de1a 100644 --- a/Sources/OAuthKit/OAuth.swift +++ b/Sources/OAuthKit/OAuth.swift @@ -5,6 +5,9 @@ // Created by Kevin McKee // import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif #if canImport(LocalAuthentication) import LocalAuthentication #endif