Skip to content

Commit 4496b5b

Browse files
authored
Fix implementation of ASWebAuthenticationPresentationContextProviding protocol (p2#357)
This protocol expects an ASPresentationAnchor to be returned (which is a UIWindow on iOS, or NSWindow on Mac OS). This commit updates the implementation to return the window object associated with the view of the `authorizeContext` (and assumes the authorize context is an instance of UIViewController). This change is in line with Apple’s example documentation: https://developer.apple.com/documentation/authenticationservices/authenticating_a_user_through_a_web_service
1 parent ff48da3 commit 4496b5b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Sources/iOS/OAuth2Authorizer+iOS.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,17 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
150150
self.oauth2.logger?.warn("OAuth2", msg: "Cannot intercept redirect URL: \(err)")
151151
}
152152
} else {
153-
self.oauth2.didFail(with: error?.asOAuth2Error)
153+
if let authenticationSessionError = error as? ASWebAuthenticationSessionError {
154+
switch authenticationSessionError.code {
155+
case .canceledLogin:
156+
self.oauth2.didFail(with: .requestCancelled)
157+
default:
158+
self.oauth2.didFail(with: error?.asOAuth2Error)
159+
}
160+
}
161+
else {
162+
self.oauth2.didFail(with: error?.asOAuth2Error)
163+
}
154164
}
155165
self.authenticationSession = nil
156166
self.webAuthenticationPresentationContextProvider = nil
@@ -314,11 +324,11 @@ class OAuth2ASWebAuthenticationPresentationContextProvider: NSObject, ASWebAuthe
314324
}
315325

316326
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
317-
guard let context = authorizer.oauth2.authConfig.authorizeContext as? ASPresentationAnchor else {
318-
fatalError("Invalid authConfig.authorizeContext, must be ASPresentationAnchor but is \(String(describing: authorizer.oauth2.authConfig.authorizeContext))")
327+
guard let context = authorizer.oauth2.authConfig.authorizeContext as? UIViewController else {
328+
fatalError("Invalid authConfig.authorizeContext, must be a UIViewController but is \(String(describing: authorizer.oauth2.authConfig.authorizeContext))")
319329
}
320330

321-
return context
331+
return context.view.window!
322332
}
323333
}
324334

0 commit comments

Comments
 (0)