Trying to create an open-source Microsoft SSO (MSA...
# multiplatform
s
Trying to create an open-source Microsoft SSO (MSAL/Entra ID) library. I want to use the 1st party SDKs for each platform so that I don’t have to roll my own (complexity, security, etc.). I am having significant trouble with their iOS SDK integration. I would greatly appreciate help. Details in thread.
Common code:
Copy code
expect class MSALAuthManager {
    suspend fun getAuthToken(request: MSALRequest): MSALResponse
}
data class MSALRequest(
    val scopes: List<String>,
    val clientId: String,
    val context: Any? = null, //UIViewController, android.Context, etc.
    val redirectUri: String? = null,
)
data class MSALResponse(
    val accessToken: String? = null,
    val accountIdentifier: String? = null,
    val error: String? = null,
)
main.ios.kt:
Copy code
internal var viewController: UIViewController? = null
fun MainViewController(): UIViewController {
    viewController = ComposeUIViewController {
        App { msalRequest ->
          MSALAuthManager().getAuthToken(msalRequest.copy(context = viewController))
        }
    }
    return viewController!!
}
Swift code:
Copy code
class Coordinator {
        var accessToken: String?

        func getAuth(for viewController: UIViewController, completion: @escaping () -> Void) {
            let config = MSALPublicClientApplicationConfig(clientId: "<your-client-id-here>")
            let scopes = ["User.Read"]

            do {
                let application = try MSALPublicClientApplication(configuration: config)
                let webviewParameters = MSALWebviewParameters(authPresentationViewController: viewController)
                let interactiveParameters = MSALInteractiveTokenParameters(scopes: scopes, webviewParameters: webviewParameters)

                application.acquireToken(with: interactiveParameters) { [weak self] (result, error) in
                    guard let authResult = result else {
                        print("Could not acquire token: \(error?.localizedDescription ?? "No error information available")")
                        completion()
                        return
                    }
                    print("Auth result: \(authResult)")
                    self?.accessToken = authResult.accessToken
                    completion()
                }
            } catch {
                print("Unable to create application: \(error.localizedDescription)")
            }
        }
    }
@objc
class AuthBridge: NSObject {
    @objc static func getAuthToken(with viewController: UIViewController, completion: @escaping (String?) -> Void) {
         let coordinator = Coordinator()
         coordinator.getAuth(for: viewController) {
             completion(coordinator.accessToken)
         }
    }
}
The code currently builds, the problem is that neither
AuthBridge
nor
Coordinator
are not appearing the Kotlin side (Android Studio autocomplete). In the
build.gradle.kts
file. Created this .def file:
Copy code
language = Objective-C
headers = "MSALAuth.h"
compilerOpts = "-framework Foundation"
but it didn’t help.