Search in sources :

Example 6 with BaseException

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

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

the class MsalBrokerResultAdapter method getBaseExceptionFromErrorCodes.

/**
 * Method to get the right base exception based on error codes.
 * Note : In newer versions of Broker, exception type will be sent and is used to determine the right exception.
 * <p>
 * This method is to support legacy broker versions (3.1.8 or below)
 *
 * @return {@link BaseException}
 */
@NonNull
private BaseException getBaseExceptionFromErrorCodes(@NonNull final BrokerResult brokerResult) {
    final String errorCode = brokerResult.getErrorCode();
    final BaseException baseException;
    // INTERACTION_REQUIRED is marked as deprecated
    if (AuthenticationConstants.OAuth2ErrorCode.INTERACTION_REQUIRED.equalsIgnoreCase(errorCode) || AuthenticationConstants.OAuth2ErrorCode.INVALID_GRANT.equalsIgnoreCase(errorCode) || ErrorStrings.INVALID_BROKER_REFRESH_TOKEN.equalsIgnoreCase(errorCode) || ErrorStrings.NO_TOKENS_FOUND.equalsIgnoreCase(errorCode)) {
        Logger.warn(TAG, "Received a UIRequired exception from Broker : " + errorCode);
        baseException = new UiRequiredException(errorCode, brokerResult.getErrorMessage());
    } else if (AuthenticationConstants.OAuth2ErrorCode.UNAUTHORIZED_CLIENT.equalsIgnoreCase(errorCode) && AuthenticationConstants.OAuth2SubErrorCode.PROTECTION_POLICY_REQUIRED.equalsIgnoreCase(brokerResult.getSubErrorCode())) {
        Logger.warn(TAG, "Received a IntuneAppProtectionPolicyRequiredException exception from Broker : " + errorCode);
        baseException = getIntuneProtectionRequiredException(brokerResult);
    } else if (ErrorStrings.USER_CANCELLED.equalsIgnoreCase(errorCode)) {
        Logger.warn(TAG, "Received a User cancelled exception from Broker : " + errorCode);
        baseException = new UserCancelException();
    } else if (ArgumentException.ILLEGAL_ARGUMENT_ERROR_CODE.equalsIgnoreCase(errorCode)) {
        Logger.warn(TAG, "Received a Argument exception from Broker : " + errorCode);
        baseException = new ArgumentException(ArgumentException.BROKER_TOKEN_REQUEST_OPERATION_NAME, errorCode, brokerResult.getErrorMessage());
    } else if (!StringUtil.isEmpty(brokerResult.getHttpResponseHeaders()) || !StringUtil.isEmpty(brokerResult.getHttpResponseBody())) {
        Logger.warn(TAG, "Received a Service exception from Broker : " + errorCode);
        baseException = getServiceException(brokerResult);
    } else {
        Logger.warn(TAG, "Received a Client exception from Broker : " + errorCode);
        baseException = new ClientException(brokerResult.getErrorCode(), brokerResult.getErrorMessage());
    }
    baseException.setCliTelemErrorCode(brokerResult.getCliTelemErrorCode());
    baseException.setCliTelemSubErrorCode(brokerResult.getCliTelemSubErrorCode());
    baseException.setCorrelationId(brokerResult.getCorrelationId());
    baseException.setSpeRing(brokerResult.getSpeRing());
    baseException.setRefreshTokenAge(brokerResult.getRefreshTokenAge());
    return baseException;
}
Also used : BaseException(com.microsoft.identity.common.exception.BaseException) UiRequiredException(com.microsoft.identity.common.exception.UiRequiredException) UserCancelException(com.microsoft.identity.common.exception.UserCancelException) GzipUtil.compressString(com.microsoft.identity.common.internal.util.GzipUtil.compressString) ArgumentException(com.microsoft.identity.common.exception.ArgumentException) ClientException(com.microsoft.identity.common.exception.ClientException) NonNull(androidx.annotation.NonNull)

Example 8 with BaseException

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

the class MsalBrokerResultAdapter method getBaseExceptionFromExceptionType.

