Search in sources :

Example 1 with BrokerCommunicationException

use of com.microsoft.identity.common.exception.BrokerCommunicationException in project microsoft-authentication-library-common-for-android by AzureAD.

the class MicrosoftAuthClient method performOperationInternal.

@Override
@Nullable
Bundle performOperationInternal(@NonNull final BrokerOperationBundle brokerOperationBundle, @NonNull final IMicrosoftAuthService microsoftAuthService) throws RemoteException, BrokerCommunicationException {
    final Bundle inputBundle = brokerOperationBundle.getBundle();
    switch(brokerOperationBundle.getOperation()) {
        case MSAL_HELLO:
            return microsoftAuthService.hello(inputBundle);
        case MSAL_GET_INTENT_FOR_INTERACTIVE_REQUEST:
            final Intent intent = microsoftAuthService.getIntentForInteractiveRequest();
            final Bundle bundle = intent.getExtras();
            // older brokers (pre-ContentProvider) are ONLY sending these values in the intent itself.
            if (intent.getComponent() != null && !TextUtils.isEmpty(intent.getPackage()) && !TextUtils.isEmpty(intent.getComponent().getClassName())) {
                bundle.putString(BROKER_PACKAGE_NAME, intent.getPackage());
                bundle.putString(BROKER_ACTIVITY_NAME, intent.getComponent().getClassName());
            }
            return bundle;
        case MSAL_ACQUIRE_TOKEN_SILENT:
            return microsoftAuthService.acquireTokenSilently(inputBundle);
        case MSAL_GET_ACCOUNTS:
            return microsoftAuthService.getAccounts(inputBundle);
        case MSAL_REMOVE_ACCOUNT:
            return microsoftAuthService.removeAccount(inputBundle);
        case MSAL_GET_DEVICE_MODE:
            return microsoftAuthService.getDeviceMode();
        case MSAL_GET_CURRENT_ACCOUNT_IN_SHARED_DEVICE:
            return microsoftAuthService.getCurrentAccount(inputBundle);
        case MSAL_SIGN_OUT_FROM_SHARED_DEVICE:
            return microsoftAuthService.removeAccountFromSharedDevice(inputBundle);
        case MSAL_GENERATE_SHR:
            return microsoftAuthService.generateSignedHttpRequest(inputBundle);
        default:
            final String errorMessage = "Operation " + brokerOperationBundle.getOperation().name() + " is not supported by MicrosoftAuthClient.";
            throw new BrokerCommunicationException(OPERATION_NOT_SUPPORTED_ON_CLIENT_SIDE, BOUND_SERVICE, errorMessage, null);
    }
}
Also used : Bundle(android.os.Bundle) BrokerOperationBundle(com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle) Intent(android.content.Intent) BrokerCommunicationException(com.microsoft.identity.common.exception.BrokerCommunicationException) Nullable(androidx.annotation.Nullable)

Example 2 with BrokerCommunicationException

use of com.microsoft.identity.common.exception.BrokerCommunicationException in project microsoft-authentication-library-common-for-android by AzureAD.

the class ContentProviderStrategy method communicateToBroker.

@Override
@Nullable
public Bundle communicateToBroker(@NonNull final BrokerOperationBundle brokerOperationBundle) throws BrokerCommunicationException {
    final String methodName = brokerOperationBundle.getOperation().name();
    final Uri uri = getContentProviderURI(brokerOperationBundle.getTargetBrokerAppPackageName(), brokerOperationBundle.getContentProviderPath());
    Logger.info(TAG + methodName, "Request to BrokerContentProvider for uri path " + brokerOperationBundle.getContentProviderPath());
    String marshalledRequestString = null;
    final Bundle requestBundle = brokerOperationBundle.getBundle();
    if (requestBundle != null) {
        byte[] marshalledBytes = ParcelableUtil.marshall(requestBundle);
        marshalledRequestString = Base64.encodeToString(marshalledBytes, 0);
    }
    final Cursor cursor = mContext.getContentResolver().query(uri, null, marshalledRequestString, null, null);
    if (cursor != null) {
        final Bundle resultBundle = cursor.getExtras();
        cursor.close();
        if (resultBundle == null) {
            final String message = "Received an empty bundle. This means the operation is not supported on the other side. " + "If you're using a newer feature, please bump the minimum protocol version.";
            Logger.error(TAG + methodName, message, null);
            throw new BrokerCommunicationException(OPERATION_NOT_SUPPORTED_ON_SERVER_SIDE, getType(), message, null);
        }
        Logger.info(TAG + methodName, "Received successful result from Broker Content Provider.");
        return resultBundle;
    } else {
        final String message = "Failed to get result from Broker Content Provider, cursor is null";
        Logger.error(TAG + methodName, message, null);
        throw new BrokerCommunicationException(CONNECTION_ERROR, getType(), message, null);
    }
}
Also used : Bundle(android.os.Bundle) BrokerCommunicationException(com.microsoft.identity.common.exception.BrokerCommunicationException) Cursor(android.database.Cursor) Uri(android.net.Uri) Nullable(androidx.annotation.Nullable)

