AzureAD / microsoft-authentication-library-common-for-android

Common code used by both the Active Directory Authentication Library (ADAL) and the Microsoft Authentication Library (MSAL)
MIT License
41 stars 35 forks source link

Token acquisition in Doze Maintenance Window fails with DEVICE_NETWORK_NOT_AVAILABLE #2488

Open tobias-morschheuser opened 2 months ago

tobias-morschheuser commented 2 months ago

It looks like the throwIfNetworkNotAvailable() method is not reliable in Doze mode. We have a background worker that only runs when network is avalible. that checks if the access token has expired. When the token has expired it tries to refresh token with method acquireTokenSilentAsync(acquireTokenSilentParameters). It works fine as long the user is active in the app but fails when the app is in Doze Mode with following issue.

SilentAuthenticationCallback -> onError
com.microsoft.identity.client.exception.MsalClientException: Connection is not available to refresh token
    at com.microsoft.identity.client.internal.controllers.MsalExceptionAdapter.msalExceptionFromBaseException(MsalExceptionAdapter.java:53)
    at com.microsoft.identity.client.SingleAccountPublicClientApplication$3.onError(SingleAccountPublicClientApplication.java:446)
    at com.microsoft.identity.client.SingleAccountPublicClientApplication$3.onError(SingleAccountPublicClientApplication.java:432)
    at com.microsoft.identity.common.java.controllers.CommandDispatcher.commandCallbackOnError(CommandDispatcher.java:639)
    at com.microsoft.identity.common.java.controllers.CommandDispatcher.access$900(CommandDispatcher.java:98)
    at com.microsoft.identity.common.java.controllers.CommandDispatcher$4.run(CommandDispatcher.java:619)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:230)
    at android.os.Looper.loop(Looper.java:319)
    at android.app.ActivityThread.main(ActivityThread.java:8919)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
Caused by: com.microsoft.identity.common.java.exception.ClientException: Connection is not available to refresh token
    at com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter.getBaseExceptionFromExceptionType(MsalBrokerResultAdapter.java:363)
    at com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter.getBaseExceptionFromBundle(MsalBrokerResultAdapter.java:251)
    at com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter.getAcquireTokenResultFromResultBundle(MsalBrokerResultAdapter.java:710)
    at com.microsoft.identity.common.internal.controllers.BrokerMsalController$5.extractResultBundle(BrokerMsalController.java:648)
    at com.microsoft.identity.common.internal.controllers.BrokerMsalController$5.extractResultBundle(BrokerMsalController.java:619)
    at com.microsoft.identity.common.internal.controllers.BrokerOperationExecutor.performStrategy(BrokerOperationExecutor.java:241)
    at com.microsoft.identity.common.internal.controllers.BrokerOperationExecutor.execute(BrokerOperationExecutor.java:150)
    at com.microsoft.identity.common.internal.controllers.BrokerMsalController.acquireTokenSilent(BrokerMsalController.java:618)
    at com.microsoft.identity.common.java.commands.SilentTokenCommand.execute(SilentTokenCommand.java:88)
    at com.microsoft.identity.common.java.commands.SilentTokenCommand.execute(SilentTokenCommand.java:48)
    at com.microsoft.identity.common.java.controllers.CommandDispatcher.executeCommand(CommandDispatcher.java:539)
    at com.microsoft.identity.common.java.controllers.CommandDispatcher.access$100(CommandDispatcher.java:98)
    at com.microsoft.identity.common.java.controllers.CommandDispatcher$1.run(CommandDispatcher.java:364)
    at io.opentelemetry.context.Context.lambda$wrap$1(Context.java:212)
    at io.opentelemetry.context.Context$$ExternalSyntheticLambda4.run(Unknown Source:4)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
    at java.lang.Thread.run(Thread.java:1012)

what we need is a setting to disable that check by default

this change is done to CommonFlight where @rpdome mentions this issue: #2480