Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Sources/OAuthKit/Network/NetworkMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ import Observation
/// A type that broadcasts network reachability via Combine event publishing.
@MainActor
@Observable
final class NetworkMonitor {
public final class NetworkMonitor {

@ObservationIgnored
private let pathMonitor = NWPathMonitor()

/// Returns true if the network has an available wifi interface.
var onWifi = false
public var onWifi = false
/// Returns true if the network has an available cellular interface.
var onCellular = false
public var onCellular = false
/// Returns true if the network has an wired ethernet interface.
var onWiredEthernet = false
public var onWiredEthernet = false

/// Returns true if the network is online with any available interface.
var isOnline: Bool {
public var isOnline: Bool {
onWifi || onCellular || onWiredEthernet
}

/// Initializer.
init() { }
public init() { }

/// Starts the network monitor (conforms to AsyncSequence).
func start() async {
public func start() async {
for await path in pathMonitor {
handle(path: path)
}
Expand Down
9 changes: 3 additions & 6 deletions Sources/OAuthKit/OAuth+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ extension OAuth {
/// - deviceCode: the device code data
/// - Returns: a `/token` request that can be used for polling
static func token(provider: Provider, deviceCode: DeviceCode) -> URLRequest? {
guard var urlComponents = URLComponents(string: provider.accessTokenURL.absoluteString) else {
return nil
}
guard var urlComponents = URLComponents(string: provider.accessTokenURL.absoluteString) else { return nil }
urlComponents.queryItems = buildQueryItems(provider: provider, deviceCode: deviceCode)
guard let url = urlComponents.url else { return nil }
var request = URLRequest(url: url)
Expand Down Expand Up @@ -144,9 +142,8 @@ extension OAuth {
/// - provider: the oauth provider
/// - Returns: the url request
static func device(provider: Provider) -> URLRequest? {
guard let deviceCodeURL = provider.deviceCodeURL, var urlComponents = URLComponents(string: deviceCodeURL.absoluteString) else {
return nil
}
guard let deviceCodeURL = provider.deviceCodeURL,
var urlComponents = URLComponents(string: deviceCodeURL.absoluteString) else { return nil }
urlComponents.queryItems = buildQueryItems(provider: provider, grantType: .deviceCode)
guard let url = urlComponents.url else { return nil }
var request = URLRequest(url: url)
Expand Down
22 changes: 7 additions & 15 deletions Sources/OAuthKit/OAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,7 @@ extension OAuth {
/// - Parameters:
/// - provider: the provider to request a refresh token for
func refreshToken(provider: Provider) async {
guard let auth: OAuth.Authorization = try? keychain.get(key: provider.id) else {
return
}
guard let auth: OAuth.Authorization = try? keychain.get(key: provider.id) else { return }

// If we can't build a refresh request simply bail as no refresh token
// was returned in the original auth request
Expand Down Expand Up @@ -422,8 +420,7 @@ extension OAuth {
func requestDeviceCode(provider: Provider) async {
guard let request = Request.device(provider: provider) else { return }
guard let (data, response) = try? await urlSession.data(for: request) else {
publish(state: .empty)
return
return publish(state: .empty)
}

if provider.debug {
Expand All @@ -434,8 +431,7 @@ extension OAuth {

// Decode the device code
guard let deviceCode = try? decoder.decode(DeviceCode.self, from: data) else {
publish(state: .empty)
return
return publish(state: .empty)
}

// Publish the state
Expand All @@ -448,8 +444,7 @@ extension OAuth {
func requestClientCredentials(provider: Provider) async {
guard let request = Request.token(provider: provider) else { return }
guard let (data, response) = try? await urlSession.data(for: request) else {
publish(state: .empty)
return
return publish(state: .empty)
}

if provider.debug {
Expand Down Expand Up @@ -479,13 +474,11 @@ extension OAuth {
func poll(provider: Provider, deviceCode: DeviceCode) async {

guard !deviceCode.isExpired, let request = Request.token(provider: provider, deviceCode: deviceCode) else {
publish(state: .empty)
return
return publish(state: .empty)
}

guard let (data, response) = try? await urlSession.data(for: request) else {
publish(state: .empty)
return
return publish(state: .empty)
}

if provider.debug {
Expand All @@ -497,8 +490,7 @@ extension OAuth {
/// If we received something other than a 200 response or we can't decode the token then restart the polling
guard response.isOK, let token = try? decoder.decode(Token.self, from: data) else {
// Reschedule the polling task
schedule(provider: provider, deviceCode: deviceCode)
return
return schedule(provider: provider, deviceCode: deviceCode)
}

// Store the authorization
Expand Down
18 changes: 18 additions & 0 deletions Tests/OAuthKitTests/UtilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,23 @@ struct UtilityTests {
let executed = try await task.value
#expect(executed)
}

/// Tests the network monitor
@MainActor
@Test("Network Monitor")
func whenNetworkMonitoring() async throws {
let monitor = NetworkMonitor()
#expect(monitor.isOnline == false)
withObservationTracking {
_ = monitor.isOnline
} onChange: {
Task { @MainActor in
#expect(monitor.isOnline)
}
}
Task {
await monitor.start()
}
}
}