Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Exception when using ADAL cordova on a encrypted android phone #14

Closed
fw224 opened this issue Jul 28, 2015 · 6 comments
Closed

Exception when using ADAL cordova on a encrypted android phone #14

fw224 opened this issue Jul 28, 2015 · 6 comments
Assignees
Labels

Comments

@fw224
Copy link

fw224 commented Jul 28, 2015

I am developing a cordova app that uses ADAL cordova and requires a service from a corp network (MSFT corp network). ADAL cordova works using an emulator, but on a real device (which requires encryption to access the corp network) ADAL cordova is giving me an exception. The following is the exact error:

E/PluginManager(18227): Uncaught exception from plugin
E/PluginManager(18227): java.lang.ClassCastException: com.microsoft.aad.adal.AuthenticationContext$1 cannot be cast to com.microsoft.aad.adal.DefaultTokenCacheStore
E/PluginManager(18227): at com.microsoft.aad.adal.CordovaAdalPlugin.readTokenCacheItems(CordovaAdalPlugin.java:165)
E/PluginManager(18227): at com.microsoft.aad.adal.CordovaAdalPlugin.execute(CordovaAdalPlugin.java:83)
E/PluginManager(18227): at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:84)
E/PluginManager(18227): at org.apache.cordova.PluginManager.exec(PluginManager.java:147)
E/PluginManager(18227): at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
E/PluginManager(18227): at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:40)
E/PluginManager(18227): at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
E/PluginManager(18227): at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:53)
E/PluginManager(18227): at android.os.Handler.dispatchMessage(Handler.java:102)
E/PluginManager(18227): at android.os.Looper.loop(Looper.java:145)
E/PluginManager(18227): at android.os.HandlerThread.run(HandlerThread.java:61)

And the following is my code:
$scope.context = new Microsoft.ADAL.AuthenticationContext(authority);

$scope.context.tokenCache.readItems().then(function (items) {
  if (items.length > 0) {
    authority = items[0].authority;
    $scope.context = new Microsoft.ADAL.AuthenticationContext(authority);
  }

  // Attempt to authorize user silently
  $scope.context.acquireTokenSilentAsync(resourceUrl, appId)
  .then(onAADSuccess, function () {
  // We require user cridentials so triggers authentication dialog
  $scope.context.acquireTokenAsync(resourceUrl, appId, redirectUrl)
 .then(onAADSuccess, function (err) {
    });
  });
});

Any help would be appreciated!

@RandalliLama
Copy link

@fw224 Was the Company Portal app installed on the device where this repros?

@BartNetJS
Copy link

Same issue here. On iOS it is working, on android the type cast exception occurs
The company portal app is indeed installed on the device

@yucaowang
Copy link

+1
Any update on this bug? After uninstalled Company Portal it won't repro. Anyone knows the root cause?

@RandalliLama
Copy link

FYI @weijjia

I think issue here is that if the Company Portal is installed then the Android version of ADAL uses that to get tokens. The cordova version of ADAL depends on the Android version of ADAL. When Android ADAL talks the Company Portal a different object type that the Cordova version of ADAL isn't prepared to deal with causes a crash.

We've seen this issue in at least one other context as well and are working on a fix, but it may require an update to both Cordova ADAL and the Company Portal.

@weijjia
Copy link

weijjia commented Nov 4, 2015

@RandalliLama Sorry I just saw the issue. For the crash stack trace, I don't think it even starts to talk to company portal. It looks like the crash happens inside the readTokenCacheItems, which contains the authContext.getCache. In android version of ADAL, the getCache method will check if we can switch to broker, it seems in this particular case, the existence of Company Portal makes adal-android switch to broker, and we're retuning a new anonymous inner class implementing the interface ITokenCacheStore. When authContext.getCache returns, the cordova plugin is trying to cast it to the DefaultTokenCacheStore...oops, exception is thrown here. The returned anonymous inner class implementing the interface ITokenCacheStore cannot be casted to DefaultTokenCacheStore.

@danyoel
Copy link
Contributor

danyoel commented Nov 19, 2015

Also seeing this issue on a WP8.1 device, but not the emulator. Packaging is Windows Universal app.

Exception was thrown at line 208, column 17 in ms-appx://com.microsoft.research.selfsourcing.mobile/www/plugins/cordova-plugin-ms-adal/src/windows/ADALProxy.js
0x80004002 - JavaScript runtime error: No such interface supported
System.InvalidCastException: Specified cast is not valid.
   at System.StubHelpers.InterfaceMarshaler.ConvertToNative(Object objSrc, IntPtr itfMT, IntPtr classMT, Int32 flags)
WinRT information: System.InvalidCastException: Specified cast is not valid.
   at System.StubHelpers.InterfaceMarshaler.ConvertToNative(Object objSrc, IntPtr itfMT, IntPtr classMT, Int32 flags)

The relevant line, as @RandalliLama and @weijjia suggest, is reading the token cache:

win(context.tokenCache.readItems().map(function(item) {

vladimir-kotikov pushed a commit that referenced this issue Dec 11, 2015
Fixes #14. Perform tokenCache operations with caution.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants