@@ -42,7 +42,10 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
4242
4343 /// Used to store the authentication session.
4444 private var authenticationSession : AnyObject ?
45-
45+
46+ /// Used to store the ASWebAuthenticationPresentationContextProvider
47+ private var webAuthenticationPresentationContextProvider : AnyObject ?
48+
4649 public init ( oauth2: OAuth2Base ) {
4750 self . oauth2 = oauth2
4851 }
@@ -142,9 +145,10 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
142145 self . oauth2. logger? . warn ( " OAuth2 " , msg: " Cannot intercept redirect URL: \( err) " )
143146 }
144147 } else {
145- self . oauth2. didFail ( with: nil )
148+ self . oauth2. didFail ( with: error ? . asOAuth2Error )
146149 }
147150 self . authenticationSession = nil
151+ self . webAuthenticationPresentationContextProvider = nil
148152 }
149153
150154#if targetEnvironment(macCatalyst)
@@ -153,6 +157,10 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
153157#else
154158 if #available( iOS 12 , * ) {
155159 authenticationSession = ASWebAuthenticationSession ( url: url, callbackURLScheme: redirect, completionHandler: completionHandler)
160+ if #available( iOS 13 . 0 , * ) {
161+ webAuthenticationPresentationContextProvider = OAuth2ASWebAuthenticationPresentationContextProvider ( authorizer: self )
162+ ( authenticationSession as! ASWebAuthenticationSession ) . presentationContextProvider = webAuthenticationPresentationContextProvider as! OAuth2ASWebAuthenticationPresentationContextProvider
163+ }
156164 return ( authenticationSession as! ASWebAuthenticationSession ) . start ( )
157165 } else {
158166 authenticationSession = SFAuthenticationSession ( url: url, callbackURLScheme: redirect, completionHandler: completionHandler)
@@ -291,4 +299,22 @@ class OAuth2SFViewControllerDelegate: NSObject, SFSafariViewControllerDelegate {
291299 }
292300}
293301
302+ @available ( iOS 13 . 0 , * )
303+ class OAuth2ASWebAuthenticationPresentationContextProvider : NSObject , ASWebAuthenticationPresentationContextProviding {
304+
305+ private let authorizer : OAuth2Authorizer
306+
307+ init ( authorizer: OAuth2Authorizer ) {
308+ self . authorizer = authorizer
309+ }
310+
311+ public func presentationAnchor( for session: ASWebAuthenticationSession ) -> ASPresentationAnchor {
312+ guard let context = authorizer. oauth2. authConfig. authorizeContext as? ASPresentationAnchor else {
313+ fatalError ( " Invalid authorizeContext -- must be ASPresentationAnchor (AKA, UIWindow) " )
314+ }
315+
316+ return context
317+ }
318+ }
319+
294320#endif
0 commit comments