recognizegroup / capacitor-plugin-msauth

Capacitor-wrapper for MSAL libraries. Works on iOS, Android and web.
Other
17 stars 17 forks source link

Error running on android #8

Closed JonathanJPereira closed 1 year ago

JonathanJPereira commented 1 year ago

Hello, thanks for developing this plugin!

I'm trying to make the plugin work on android but I'm getting an error and I can't proceed

2022-10-25 10:50:46.550 5426-5545/com.carewebsolutions.celatools W/LibraryConfiguration:  [2022-10-25 13:50:46 - thread_name : CapacitorPlugins, correlation_id : 59e68136-7380-4cac-a855-c7dd25f21155] MsalConfiguration was already initialized Android 31
2022-10-25 10:50:46.567 5426-5470/com.carewebsolutions.celatools D/EGL_emulation: app_time_stats: avg=1725.90ms min=4.75ms max=32634.13ms count=19
2022-10-25 10:50:46.579 5426-5545/com.carewebsolutions.celatools W/LocalMSALController:getDeviceMode:  [2022-10-25 13:50:46 - thread_name : pool-6-thread-5, correlation_id : 6dff6c74-42f2-4b29-a8dc-59ea9b0dafd6] LocalMSALController is not eligible to use the broker. Do not check sharedDevice mode and return false immediately. Android 31
2022-10-25 10:50:46.609 5426-5545/com.carewebsolutions.celatools W/Telemetry:  [2022-10-25 13:50:46 - thread_name : main, correlation_id : UNSET] Telemetry is disabled because the Telemetry context or configuration is null Android 31
2022-10-25 10:50:46.721 5426-5603/com.carewebsolutions.celatools E/Capacitor: Unable to login
    java.lang.IllegalArgumentException: Empty scopes list.
        at com.microsoft.identity.client.TokenParameters$Builder.withScopes(TokenParameters.java:180)
        at nl.recognize.msauthplugin.MsAuthPlugin.acquireTokenInteractively(MsAuthPlugin.java:124)
        at nl.recognize.msauthplugin.MsAuthPlugin.acquireToken(MsAuthPlugin.java:164)
        at nl.recognize.msauthplugin.MsAuthPlugin.login(MsAuthPlugin.java:55)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121)
        at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:598)
        at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.os.HandlerThread.run(HandlerThread.java:67)
