Search in sources :

Example 1 with UserCancelException

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

the class BrokerOperationExecutorTests method expectUserCancelledException.

private void expectUserCancelledException(final List<IIpcStrategy> strategyList) {
    try {
        final BrokerOperationExecutor executor = new BrokerOperationExecutor(strategyList);
        executor.execute(getMockParameter(), getBrokerOperation());
        Assert.fail("Failure is expected.");
    } catch (final BaseException e) {
        Assert.assertTrue(e instanceof UserCancelException);
    }
}
Also used : BrokerOperationExecutor(com.microsoft.identity.common.internal.controllers.BrokerOperationExecutor) BaseException(com.microsoft.identity.common.exception.BaseException) UserCancelException(com.microsoft.identity.common.exception.UserCancelException)

Example 2 with UserCancelException

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

the class ExceptionAdapter method exceptionFromAcquireTokenResult.

@Nullable
public static BaseException exceptionFromAcquireTokenResult(final AcquireTokenResult result, final CommandParameters commandParameters) {
    final String methodName = ":exceptionFromAcquireTokenResult";
    @SuppressWarnings(WarningType.rawtype_warning) final AuthorizationResult authorizationResult = result.getAuthorizationResult();
    if (null != authorizationResult) {
        final AuthorizationErrorResponse authorizationErrorResponse = authorizationResult.getAuthorizationErrorResponse();
        if (!authorizationResult.getSuccess()) {
            // THERE ARE CURRENTLY NO USAGES of INVALID_REQUEST
            switch(result.getAuthorizationResult().getAuthorizationStatus()) {
                case FAIL:
                    // Check if the error is to register device and throw DEVICE_REGISTRATION_NEEDED exception
                    if (authorizationErrorResponse instanceof MicrosoftAuthorizationErrorResponse) {
                        MicrosoftAuthorizationErrorResponse microsoftAuthorizationErrorResponse = (MicrosoftAuthorizationErrorResponse) authorizationErrorResponse;
                        if (microsoftAuthorizationErrorResponse.getError().equals(MicrosoftAuthorizationErrorResponse.DEVICE_REGISTRATION_NEEDED)) {
                            return new DeviceRegistrationRequiredException(microsoftAuthorizationErrorResponse.getError(), microsoftAuthorizationErrorResponse.getErrorDescription(), microsoftAuthorizationErrorResponse.getUserName());
                        }
                    }
                    return new ServiceException(authorizationErrorResponse.getError(), authorizationErrorResponse.getErrorDescription(), ServiceException.DEFAULT_STATUS_CODE, null);
                case SDK_CANCEL:
                    return new ClientException(authorizationErrorResponse.getError(), authorizationErrorResponse.getErrorDescription());
                case USER_CANCEL:
                    return new UserCancelException();
            }
        }
    } else {
        Logger.warn(TAG + methodName, "AuthorizationResult was null -- expected for ATS cases.");
    }
    return exceptionFromTokenResult(result.getTokenResult(), commandParameters);
}
Also used : MicrosoftAuthorizationErrorResponse(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftAuthorizationErrorResponse) AuthorizationErrorResponse(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationErrorResponse) MicrosoftAuthorizationErrorResponse(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftAuthorizationErrorResponse) ServiceException(com.microsoft.identity.common.exception.ServiceException) DeviceRegistrationRequiredException(com.microsoft.identity.common.exception.DeviceRegistrationRequiredException) UserCancelException(com.microsoft.identity.common.exception.UserCancelException) ClientException(com.microsoft.identity.common.exception.ClientException) AuthorizationResult(com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult) Nullable(androidx.annotation.Nullable)

Example 3 with UserCancelException

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

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

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

UserCancelException (com.microsoft.identity.common.exception.UserCancelException)6 BaseException (com.microsoft.identity.common.exception.BaseException)5 ClientException (com.microsoft.identity.common.exception.ClientException)4 NonNull (androidx.annotation.NonNull)2 ArgumentException (com.microsoft.identity.common.exception.ArgumentException)2 UiRequiredException (com.microsoft.identity.common.exception.UiRequiredException)2 Nullable (androidx.annotation.Nullable)1 BrokerCommunicationException (com.microsoft.identity.common.exception.BrokerCommunicationException)1 DeviceRegistrationRequiredException (com.microsoft.identity.common.exception.DeviceRegistrationRequiredException)1 IntuneAppProtectionPolicyRequiredException (com.microsoft.identity.common.exception.IntuneAppProtectionPolicyRequiredException)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1 BrokerOperationExecutor (com.microsoft.identity.common.internal.controllers.BrokerOperationExecutor)1 MicrosoftAuthorizationErrorResponse (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftAuthorizationErrorResponse)1 AuthorizationErrorResponse (com.microsoft.identity.common.internal.providers.oauth2.AuthorizationErrorResponse)1 AuthorizationResult (com.microsoft.identity.common.internal.providers.oauth2.AuthorizationResult)1 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)1 GzipUtil.compressString (com.microsoft.identity.common.internal.util.GzipUtil.compressString)1 ExecutionException (java.util.concurrent.ExecutionException)1