Skip to content

Commit 5562459

Browse files
committed
Merge branch 'master' of github.com:p2/OAuth2
2 parents 93a2228 + aebb11e commit 5562459

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Contributors
33

44
Contributors to the codebase, in reverse chronological order:
55

6+
- James Addyman, @james-rantmedia
67
- Foti Dim, @fotidim
78
- Denis, @telipskiy
89
- Larry Brunet, @larrybrunet

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ func application(_ app: UIApplication,
123123
}
124124
```
125125

126+
For iOS 13 make the callback in `SceneDelegate.swift`
127+
128+
```swift
129+
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
130+
if let url = URLContexts.first?.url {
131+
AppDelegate.shared.oauth2?.handleRedirectURL(url)
132+
}
133+
}
134+
```
135+
126136
You’re all set!
127137

128138
---

Sources/iOS/OAuth2Authorizer+iOS.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)