@NonNull
private BaseException getBaseExceptionFromExceptionType(@NonNull final String exceptionType, @NonNull final BrokerResult brokerResult) {
    BaseException baseException;
    Logger.warn(TAG, "Received a " + exceptionType + " from Broker : " + brokerResult.getErrorCode());
    if (exceptionType.equalsIgnoreCase(UiRequiredException.sName)) {
        baseException = new UiRequiredException(brokerResult.getErrorCode(), brokerResult.getErrorMessage());
    } else if (exceptionType.equalsIgnoreCase(ServiceException.sName)) {
        baseException = getServiceException(brokerResult);
    } else if (exceptionType.equalsIgnoreCase(IntuneAppProtectionPolicyRequiredException.sName)) {
        baseException = getIntuneProtectionRequiredException(brokerResult);
    } else if (exceptionType.equalsIgnoreCase(UserCancelException.sName)) {
        baseException = new UserCancelException();
    } else if (exceptionType.equalsIgnoreCase(ClientException.sName)) {
        baseException = new ClientException(brokerResult.getErrorCode(), brokerResult.getErrorMessage());
    } else if (exceptionType.equalsIgnoreCase(ArgumentException.sName)) {
        baseException = new ArgumentException(ArgumentException.BROKER_TOKEN_REQUEST_OPERATION_NAME, brokerResult.getErrorCode(), brokerResult.getErrorMessage());
    } else {
        // Default to ClientException if null
        Logger.warn(TAG, " Exception type is unknown : " + exceptionType + brokerResult.getErrorCode() + ", defaulting to Client Exception ");
        baseException = new ClientException(brokerResult.getErrorCode(), brokerResult.getErrorMessage());
    }
    baseException.setCliTelemErrorCode(brokerResult.getCliTelemErrorCode());
    baseException.setCliTelemSubErrorCode(brokerResult.getCliTelemSubErrorCode());
    baseException.setCorrelationId(brokerResult.getCorrelationId());
    baseException.setSpeRing(brokerResult.getSpeRing());
    baseException.setRefreshTokenAge(brokerResult.getRefreshTokenAge());
    return baseException;
}
Also used : BaseException(com.microsoft.identity.common.exception.BaseException) UiRequiredException(com.microsoft.identity.common.exception.UiRequiredException) UserCancelException(com.microsoft.identity.common.exception.UserCancelException) ClientException(com.microsoft.identity.common.exception.ClientException) ArgumentException(com.microsoft.identity.common.exception.ArgumentException) NonNull(androidx.annotation.NonNull)

Example 9 with BaseException

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

the class BrokerMsalController method acquireToken.

/**
 * Performs interactive acquire token with Broker.
 *
 * @param parameters a {@link InteractiveTokenCommandParameters}
 * @return an {@link AcquireTokenResult}.
 */
@Override
public AcquireTokenResult acquireToken(@NonNull final InteractiveTokenCommandParameters parameters) throws BaseException, InterruptedException {
    Telemetry.emit(new ApiStartEvent().putProperties(parameters).putApiId(TelemetryEventStrings.Api.BROKER_ACQUIRE_TOKEN_INTERACTIVE));
    // Create BrokerResultFuture to block on response from the broker... response will be return as an activity result
    // BrokerActivity will receive the result and ask the API dispatcher to complete the request
    // In completeAcquireToken below we will set the result on the future and unblock the flow.
    mBrokerResultFuture = new BrokerResultFuture();
    // Get the broker interactive parameters intent
    final Intent interactiveRequestIntent = getBrokerAuthorizationIntent(parameters);
    // Pass this intent to the BrokerActivity which will be used to start this activity
    final Intent brokerActivityIntent = new Intent(parameters.getAndroidApplicationContext(), BrokerActivity.class);
    brokerActivityIntent.putExtra(BrokerActivity.BROKER_INTENT, interactiveRequestIntent);
    if (null == parameters.getActivity()) {
        // To support calling from OneAuth-MSAL, which may be initialized without an Activity
        // add Flags to start as a NEW_TASK if we are launching from an application Context
        brokerActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mApplicationContext.startActivity(brokerActivityIntent);
    } else {
        // Start the BrokerActivity using our existing Activity
        parameters.getActivity().startActivity(brokerActivityIntent);
    }
    // Wait to be notified of the result being returned... we could add a timeout here if we want to
    final Bundle resultBundle = mBrokerResultFuture.get();
    // If the request is from MSALCPP , OAuth2TokenCache will be null.
    if (parameters.getOAuth2TokenCache() != null) {
        saveMsaAccountToCache(resultBundle, (MsalOAuth2TokenCache) parameters.getOAuth2TokenCache());
    }
    final AcquireTokenResult result;
    try {
        result = new MsalBrokerResultAdapter().getAcquireTokenResultFromResultBundle(resultBundle);
    } catch (BaseException e) {
        Telemetry.emit(new ApiEndEvent().putException(e).putApiId(TelemetryEventStrings.Api.BROKER_ACQUIRE_TOKEN_INTERACTIVE));
        throw e;
    }
    Telemetry.emit(new ApiEndEvent().putResult(result).putApiId(TelemetryEventStrings.Api.BROKER_ACQUIRE_TOKEN_INTERACTIVE));
    return result;
}
Also used : AcquireTokenResult(com.microsoft.identity.common.internal.result.AcquireTokenResult) MsalBrokerResultAdapter(com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter) BaseException(com.microsoft.identity.common.exception.BaseException) ApiEndEvent(com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent) Bundle(android.os.Bundle) BrokerOperationBundle(com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle) ApiStartEvent(com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent) BrokerResultFuture(com.microsoft.identity.common.internal.broker.BrokerResultFuture) Intent(android.content.Intent)

