nayanAubie / msal_auth

A new Flutter plugin for Azure AD authentication.
MIT License
5 stars 8 forks source link

Msal exception with error: Multiple account PublicClientApplication could not be created for unknown reasons #21

Open nelzaatari opened 1 month ago

nelzaatari commented 1 month ago

on android, im getting the following error:

Msal exception with error: Multiple account PublicClientApplication could not be created for unknown reasons

msal_config.json

{
  "client_id" : "a12ff56b-124b-411e-ab57-xxxxxxxxxxxx",
  "authorization_user_agent" : "DEFAULT",
  "broker_redirect_uri_registered": true,
  "account_mode": "SINGLE",
  "redirect_uri" : "msauth://com.example.ws/hash",
  "authorities" : [
    {
      "type": "AAD",
      "audience": {
        "type": "AzureADMyOrg",
        "tenant_id": "e7c58320-6dbf-4137-b708-xxxxxxxxxxxx"
      }
    }
  ]
}

AndroidManifest.xml

<activity android:name="com.microsoft.identity.client.BrowserTabActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="com.example.ws"
            android:path="/hash"
            android:scheme="msauth" />
    </intent-filter>
</activity>

acquiring a token:

  final _clientId = 'a12ff56b-124b-411e-ab57-xxxxxxxxxxxx';
  final _tenantId = 'e7c58320-6dbf-4137-b708-xxxxxxxxxxxx';
  late final _authority =
      'https://login.microsoftonline.com/$_tenantId';
  final _scopes = <String>[
    'https://graph.microsoft.com/user.read',
    // Add other scopes here if required.
  ];

  Future<MsalAuth> getMsalAuth() async {
    return MsalAuth.createPublicClientApplication(
      clientId: _clientId,
      scopes: _scopes,
      androidConfig: AndroidConfig(
        configFilePath: 'assets/msal_config.json',
        tenantId: _tenantId,
      ),
      iosConfig: IosConfig(authority: _authority),
    );
  }

    Future<void> getToken() async {
    try {
      final msalAuth = await getMsalAuth();
      final user = await msalAuth.acquireToken();
      print('User data: ${user?.toJson()}');
    } on MsalException catch (e) {
      print('Msal exception with error: ${e.errorMessage}');
    } catch (e) {
      print(e.toString());
    }
  }
nelzaatari commented 1 month ago

anyone faced the same problem ?

nelzaatari commented 1 month ago

i found out that this library doesnt work for "SINGLE" accounts_mode, this code is executed either your specify single or multiple, i would be awesome if we can implement for single account

        PublicClientApplication.createMultipleAccountPublicClientApplication(
            msal.applicationContext,
            File(configFilePath!!),
            msal.getApplicationCreatedListener(result)
        )
nayanAubie commented 2 weeks ago

Thanks for the catch @nelzaatari I'll look into it.

csehlke commented 2 weeks ago

+1 also ran into this issue today Thank you @nayanAubie for taking a look at it