1

I am using AppAuth on my code.

I manage to authenticate successful , but when the SFSafariViewController gets dismiss from my Controller , the redirect url does not trigger the AppDelegate func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

The redirect URL is my Bundle Identifier name : BundleIdentifier://authenticate

I have setup in info.plist url Schemes and url identifier which they have the same name.

When I run my code setting a break point on this func I can see my redirect url correct for standarizedURL and standarizedRedirectURL

- (BOOL)shouldHandleURL:(NSURL *)URL {
  NSURL *standardizedURL = [URL standardizedURL];
  NSURL *standardizedRedirectURL = [_request.redirectURL standardizedURL];

    return OIDIsEqualIncludingNil(standardizedURL.scheme, standardizedRedirectURL.scheme) &&
    OIDIsEqualIncludingNil(standardizedURL.user, standardizedRedirectURL.user) &&
    OIDIsEqualIncludingNil(standardizedURL.password, standardizedRedirectURL.password) &&
    OIDIsEqualIncludingNil(standardizedURL.host, standardizedRedirectURL.host) &&
    OIDIsEqualIncludingNil(standardizedURL.port, standardizedRedirectURL.port) &&
    OIDIsEqualIncludingNil(standardizedURL.path, standardizedRedirectURL.path);

But when AppAuth finishes the authentication and I have an access token , func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool doesn't get triggered.

Any idea why?

Here is my code

class func signInAuth(discoveryURLstr: String,presenter : UIViewController,completionHandler: @escaping ( (OIDAuthState?,Error?) -> () )){

        guard let discoveruURL = URL(string: discoveryURLstr) else{
            completionHandler(nil,AuthErrors.InvalidDiscoveryURL)
            return
        }

        appAuthDiscoverConfiguration(discoveryURL: discoveruURL) { (configurationFile, error) in

            guard let configurationFile = configurationFile else {
                completionHandler(nil,AuthErrors.InvalidConfigurationFile)
                return
            }

            let authRequest = appAuthRequest(configurationFile: configurationFile)

             self.appAuthenticationSession = OIDAuthState.authState(byPresenting: authRequest, presenting: presenter, callback: { (state, error) in

                if let error = error {
                    //self.authState = nil
                    completionHandler(nil,error)
                    return
                }

                if let state = state {
                    self.authState = state
                    completionHandler(state,nil)

                }else{
                    completionHandler(nil,AuthErrors.InvalideState)
                }
            })

        }


    }

    class func appAuthDiscoverConfiguration(discoveryURL : URL, completionHandler: @escaping ((OIDServiceConfiguration?,Error?) -> ())) {

        OIDAuthorizationService.discoverConfiguration(forDiscoveryURL: discoveryURL) { (configuration, error) in

            if let error = error {
                completionHandler(nil,error)
                return
            }else{
                guard let configurationFile = configuration else {
                    completionHandler(nil,AuthErrors.InvalidConfigurationFile)
                    return
                }
                completionHandler(configurationFile,nil)
            }

        }

    }

    class func appAuthRequest(configurationFile : OIDServiceConfiguration) -> OIDAuthorizationRequest{

        return OIDAuthorizationRequest(configuration: configurationFile, clientId: AppAuthConstants.clientId, clientSecret: nil, scope: AppAuthConstants.scope, redirectURL: AppAuthConstants.redirectURL, responseType: AppAuthConstants.responseType, state: nil, nonce: nil, codeVerifier: nil, codeChallenge: nil, codeChallengeMethod: nil, additionalParameters: AppAuthConstants.additionalParameters)
    }
2
  • Is your server call this url: The redirect URL is my Bundle Identifier name : BundleIdentifier://authenticate after authentication? Commented May 28, 2019 at 21:00
  • Server doesn’t call this url. This is the redirect url. I set redirect url on my request as you can see . And redirect url is bundleidentifier://authenticate Commented May 28, 2019 at 21:03

1 Answer 1

1

On iOS 12, App-Auth uses ASWebAuthenticationSession, and on iOS 11, it uses the now-deprecated SFAuthenticationSession instead of requiring the app to support handling the redirect manually. To support earlier versions of iOS, you still need your code in the func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool method.

For reference, you can see what AppAuth is doing under the covers here. Also, this is a great answer that explains how to generically get an OAuth token on iOS without using AppAuth.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.