Example 10 with BaseException

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

the class CommandDispatcher method executeCommand.

/**
 * We need to inspect the AcquireTokenResult type to determine whether the request was successful, cancelled or encountered an exception
 * <p>
 * Execute the command provided to the command dispatcher
 *
 * @param command
 * @return
 */
private static CommandResult executeCommand(@SuppressWarnings(WarningType.rawtype_warning) BaseCommand command) {
    Object result = null;
    BaseException baseException = null;
    CommandResult commandResult;
    try {
        // Try executing request
        result = command.execute();
    } catch (final Exception e) {
        if (e instanceof BaseException) {
            baseException = (BaseException) e;
        } else {
            baseException = ExceptionAdapter.baseExceptionFromException(e);
        }
    }
    if (baseException != null) {
        if (baseException instanceof UserCancelException) {
            commandResult = new CommandResult(CommandResult.ResultStatus.CANCEL, null, command.getParameters().getCorrelationId());
        } else {
            // Post On Error
            commandResult = new CommandResult(CommandResult.ResultStatus.ERROR, baseException, command.getParameters().getCorrelationId());
        }
    } else /* baseException == null */
    {
        if (result != null && result instanceof AcquireTokenResult) {
            // Handler handler, final BaseCommand command, BaseException baseException, AcquireTokenResult result
            commandResult = getCommandResultFromTokenResult(baseException, (AcquireTokenResult) result, command.getParameters());
        } else {
            // For commands that don't return an AcquireTokenResult
            commandResult = new CommandResult(CommandResult.ResultStatus.COMPLETED, result, command.getParameters().getCorrelationId());
        }
    }
    return commandResult;
}
Also used : AcquireTokenResult(com.microsoft.identity.common.internal.result.AcquireTokenResult) BaseException(com.microsoft.identity.common.exception.BaseException) UserCancelException(com.microsoft.identity.common.exception.UserCancelException) UserCancelException(com.microsoft.identity.common.exception.UserCancelException) BaseException(com.microsoft.identity.common.exception.BaseException) BrokerCommunicationException(com.microsoft.identity.common.exception.BrokerCommunicationException) IntuneAppProtectionPolicyRequiredException(com.microsoft.identity.common.exception.IntuneAppProtectionPolicyRequiredException) ClientException(com.microsoft.identity.common.exception.ClientException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

BaseException (com.microsoft.identity.common.exception.BaseException)16 ClientException (com.microsoft.identity.common.exception.ClientException)6 UserCancelException (com.microsoft.identity.common.exception.UserCancelException)5 IIpcStrategy (com.microsoft.identity.common.internal.broker.ipc.IIpcStrategy)5 BrokerOperationExecutor (com.microsoft.identity.common.internal.controllers.BrokerOperationExecutor)5 BrokerCommunicationException (com.microsoft.identity.common.exception.BrokerCommunicationException)4 Bundle (android.os.Bundle)3 NonNull (androidx.annotation.NonNull)3 BrokerOperationBundle (com.microsoft.identity.common.internal.broker.ipc.BrokerOperationBundle)3 ArgumentException (com.microsoft.identity.common.exception.ArgumentException)2 ServiceException (com.microsoft.identity.common.exception.ServiceException)2 UiRequiredException (com.microsoft.identity.common.exception.UiRequiredException)2 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)2 Context (android.content.Context)1 Intent (android.content.Intent)1 BaseBundle (android.os.BaseBundle)1 IntuneAppProtectionPolicyRequiredException (com.microsoft.identity.common.exception.IntuneAppProtectionPolicyRequiredException)1 BrokerResultFuture (com.microsoft.identity.common.internal.broker.BrokerResultFuture)1 TokenCommand (com.microsoft.identity.common.internal.commands.TokenCommand)1 CommandParameters (com.microsoft.identity.common.internal.commands.parameters.CommandParameters)1