Skip to content

Commit c171ab6

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

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

Sources/iOS/OAuth2Authorizer+iOS.swift

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
6464
public func openAuthorizeURLInBrowser(_ url: URL) throws {
6565

6666
#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")
67+
Task {
68+
guard await UIApplication.shared.canOpenURL(url) else {
69+
throw OAuth2Error.unableToOpenAuthorizeURL
70+
}
71+
await UIApplication.shared.open(url) { didOpen in
72+
if !didOpen {
73+
Task { @OAuth2Actor in
74+
self.oauth2.logger?.warn("OAuth2", msg: "Unable to open authorize URL")
75+
}
76+
}
7377
}
7478
}
7579
#else
@@ -84,7 +88,7 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
8488
- parameter with: The configuration to be used; usually uses the instance's `authConfig`
8589
- parameter at: The authorize URL to open
8690
*/
87-
public func authorizeEmbedded(with config: OAuth2AuthConfig, at url: URL) throws {
91+
public func authorizeEmbedded(with config: OAuth2AuthConfig, at url: URL) async throws {
8892
if config.ui.useAuthenticationSession {
8993
guard let redirect = oauth2.redirect else {
9094
throw OAuth2Error.noRedirectURL
@@ -99,11 +103,13 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
99103
throw (nil == config.authorizeContext) ? OAuth2Error.noAuthorizationContext : OAuth2Error.invalidAuthorizationContext
100104
}
101105

102-
let web = try authorizeSafariEmbedded(from: controller, at: url)
106+
let web = try await authorizeSafariEmbedded(from: controller, at: url)
103107
if config.authorizeEmbeddedAutoDismiss {
104108
oauth2.internalAfterAuthorizeOrFail = { wasFailure, error in
105109
self.safariViewDelegate = nil
106-
web.dismiss(animated: true)
110+
Task {
111+
await web.dismiss(animated: true)
112+
}
107113
}
108114
}
109115
#endif
@@ -199,23 +205,35 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
199205
- returns: SFSafariViewController, being already presented automatically
200206
*/
201207
@discardableResult
202-
public func authorizeSafariEmbedded(from controller: UIViewController, at url: URL) throws -> SFSafariViewController {
203-
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
208+
public func authorizeSafariEmbedded(from controller: UIViewController, at url: URL) async throws -> SFSafariViewController {
209+
return await Task {
210+
safariViewDelegate = await OAuth2SFViewControllerDelegate(authorizer: self)
211+
let web = await SFSafariViewController(url: url)
212+
Task { @MainActor in
213+
web.title = await oauth2.authConfig.ui.title
214+
web.delegate = await safariViewDelegate
215+
}
216+
if let barTint = oauth2.authConfig.ui.barTintColor {
217+
Task { @MainActor in
218+
web.preferredBarTintColor = barTint
219+
}
220+
}
221+
if let tint = oauth2.authConfig.ui.controlTintColor {
222+
Task { @MainActor in
223+
web.preferredControlTintColor = tint
224+
}
225+
}
226+
Task { @MainActor in
227+
web.modalPresentationStyle = await oauth2.authConfig.ui.modalPresentationStyle
228+
}
229+
230+
willPresent(viewController: web, in: nil)
231+
await controller.present(web, animated: true, completion: nil)
232+
Task { @MainActor in
233+
web.presentationController?.delegate = await safariViewDelegate
234+
}
235+
return web
236+
}.value
219237
}
220238

221239

@@ -245,19 +263,24 @@ class OAuth2SFViewControllerDelegate: NSObject, SFSafariViewControllerDelegate,
245263
}
246264

247265
@available(iOS 9.0, *)
248-
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
249-
authorizer?.safariViewControllerDidCancel(controller)
266+
nonisolated func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
267+
Task {
268+
await authorizer?.safariViewControllerDidCancel(controller)
269+
}
250270
}
251271

252272
// called in case ViewController is dismissed via pulling down the presented sheet.
253273
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
254274
guard let safariViewController = presentationController.presentedViewController as? SFSafariViewController else { return }
255-
authorizer?.safariViewControllerDidCancel(safariViewController)
275+
Task {
276+
await authorizer?.safariViewControllerDidCancel(safariViewController)
277+
}
256278
}
257279
}
258280
#endif
259281

260282
@available(iOS 13.0, *)
283+
@OAuth2Actor
261284
class OAuth2ASWebAuthenticationPresentationContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
262285

263286
private let authorizer: OAuth2Authorizer
@@ -266,6 +289,7 @@ class OAuth2ASWebAuthenticationPresentationContextProvider: NSObject, ASWebAuthe
266289
self.authorizer = authorizer
267290
}
268291

292+
@OAuth2Actor
269293
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
270294
if let context = authorizer.oauth2.authConfig.authorizeContext as? ASPresentationAnchor {
271295
return context

0 commit comments

Comments
 (0)