Search in sources :

Example 1 with UiRequiredException

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

the class ExceptionAdapter method getExceptionFromTokenErrorResponse.

/**
 * Get an exception object from the given oAuth values.
 *
 * @param errorResponse
 * @return ServiceException, UiRequiredException
 */
public static ServiceException getExceptionFromTokenErrorResponse(@NonNull final TokenErrorResponse errorResponse) {
    final ServiceException outErr;
    if (shouldBeConvertedToUiRequiredException(errorResponse.getError())) {
        outErr = new UiRequiredException(errorResponse.getError(), errorResponse.getErrorDescription());
    } else {
        outErr = new ServiceException(errorResponse.getError(), errorResponse.getErrorDescription(), null);
    }
    outErr.setOauthSubErrorCode(errorResponse.getSubError());
    setHttpResponseUsingTokenErrorResponse(outErr, errorResponse);
    return outErr;
}
Also used : ServiceException(com.microsoft.identity.common.exception.ServiceException) UiRequiredException(com.microsoft.identity.common.exception.UiRequiredException)

Example 2 with UiRequiredException

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

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

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

the class GenerateShrCommand method execute.

@Override
public GenerateShrResult execute() throws Exception {
    final String methodName = ":execute";
    GenerateShrResult result = null;
    final GenerateShrCommandParameters parameters = (GenerateShrCommandParameters) getParameters();
    // Iterate over our controllers, to service the request either locally or via the broker...
    // if the local (embedded) cache contains tokens for the supplied user, we will sign using
    // the embedded PoP keys. If no local user-state exists, the broker will be delegated to
    // where the same check is performed.
    BaseController controller;
    for (int ii = 0; ii < getControllers().size(); ii++) {
        controller = getControllers().get(ii);
        com.microsoft.identity.common.internal.logging.Logger.verbose(TAG + methodName, "Executing with controller: " + controller.getClass().getSimpleName());
        result = controller.generateSignedHttpRequest(parameters);
        if (null != result.getErrorCode()) {
            final String errorCode = result.getErrorCode();
            final String errorMessage = result.getErrorMessage();
            // of as thrown Exceptions
            if (NO_ACCOUNT_FOUND.equalsIgnoreCase(errorCode)) {
                if (getControllers().size() > ii + 1) {
                    // Try our next controller
                    continue;
                } else {
                    throw new UiRequiredException(errorCode, errorMessage);
                }
            } else {
                throw new ClientException(errorCode, errorMessage);
            }
        }
    }
    return result;
}
Also used : GenerateShrResult(com.microsoft.identity.common.internal.result.GenerateShrResult) BaseController(com.microsoft.identity.common.internal.controllers.BaseController) UiRequiredException(com.microsoft.identity.common.exception.UiRequiredException) ClientException(com.microsoft.identity.common.exception.ClientException) GenerateShrCommandParameters(com.microsoft.identity.common.internal.commands.parameters.GenerateShrCommandParameters)

Example 5 with UiRequiredException

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

the class SilentTokenCommand method execute.

@Override
public AcquireTokenResult execute() throws Exception {
    AcquireTokenResult result = null;
    final String methodName = ":execute";
    for (int ii = 0; ii < this.getControllers().size(); ii++) {
        final BaseController controller = this.getControllers().get(ii);
        try {
            com.microsoft.identity.common.internal.logging.Logger.verbose(TAG + methodName, "Executing with controller: " + controller.getClass().getSimpleName());
            result = controller.acquireTokenSilent((SilentTokenCommandParameters) getParameters());
            if (result.getSucceeded()) {
                com.microsoft.identity.common.internal.logging.Logger.verbose(TAG + methodName, "Executing with controller: " + controller.getClass().getSimpleName() + ": Succeeded");
                return result;
            }
        } catch (UiRequiredException | ClientException e) {
            if (// was invalid_grant
            e.getErrorCode().equals(AuthenticationConstants.OAuth2ErrorCode.INVALID_GRANT) && this.getControllers().size() > ii + 1) {
                // isn't the last controller we can try
                continue;
            } else if ((e.getErrorCode().equals(ErrorStrings.NO_TOKENS_FOUND) || e.getErrorCode().equals(ErrorStrings.NO_ACCOUNT_FOUND)) && this.getControllers().size() > ii + 1) {
                // if no token or account for this silent call, we should continue to the next silent call.
                continue;
            } else {
                throw e;
            }
        }
    }
    return result;
}
Also used : AcquireTokenResult(com.microsoft.identity.common.internal.result.AcquireTokenResult) SilentTokenCommandParameters(com.microsoft.identity.common.internal.commands.parameters.SilentTokenCommandParameters) BaseController(com.microsoft.identity.common.internal.controllers.BaseController) UiRequiredException(com.microsoft.identity.common.exception.UiRequiredException) ClientException(com.microsoft.identity.common.exception.ClientException)

Aggregations

UiRequiredException (com.microsoft.identity.common.exception.UiRequiredException)5 ClientException (com.microsoft.identity.common.exception.ClientException)4 NonNull (androidx.annotation.NonNull)2 ArgumentException (com.microsoft.identity.common.exception.ArgumentException)2 BaseException (com.microsoft.identity.common.exception.BaseException)2 UserCancelException (com.microsoft.identity.common.exception.UserCancelException)2 BaseController (com.microsoft.identity.common.internal.controllers.BaseController)2 ServiceException (com.microsoft.identity.common.exception.ServiceException)1 GenerateShrCommandParameters (com.microsoft.identity.common.internal.commands.parameters.GenerateShrCommandParameters)1 SilentTokenCommandParameters (com.microsoft.identity.common.internal.commands.parameters.SilentTokenCommandParameters)1 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)1 GenerateShrResult (com.microsoft.identity.common.internal.result.GenerateShrResult)1 GzipUtil.compressString (com.microsoft.identity.common.internal.util.GzipUtil.compressString)1