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);
}
}
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);
}
}
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);
}
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;
}
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());
}
}
Aggregations