Example 3 with BrokerCommunicationException

use of com.microsoft.identity.common.exception.BrokerCommunicationException in project microsoft-authentication-library-common-for-android by AzureAD.

the class BoundServiceClient method connect.

/**
 * Binds to the service.
 *
 * @param targetServicePackageName Package name of the app this client will talk to.
 */
@NonNull
protected T connect(@NonNull final String targetServicePackageName) throws BrokerCommunicationException, InterruptedException, TimeoutException, ExecutionException {
    final String methodName = ":connect";
    if (!isBoundServiceSupported(targetServicePackageName)) {
        final String errorMessage = "Bound service is not supported.";
        Logger.info(TAG + methodName, errorMessage);
        throw new BrokerCommunicationException(OPERATION_NOT_SUPPORTED_ON_SERVER_SIDE, BOUND_SERVICE, errorMessage, null);
    }
    final ResultFuture<IBinder> future = new ResultFuture<>();
    mConnection = new BoundServiceConnection(future);
    mHasStartedBinding = mContext.bindService(getIntentForBoundService(targetServicePackageName), mConnection, Context.BIND_AUTO_CREATE);
    if (!mHasStartedBinding) {
        final String errorMessage = "failed to bind. The service is not available.";
        Logger.info(TAG + methodName, errorMessage);
        throw new BrokerCommunicationException(OPERATION_NOT_SUPPORTED_ON_SERVER_SIDE, BOUND_SERVICE, errorMessage, null);
    }
    Logger.info(TAG + "connect", "Android is establishing the bound service connection.");
    final IBinder binder = future.get(mTimeOutInSeconds, TimeUnit.SECONDS);
    return getInterfaceFromIBinder(binder);
}
Also used : IBinder(android.os.IBinder) ResultFuture(com.microsoft.identity.common.internal.result.ResultFuture) BrokerCommunicationException(com.microsoft.identity.common.exception.BrokerCommunicationException) NonNull(androidx.annotation.NonNull)

Example 4 with BrokerCommunicationException

use of com.microsoft.identity.common.exception.BrokerCommunicationException 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 5 with BrokerCommunicationException

use of com.microsoft.identity.common.exception.BrokerCommunicationException in project microsoft-authentication-library-common-for-android by AzureAD.

the class IpcStrategyTests method testIpcConnectionFailed.

protected void testIpcConnectionFailed(@NonNull final BrokerOperationBundle bundle) {
    final IIpcStrategy strategy = getStrategy();
    try {
        strategy.communicateToBroker(bundle);
        Assert.fail("Operation should fail.");
    } catch (BaseException e) {
        Assert.assertTrue(e instanceof BrokerCommunicationException);
        Assert.assertSame(((BrokerCommunicationException) e).getCategory(), CONNECTION_ERROR);
        Assert.assertSame(((BrokerCommunicationException) e).getStrategyType(), strategy.getType());
    }
}
Also used : BaseException(com.microsoft.identity.common.exception.BaseException) IIpcStrategy(com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy) BrokerCommunicationException(com.microsoft.identity.common.exception.BrokerCommunicationException)

Aggregations

BrokerCommunicationException (com.microsoft.identity.common.exception.BrokerCommunicationException)8 Bundle (android.os.Bundle)3 Nullable (androidx.annotation.Nullable)3 BaseException (com.microsoft.identity.common.exception.BaseException)3 IIpcStrategy (com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy)3 AuthenticatorException (android.accounts.AuthenticatorException)1 OperationCanceledException (android.accounts.OperationCanceledException)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 IBinder (android.os.IBinder)1 NonNull (androidx.annotation.NonNull)1 API (com.microsoft.identity.common.adal.internal.AuthenticationConstants.BrokerContentProvider.API)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 BrokerOperationBundle (com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle)1 ResultFuture (com.microsoft.identity.common.internal.result.ResultFuture)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1