@@ -51,6 +51,18 @@ public final class OAuth: Sendable {
5151 #if os(macOS) || os(iOS) || os(visionOS)
5252 @ObservationIgnored
5353 var context : LAContext = . init( )
54+
55+ var policy : LAPolicy = {
56+ #if os(macOS) || os(iOS)
57+ if #available( macOS 15 . 0 , iOS 18 . 0 , * ) {
58+ return . deviceOwnerAuthenticationWithBiometricsOrCompanion
59+ }
60+ return . deviceOwnerAuthenticationWithBiometrics
61+ #else
62+ return . deviceOwnerAuthenticationWithBiometrics
63+ #endif
64+ } ( )
65+
5466 #endif
5567
5668 @ObservationIgnored
@@ -117,15 +129,15 @@ public extension OAuth {
117129 state = . authorizing( provider, grantType)
118130 case . deviceCode:
119131 state = . requestingDeviceCode( provider)
120- Task . immediate {
132+ task { [ self ] in
121133 await requestDeviceCode ( provider: provider)
122134 }
123135 case . clientCredentials:
124- Task . immediate {
136+ task { [ self ] in
125137 await requestClientCredentials ( provider: provider)
126138 }
127139 case . refreshToken:
128- Task . immediate {
140+ task { [ self ] in
129141 await refreshToken ( provider: provider)
130142 }
131143 }
@@ -138,7 +150,7 @@ public extension OAuth {
138150 /// - code: the code to exchange
139151 /// - pkce: the pkce data
140152 func token( provider: Provider , code: String , pkce: PKCE ? = nil ) {
141- Task . immediate {
153+ task { [ self ] in
142154 await requestToken ( provider: provider, code: code, pkce: pkce)
143155 }
144156 }
@@ -233,19 +245,12 @@ private extension OAuth {
233245
234246 #if os(macOS) || os(iOS) || os(visionOS)
235247 let localizedReason = context. localizedReason. isNotEmpty ? context. localizedReason: defaultAuthenticationWithBiometricsOrCompanionReason
236- #if os(macOS) || os(iOS)
237- let policy : LAPolicy = . deviceOwnerAuthenticationWithBiometricsOrCompanion
238- #else
239- let policy : LAPolicy = . deviceOwnerAuthenticationWithBiometrics
240- #endif
241248 var error : NSError ?
242249 if context. canEvaluatePolicy ( policy, error: & error) {
243250 context. evaluatePolicy ( policy, localizedReason: localizedReason) { [ weak self] success, error in
244- guard let self else { return }
245- Task . immediate { @MainActor in
246- if success {
247- self . loadAuthorizations ( )
248- }
251+ guard let self, success else { return }
252+ Task { @MainActor [ self ] in
253+ loadAuthorizations ( )
249254 }
250255 }
251256 }
@@ -257,7 +262,7 @@ private extension OAuth {
257262
258263 /// Starts the network monitor.
259264 func monitor( ) {
260- Task {
265+ task { [ self ] in
261266 await networkMonitor. start ( )
262267 }
263268 }
@@ -284,7 +289,7 @@ private extension OAuth {
284289 let timeInterval : TimeInterval = . init( deviceCode. interval)
285290 let task = Task . delayed ( timeInterval: timeInterval) { [ weak self] in
286291 guard let self else { return }
287- await self . poll ( provider: provider, deviceCode: deviceCode)
292+ await poll ( provider: provider, deviceCode: deviceCode)
288293 }
289294 tasks. append ( task)
290295 }
@@ -304,18 +309,30 @@ private extension OAuth {
304309 // Schedule the auto refresh task
305310 let task = Task . delayed ( timeInterval: timeInterval) { [ weak self] in
306311 guard let self else { return }
307- await self . refreshToken ( provider: provider)
312+ await refreshToken ( provider: provider)
308313 }
309314 tasks. append ( task)
310315 } else {
311316 // Execute the task immediately
312- Task . immediate {
317+ task { [ self ] in
313318 await refreshToken ( provider: provider)
314319 }
315320 }
316321 }
317322 }
318323 }
324+
325+ /// Create and immediately start running a new detached task in the context of this actor.
326+ /// - Parameters:
327+ /// - priority: the task priority
328+ /// - operation: the operation to be run immediately upon entering the task.
329+ func task( priority: TaskPriority = . high, operation: sending @escaping @isolated ( any) ( ) async throws -> Void ) {
330+ if #available( macOS 26 , iOS 26 , watchOS 26 , tvOS 26 , visionOS 26 , * ) {
331+ Task . immediate ( operation: operation)
332+ } else {
333+ Task ( priority: priority, operation: operation)
334+ }
335+ }
319336}
320337
321338// MARK: URLRequests
0 commit comments