Search code examples
iosswiftazureazure-ad-msal

MSAL for iOS is unable to authenticate via the broker app (Authenticator)


I have followed the Azure MSAL iOS integration Tutorial and was able to authenticate myself via WebView (flow without the broker). When I try to run it via the broker, the broker is properly activated and then returns to the test app, but the result is the following error:

Could not acquire token: Error Domain=MSALErrorDomain Code=-50000 "(null)" UserInfo={MSALErrorDescriptionKey=application did not receive response from broker., MSALInternalErrorCodeKey=-42700, MSALCorrelationIDKey=9A9A8D0C-BD53-4F72-8E94-D830E55012C5}

The relevant code is here:

applicationContext.acquireToken(with: parameters) { (result, error) in
    if let error = error { // the error is printed above
        self.updateLogging(text: "Could not acquire token: \(error)")
        return
}

enter image description here

Environment:

  • My iOS version is 16.0
  • MSAL version is 1.2.3
  • Xcode: 14.0
  • Authenticator (broker): 6.5.98

Solution

  • Found the answer here, my app is using SceneDelegate alongside with the app delegate and I didn't include logic to notify MSAL about activation in the SceneDelegate. After adding it, the broken is able to successfully authenticate me:

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
            
            guard let urlContext = URLContexts.first else {
                return
            }
            
            let url = urlContext.url
            let sourceApp = urlContext.options.sourceApplication
            
            MSALPublicClientApplication.handleMSALResponse(url, sourceApplication: sourceApp)
        }