2022-10-25 10:50:46.722 5426-5603/com.carewebsolutions.celatools D/Capacitor: Sending plugin error: {"save":false,"callbackId":"124924198","pluginId":"MsAuthPlugin","methodName":"login","success":false,"error":{"message":"Unable to fetch access token."}}
2022-10-25 10:50:46.813 5426-5426/com.carewebsolutions.celatools E/Capacitor/Console: File: http://localhost/vendor.js - Line 81599 - Msg: ERROR Error: Uncaught (in promise): Error: Unable to fetch access token.
    Error: Unable to fetch access token.
        at Object.cap.fromNative (http://localhost/:413:32)
        at <anonymous>:1:18
bartwesselink commented 1 year ago

Hi @JonathanJPereira!

Great to see that you are using the plugin. Could you try adding a scope like this scopes: ['api://<client id>/user.read'], where <client id> represents the application ID from your app registration? If this works, we will update the documentation accordingly.

Please let me know!

JonathanJPereira commented 1 year ago

Thank you so much for that, it worked! @bartwesselink

But now when I press the button, only my console.log appears, I'm going to look for something I did wrong.

JonathanJPereira commented 1 year ago

My return:

2022-10-25 12:12:49.038 13000-13000/com.carewebsolutions.celatools I/Capacitor/Console: File: http://localhost/src_app_pages_acesso_login_login_module_ts.js - Line 148 - Msg: call MSAuth
2022-10-25 12:12:49.080 13000-13171/com.carewebsolutions.celatools W/LibraryConfiguration:  [2022-10-25 15:12:49 - thread_name : CapacitorPlugins, correlation_id : 2df1adb8-dfe8-4242-84f7-ea02920938f7] MsalConfiguration was already initialized Android 31
2022-10-25 12:12:49.100 13000-13171/com.carewebsolutions.celatools W/LocalMSALController:getDeviceMode:  [2022-10-25 15:12:49 - thread_name : pool-6-thread-4, correlation_id : b031c4b1-5a56-405f-beb2-087588897393] LocalMSALController is not eligible to use the broker. Do not check sharedDevice mode and return false immediately. Android 31
2022-10-25 12:12:49.134 13000-13171/com.carewebsolutions.celatools W/Telemetry:  [2022-10-25 15:12:49 - thread_name : main, correlation_id : UNSET] Telemetry is disabled because the Telemetry context or configuration is null Android 31

I'm researching what it's about

JonathanJPereira commented 1 year ago

The first time it calls, just the console.log, even

bartwesselink commented 1 year ago

Could you show me what your call looks like, from the browser/JavaScript side?

JonathanJPereira commented 1 year ago

Could you show me what your call looks like, from the browser/JavaScript side?

It is like this

 async onLogin(value) {
    console.log('call MSAuth');
    const result = await MsAuthPlugin.login({
      clientId: '********-86ed-4a80-90a2-9cf2f39c08ea',
      keyHash: ********rIeQyoc3+mckmcEUp4=',
      tenant: '********-8f55-4f53-b2ab-3d5018045ba0',
      scopes: ['api://********-86ed-4a80-90a2-9cf2f39c08eas/user.read']
    });
    console.log('end MSAuth');
    this.accessApp(result);
  }

  accessApp(result) {
    console.log('here is acessApp');
    if (result && result.accessToken) {
      const accessToken = result.accessToken;
      if (accessToken) {
        this.loginStorage.toggleSaveAccount(JSON.stringify(accessToken));
        this.router.navigateByUrl('home/home');
      }
    }
  }
JonathanJPereira commented 1 year ago

@bartwesselink the import is like this

import { MsAuthPlugin } from '@recognizebv/capacitor-plugin-msauth';
bartwesselink commented 1 year ago

@JonathanJPereira thanks for sending the call. What is the result, does something happen? Do you see the the auth screen, are you able to login? Do you receive a token?

JonathanJPereira commented 1 year ago

@bartwesselink when I press the button it just runs the console.log "call MSAuth" and it looks like it's stuck on 'await', it doesn't open any screens and nothing runs afterwards. I can't get the token.

I have no way to test it, but another dev said it's working on IOS.

JonathanJPereira commented 1 year ago

@bartwesselink in version 3.0.0 is giving this error. In other versions, no. It seems that some package is missing, but I don't know what to do.

Screenshot_73

Task :recognizebv-capacitor-plugin-msauth:compileDebugJavaWithJavac FAILED
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\DefaultPublicClientApplicationFactory.java:3: error: package android.content does not exist
import android.content.Context;
                      ^
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\PublicClientApplicationFactory.java:3: error: package android.content does not exist
import android.content.Context;
                      ^
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\DefaultPublicClientApplicationFactory.java:15: error: cannot find symbol
        @NonNull Context context,
                 ^
  symbol:   class Context
  location: class DefaultPublicClientApplicationFactory
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\PublicClientApplicationFactory.java:12: error: cannot find symbol
        @NonNull final Context context,
                       ^
  symbol:   class Context
  location: interface PublicClientApplicationFactory
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:3: error: package android does not exist
import android.Manifest;
              ^
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:26: error: package org.json does not exist
import org.json.JSONArray;
               ^
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:27: error: package org.json does not exist
import org.json.JSONException;
               ^
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:28: error: package org.json does not exist
import org.json.JSONObject;
               ^
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:178: error: cannot find symbol
        throws MsalException, InterruptedException, IOException, JSONException {
                                                                 ^
  symbol:   class JSONException
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:209: error: cannot find symbol
    ) throws MsalException, InterruptedException, IOException, JSONException {
                                                               ^
  symbol:   class JSONException
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:251: error: cannot find symbol
    private File writeJSONObjectConfig(JSONObject data) throws IOException {
                                       ^
  symbol:   class JSONObject
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:32: error: package Manifest does not exist
    permissions = { @Permission(alias = "network", strings = { Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.INTERNET }) }
                                                                       ^
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:32: error: package Manifest does not exist
    permissions = { @Permission(alias = "network", strings = { Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.INTERNET }) }
                                                                                                                 ^
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:58: error: cannot access JSONArray
                    call.getArray("scopes").toList(),
                                           ^
  class file for org.json.JSONArray not found
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:64: error: cannot find symbol
                            JSONArray scopes = new JSONArray(Arrays.asList(tokenResult.getScopes()));
                            ^
  symbol:   class JSONArray
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:64: error: cannot find symbol
                            JSONArray scopes = new JSONArray(Arrays.asList(tokenResult.getScopes()));
                                                   ^
  symbol:   class JSONArray
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:123: error: cannot access Activity
            .startAuthorizationFromActivity(this.getActivity())
            ^
  class file for android.app.Activity not found
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:213: error: cannot find symbol
        String redirectUri = "msauth://" + getActivity().getApplicationContext().getPackageName() + "/" + urlEncodedKeyHash;
                                                        ^
  symbol:   method getApplicationContext()
  location: class AppCompatActivity
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:215: error: cannot find symbol
        JSONObject authorityConfig = new JSONObject();
        ^
  symbol:   class JSONObject
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:215: error: cannot find symbol
        JSONObject authorityConfig = new JSONObject();
                                         ^
  symbol:   class JSONObject
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:221: error: cannot find symbol
                authorityConfig.put("audience", (new JSONObject()).put("type", "AzureADMultipleOrgs").put("tenant_id", tenantId));
                                                     ^
  symbol:   class JSONObject
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:230: error: cannot find symbol
        JSONObject configFile = new JSONObject();
        ^
  symbol:   class JSONObject
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:230: error: cannot find symbol
        JSONObject configFile = new JSONObject();
                                    ^
  symbol:   class JSONObject
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:236: error: cannot find symbol
        configFile.put("authorities", (new JSONArray()).put(authorityConfig));
                                           ^
  symbol:   class JSONArray
  location: class MsAuthPlugin
C:\websolution.cella_tools-main\node_modules\@recognizebv\capacitor-plugin-msauth\android\src\main\java\nl\recognize\msauthplugin\MsAuthPlugin.java:252: error: cannot find symbol
        File config = new File(getActivity().getFilesDir() + "auth_config.json");
                                            ^
  symbol:   method getFilesDir()
  location: class AppCompatActivity
25 errors
bartwesselink commented 1 year ago

Thank you for supplying the additional logs. You can only use version 3.x with Capacitor 4, otherwise you might run into these issues. Please make sure that that is the case.

Do you see any logging in your LogCat window when you press the button? Apart from what you already sent, because those do not seem to be blocking issues.

JonathanJPereira commented 1 year ago

Thanks for your help @bartwesselink, I updated the capacitor and version 3.0.0 worked.

But it still gives the same problem as before, I press the button and no error appears in the console.log.

I made a short video demonstrating: https://www.youtube.com/watch?v=nJSQ53vcQEs&ab_channel=JonathanPereira

bartwesselink commented 1 year ago

Thanks for the video, it helps. So, there is no output at all. What you could try, is registering the plugin manually like this, in your MainActivity (please also add the imports):

@Override
public void onCreate(Bundle savedInstanceState) {
    registerPlugin(MsAuthPlugin.class);
    super.onCreate(savedInstanceState);
}

If this does not work, you will have to give us a minimal setup repository that we can use to reproduce the problem.

wangzhengjiang commented 1 year ago

@JonathanJPereira, have you fixed the problem? I have same issue like yours to use this plugin on Android.

bartwesselink commented 1 year ago

@wangzhengjiang does the solution above work for you?

wangzhengjiang commented 1 year ago

@bartwesselink , thanks for creating this Plugin. I added above code into mainactivity, but still not working.

JonathanJPereira commented 1 year ago

@JonathanJPereira, have you fixed the problem? I have same issue like yours to use this plugin on Android.

Sorry for the delay, unfortunately I couldn't solve the problem

bartwesselink commented 1 year ago

@wangzhengjiang @JonathanJPereira sorry for the delayed response. I have tried to replicate this, but had no luck. Is anybody of you able to create an empty repository that has the problem you are describing? This will help debugging.

wangzhengjiang commented 1 year ago

@bartwesselink, I tried again and redo the app setting in azure portal, basically generated the hash again, it is working now.

bartwesselink commented 1 year ago

@wangzhengjiang great to hear that. @JonathanJPereira does the same hold for you?

JonathanJPereira commented 1 year ago

@wangzhengjiangótimo ouvir isso.@JonathanJPereirao mesmo vale para você?

Unfortunately I no longer have access to the previous code