Search in sources :

Example 1 with IIpcStrategy

use of com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerOperationExecutorTests method getStrategyWithUserCanceledResult.

// Gets a bundle back, the bundle contains a user cancelled result.
private IIpcStrategy getStrategyWithUserCanceledResult() {
    return new IIpcStrategy() {

        @Override
        @NonNull
        public Bundle communicateToBroker(@NonNull final BrokerOperationBundle bundle) throws BrokerCommunicationException {
            final Bundle result = new Bundle();
            result.putBoolean(USER_CANCEL_BUNDLE_KEY, true);
            return result;
        }

        @Override
        public Type getType() {
            return MOCK_TYPE;
        }
    };
}
Also used : BrokerOperationBundle(com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle) Bundle(android.os.Bundle) BrokerOperationBundle(com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle) IIpcStrategy(com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy) NonNull(androidx.annotation.NonNull)

Example 2 with IIpcStrategy

use of com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy in project microsoft-authentication-library-common-for-android by AzureAD.

the class IpcStrategyTests method testOperationSucceeds.

protected void testOperationSucceeds(@NonNull final BrokerOperationBundle bundle, @NonNull final Bundle expectedResultBundle) {
    final IIpcStrategy strategy = getStrategy();
    try {
        final Bundle resultBundle = strategy.communicateToBroker(bundle);
        Assert.assertNotNull(resultBundle);
        Assert.assertTrue(isBundleEqual(resultBundle, expectedResultBundle));
    } catch (BaseException e) {
        Assert.fail("Exception is not expected.");
    }
}
Also used : BaseException(com.microsoft.identity.common.exception.BaseException) Bundle(android.os.Bundle) BrokerOperationBundle(com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle) BaseBundle(android.os.BaseBundle) IIpcStrategy(com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy)

Example 3 with IIpcStrategy

use of com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerOperationExecutor method execute.

/**
 * A generic method that would initialize and iterate through available strategies.
 * It will return a result immediately if any of the strategy succeeds, or throw an exception if all of the strategies fails.
 */
public <T extends CommandParameters, U> U execute(@Nullable final T parameters, @NonNull final BrokerOperation<U> operation) throws BaseException {
    final String methodName = ":execute";
    emitOperationStartEvent(parameters, operation);
    if (mStrategies.size() == 0) {
        final ClientException exception = new ClientException(ErrorStrings.BROKER_BIND_SERVICE_FAILED, "No strategies can be used to connect to the broker.");
        emitOperationFailureEvent(operation, exception);
        throw exception;
    }
    final List<BrokerCommunicationException> communicationExceptionStack = new ArrayList<>();
    for (final IIpcStrategy strategy : mStrategies) {
        try {
            final U result = performStrategy(strategy, operation);
            emitOperationSuccessEvent(operation, result);
            return result;
        } catch (final BrokerCommunicationException communicationException) {
            // Fails to communicate to the . Try next strategy.
            communicationExceptionStack.add(communicationException);
        } catch (final BaseException exception) {
            emitOperationFailureEvent((BrokerOperation<U>) operation, exception);
            throw exception;
        }
    }
    final ClientException exception = new ClientException(ErrorStrings.BROKER_BIND_SERVICE_FAILED, "Unable to connect to the broker. Please refer to MSAL/Broker logs " + "or suppressed exception (API 19+) for more details.");
    // This means that we've tried every strategies... log everything...
    for (final BrokerCommunicationException e : communicationExceptionStack) {
        Logger.error(TAG + methodName, e.getMessage(), e);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            exception.addSuppressed(e);
        }
    }
    emitOperationFailureEvent(operation, exception);
    throw exception;
}
Also used : BaseException(com.microsoft.identity.common.exception.BaseException) IIpcStrategy(com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy) ArrayList(java.util.ArrayList) ClientException(com.microsoft.identity.common.exception.ClientException) BrokerCommunicationException(com.microsoft.identity.common.exception.BrokerCommunicationException)

Example 4 with IIpcStrategy

use of com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerMsalController method getIpcStrategies.

/**
 * Gets a list of communication strategies.
 * Order of objects in the list will reflects the order of strategies that will be used.
 */
@NonNull
private static List<IIpcStrategy> getIpcStrategies(final Context applicationContext, final String activeBrokerPackageName) {
    final List<IIpcStrategy> strategies = new ArrayList<>();
    final StringBuilder sb = new StringBuilder(100);
    sb.append("Broker Strategies added : ");
    final ContentProviderStrategy contentProviderStrategy = new ContentProviderStrategy(applicationContext);
    if (contentProviderStrategy.isBrokerContentProviderAvailable(activeBrokerPackageName)) {
        sb.append("ContentProviderStrategy, ");
        strategies.add(contentProviderStrategy);
    }
    final MicrosoftAuthClient client = new MicrosoftAuthClient(applicationContext);
    if (client.isBoundServiceSupported(activeBrokerPackageName)) {
        sb.append("BoundServiceStrategy, ");
        strategies.add(new BoundServiceStrategy<>(client));
    }
    if (AccountManagerUtil.canUseAccountManagerOperation(applicationContext)) {
        sb.append("AccountManagerStrategy.");
        strategies.add(new AccountManagerAddAccountStrategy(applicationContext));
    }
    Logger.info(TAG, sb.toString());
    return strategies;
}
Also used : ContentProviderStrategy(com.microsoft.identity.common.internal.broker.ipc.ContentProviderStrategy) IIpcStrategy(com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy) ArrayList(java.util.ArrayList) MicrosoftAuthClient(com.microsoft.identity.common.internal.broker.MicrosoftAuthClient) AccountManagerAddAccountStrategy(com.microsoft.identity.common.internal.broker.ipc.AccountManagerAddAccountStrategy) NonNull(androidx.annotation.NonNull)

Example 5 with IIpcStrategy

use of com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerOperationExecutorTests method getStrategyWithValidResult.

private IIpcStrategy getStrategyWithValidResult() {
    return new IIpcStrategy() {

        @Override
        @NonNull
        public Bundle communicateToBroker(@NonNull BrokerOperationBundle bundle) throws BrokerCommunicationException {
            final Bundle result = new Bundle();
            result.putBoolean(SUCCESS_BUNDLE_KEY, true);
            return result;
        }

        @Override
        public Type getType() {
            return MOCK_TYPE;
        }
    };
}
Also used : BrokerOperationBundle(com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle) Bundle(android.os.Bundle) BrokerOperationBundle(com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle) IIpcStrategy(com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy) NonNull(androidx.annotation.NonNull)

Aggregations

IIpcStrategy (com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy)9 Bundle (android.os.Bundle)5 NonNull (androidx.annotation.NonNull)5 BaseException (com.microsoft.identity.common.exception.BaseException)5 BrokerOperationBundle (com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle)5 BrokerCommunicationException (com.microsoft.identity.common.exception.BrokerCommunicationException)3 ArrayList (java.util.ArrayList)2 Context (android.content.Context)1 BaseBundle (android.os.BaseBundle)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 MicrosoftAuthClient (com.microsoft.identity.common.internal.broker.MicrosoftAuthClient)1 AccountManagerAddAccountStrategy (com.microsoft.identity.common.internal.broker.ipc.AccountManagerAddAccountStrategy)1 ContentProviderStrategy (com.microsoft.identity.common.internal.broker.ipc.ContentProviderStrategy)1 CommandParameters (com.microsoft.identity.common.internal.commands.parameters.CommandParameters)1 BrokerMsalController (com.microsoft.identity.common.internal.controllers.BrokerMsalController)1 Test (org.junit.Test)1