Skip to content

Commit c48aba2

Browse files
committed
Fix to ASWebAuthenticationSession's redirect scheme
Fixes p2#345
1 parent 5562459 commit c48aba2

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Sources/iOS/OAuth2Authorizer+iOS.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,17 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
130130

131131
This method dismisses the view controller automatically - this cannot be disabled.
132132

133-
- parameter at: The authorize URL to open
134-
- returns: A Boolean value indicating whether the web authentication session starts successfully.
133+
- parameter at: The authorize URL to open
134+
- parameter redirect: The full redirect URL to use
135+
- returns: A Boolean value indicating whether the web authentication session starts successfully.
135136
*/
136137
@available(iOS 11.0, *)
137138
@discardableResult
138139
public func authenticationSessionEmbedded(at url: URL, withRedirect redirect: String) -> Bool {
140+
guard let redirectURL = URL(string: redirect) else {
141+
oauth2.logger?.warn("OAuth2", msg: "Unable to parse redirect URL ”(redirect)“")
142+
return false
143+
}
139144
let completionHandler: (URL?, Error?) -> Void = { url, error in
140145
if let url = url {
141146
do {
@@ -152,11 +157,11 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
152157
}
153158

154159
#if targetEnvironment(macCatalyst)
155-
authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: redirect, completionHandler: completionHandler)
160+
authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: redirectURL.scheme, completionHandler: completionHandler)
156161
return (authenticationSession as! ASWebAuthenticationSession).start()
157162
#else
158163
if #available(iOS 12, *) {
159-
authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: redirect, completionHandler: completionHandler)
164+
authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: redirectURL.scheme, completionHandler: completionHandler)
160165
if #available(iOS 13.0, *) {
161166
webAuthenticationPresentationContextProvider = OAuth2ASWebAuthenticationPresentationContextProvider(authorizer: self)
162167
(authenticationSession as! ASWebAuthenticationSession).presentationContextProvider = webAuthenticationPresentationContextProvider as! OAuth2ASWebAuthenticationPresentationContextProvider
@@ -310,7 +315,7 @@ class OAuth2ASWebAuthenticationPresentationContextProvider: NSObject, ASWebAuthe
310315

311316
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
312317
guard let context = authorizer.oauth2.authConfig.authorizeContext as? ASPresentationAnchor else {
313-
fatalError("Invalid authorizeContext -- must be ASPresentationAnchor (AKA, UIWindow)")
318+
fatalError("Invalid authConfig.authorizeContext, must be ASPresentationAnchor but is \(authorizer.oauth2.authConfig.authorizeContext)")
314319
}
315320

316321
return context

0 commit comments

Comments
 (0)