ASWebAuthenticationSession callbackURLScheme

Hi everyone,

I am trying to authenticate an user through ASWebAuthenticationSession, and after that redirect to an URL that uses the callback scheme.

The authentication page URL is correctly loaded on a browser thanks to ASWebAuthenticationPresentationContextProviding.

But after form completed and authentication successfully, what I am doing is a redirect directly from my server to "http://localhost:5000/ios/hola?hola=hola"

I am trying to catch this URL using a callbackScheme in my iOS app, using the same url that the one which I redirected the browser to, but this is not working.

I also tried to create a Scheme URL to my identifier, and pass it to the callbackScheme, but this is not working either. Documentation is not very clear at how to manage the authentication callback and as a beginner I don't know the way to solve this.

Some help would be appreciated. Thank you for your time!

PD: This is the code of my class
Code Block
@available(iOS 12.0, *)
class AuthView: UIViewController {
    var authSession: ASWebAuthenticationSession!
    override func viewDidLoad() {
      super.viewDidLoad()
        if #available(iOS 13.0, *) {
            configureAuthSession()
        }
    }
  @available(iOS 13.0, *)
private func configureAuthSession() {
let urlString = "http://localhost:3000/"
        guard let url = URL(string: urlString) else { return }
        let callbackScheme = "http://localhost:5000/ios/matriga/hola"
        authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackScheme)
        { (callbackURL, error) in
            guard error == nil, let successURL = callbackURL else { return }
            let code = NSURLComponents(string: (successURL.absoluteString))?.queryItems?.filter({ $0.name == "code" }).first
        }
        authSession.presentationContextProvider = self
        authSession.start()
    }
}
@available(iOS 12.0, *)
extension AuthView: ASWebAuthenticationPresentationContextProviding {
    @available(iOS 12.0, *)
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        return self.view.window ?? ASPresentationAnchor()
    }
}


  • That port number might be a problem. http://localhost:5000/ios/hola?hola=hola -> http://localhost/ios/hola?hola=hola

Add a Comment

Accepted Reply

I will be really appreciated if anyone could ask my question.

Thank you very much guys :)

Replies

I will be really appreciated if anyone could ask my question.

Thank you very much guys :)

I will be really appreciated if anyone could ask my question.

DevForums is an informal support channel. If you don’t get an answer here, I recommend that you request formal support by opening a DTS tech support incident.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
I suspect the breakdown here is the full URL being used for the callbackURLScheme.

This all depends on how your server and OAuth process set the Location header on your system to redirect the callback. Try setting up something like myapp://?token=myOAuthToken and see if that gets picked up. Note that this will also have to be setup on your server too to fulfill the Location redirect.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
From the docs:

callbackURLScheme
The custom URL scheme that the app expects in the callback URL.

Note "custom" URL scheme, which will allow ASWebAuthenticationSession to redirect back to your app and complete.

If you need to redirect back to your app with an http(s) URL from an external user agent—that is, an authentication class like ASWebAuthenticationSession or a system browser—you'll need to do it on the OS level by using Universal Links.

It's a big subject covered in the aforementioned doc and others referenced from there. In short, your redirection URI will point to a server where your apple-app-site-association file is hosted and a path specified for your app in this file. Then you'll need to handle the redirection in your app delegate's application(_:continue:restorationHandler:) method. You will also need to manually cancel your ASWebAuthenticationSession, as it will not complete on an http(s) redirection.

I described a Universal Link implementation here: h ttps://developer.forgerock.com/docs/platform/how-tos/implementing-oauth-20-authorization-code-grant-protected-pkce-appauth-sdk-ios#simple-app-universal-links
It was written a while ago and in the context of an OAuth 2 client built on top of the AppAuth SDK for iOS, but could still be a relevant compliment to the docs.

Hi,

Have you resolved this issue? url scheme is https, http, blob, ...

so that your url scheme might put it wrongly, for example you want it return with "holalo"

then you set your callBackUrlScheme = "holalo" then your server return / redirect should be holalo://ios/matriga/hola?token=1234

Just my 2 cents.

Hi there, I am trying to do the same, with the Twitter API, however their first step of the OAuth flow seems to require a POST request, and I don't believe that ASWebAuthenticationSession gives that option. Would you confirm it is not possible to authenticate with the twitter API through ASWebAuthenticationSession ?