Skip to content

Commit 25bb5fd

Browse files
committed
Fix isolation issues for iOS target
1 parent 7c46761 commit 25bb5fd

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

Sources/iOS/OAuth2Authorizer+iOS.swift

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This authorizer takes care of iOS-specific tasks when showing the authorization
3232

3333
You can subclass this class and override `willPresent(viewController:naviController:)` in order to further customize presentation of the UI.
3434
*/
35+
@OAuth2Actor
3536
open class OAuth2Authorizer: OAuth2AuthorizerUI {
3637

3738
/// The OAuth2 instance this authorizer belongs to.
@@ -64,12 +65,14 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
6465
public func openAuthorizeURLInBrowser(_ url: URL) throws {
6566

6667
#if !P2_APP_EXTENSIONS && !os(visionOS)
67-
guard UIApplication.shared.canOpenURL(url) else {
68-
throw OAuth2Error.unableToOpenAuthorizeURL
69-
}
70-
UIApplication.shared.open(url) { didOpen in
71-
if !didOpen {
72-
self.oauth2.logger?.warn("OAuth2", msg: "Unable to open authorize URL")
68+
Task {
69+
guard await UIApplication.shared.canOpenURL(url) else {
70+
throw OAuth2Error.unableToOpenAuthorizeURL
71+
}
72+
await UIApplication.shared.open(url) { didOpen in
73+
if !didOpen {
74+
self.oauth2.logger?.warn("OAuth2", msg: "Unable to open authorize URL")
75+
}
7376
}
7477
}
7578
#else
@@ -84,7 +87,7 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
8487
- parameter with: The configuration to be used; usually uses the instance's `authConfig`
8588
- parameter at: The authorize URL to open
8689
*/
87-
public func authorizeEmbedded(with config: OAuth2AuthConfig, at url: URL) throws {
90+
public func authorizeEmbedded(with config: OAuth2AuthConfig, at url: URL) async throws {
8891
if config.ui.useAuthenticationSession {
8992
guard let redirect = oauth2.redirect else {
9093
throw OAuth2Error.noRedirectURL
@@ -99,11 +102,13 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
99102
throw (nil == config.authorizeContext) ? OAuth2Error.noAuthorizationContext : OAuth2Error.invalidAuthorizationContext
100103
}
101104

102-
let web = try authorizeSafariEmbedded(from: controller, at: url)
105+
let web = try await authorizeSafariEmbedded(from: controller, at: url)
103106
if config.authorizeEmbeddedAutoDismiss {
104107
oauth2.internalAfterAuthorizeOrFail = { wasFailure, error in
105108
self.safariViewDelegate = nil
106-
web.dismiss(animated: true)
109+
Task {
110+
await web.dismiss(animated: true)
111+
}
107112
}
108113
}
109114
#endif
@@ -199,23 +204,26 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
199204
- returns: SFSafariViewController, being already presented automatically
200205
*/
201206
@discardableResult
202-
public func authorizeSafariEmbedded(from controller: UIViewController, at url: URL) throws -> SFSafariViewController {
207+
public func authorizeSafariEmbedded(from controller: UIViewController, at url: URL) async throws -> SFSafariViewController {
203208
safariViewDelegate = OAuth2SFViewControllerDelegate(authorizer: self)
204-
let web = SFSafariViewController(url: url)
205-
web.title = oauth2.authConfig.ui.title
206-
web.delegate = safariViewDelegate
207-
if let barTint = oauth2.authConfig.ui.barTintColor {
208-
web.preferredBarTintColor = barTint
209-
}
210-
if let tint = oauth2.authConfig.ui.controlTintColor {
211-
web.preferredControlTintColor = tint
212-
}
213-
web.modalPresentationStyle = oauth2.authConfig.ui.modalPresentationStyle
214-
215-
willPresent(viewController: web, in: nil)
216-
controller.present(web, animated: true, completion: nil)
217-
web.presentationController?.delegate = safariViewDelegate
218-
return web
209+
210+
return await Task { @MainActor in
211+
let web = SFSafariViewController(url: url)
212+
web.title = await oauth2.authConfig.ui.title
213+
web.delegate = await safariViewDelegate
214+
if let barTint = await oauth2.authConfig.ui.barTintColor {
215+
web.preferredBarTintColor = barTint
216+
}
217+
if let tint = await oauth2.authConfig.ui.controlTintColor {
218+
web.preferredControlTintColor = tint
219+
}
220+
web.modalPresentationStyle = await oauth2.authConfig.ui.modalPresentationStyle
221+
222+
await willPresent(viewController: web, in: nil)
223+
controller.present(web, animated: true, completion: nil)
224+
web.presentationController?.delegate = await safariViewDelegate
225+
return web
226+
}.value
219227
}
220228

221229

@@ -236,6 +244,7 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
236244
/**
237245
A custom `SFSafariViewControllerDelegate` that we use with the safari view controller.
238246
*/
247+
@OAuth2Actor
239248
class OAuth2SFViewControllerDelegate: NSObject, SFSafariViewControllerDelegate, UIAdaptivePresentationControllerDelegate {
240249

241250
weak var authorizer: OAuth2Authorizer?
@@ -245,19 +254,24 @@ class OAuth2SFViewControllerDelegate: NSObject, SFSafariViewControllerDelegate,
245254
}
246255

247256
@available(iOS 9.0, *)
248-
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
249-
authorizer?.safariViewControllerDidCancel(controller)
257+
nonisolated func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
258+
Task { @OAuth2Actor in
259+
authorizer?.safariViewControllerDidCancel(controller)
260+
}
250261
}
251262

252263
// called in case ViewController is dismissed via pulling down the presented sheet.
253264
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
254265
guard let safariViewController = presentationController.presentedViewController as? SFSafariViewController else { return }
255-
authorizer?.safariViewControllerDidCancel(safariViewController)
266+
Task {
267+
await authorizer?.safariViewControllerDidCancel(safariViewController)
268+
}
256269
}
257270
}
258271
#endif
259272

260273
@available(iOS 13.0, *)
274+
@OAuth2Actor
261275
class OAuth2ASWebAuthenticationPresentationContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
262276

263277
private let authorizer: OAuth2Authorizer
@@ -266,6 +280,7 @@ class OAuth2ASWebAuthenticationPresentationContextProvider: NSObject, ASWebAuthe
266280
self.authorizer = authorizer
267281
}
268282

283+
@OAuth2Actor
269284
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
270285
if let context = authorizer.oauth2.authConfig.authorizeContext as? ASPresentationAnchor {
271286
return context

0 commit comments

Comments
 (0)