Search in sources :

Example 26 with ClientException

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

the class Authority method getKnownAuthorityResult.

public static KnownAuthorityResult getKnownAuthorityResult(Authority authority) {
    final String methodName = ":getKnownAuthorityResult";
    Logger.verbose(TAG + methodName, "Getting known authority result...");
    ClientException clientException = null;
    boolean known = false;
    try {
        Logger.info(TAG + methodName, "Performing cloud discovery");
        performCloudDiscovery();
    } catch (final IOException ex) {
        clientException = new ClientException(ClientException.IO_ERROR, "Unable to perform cloud discovery", ex);
    }
    Logger.info(TAG + methodName, "Cloud discovery complete.");
    if (clientException == null) {
        if (!isKnownAuthority(authority)) {
            clientException = new ClientException(ClientException.UNKNOWN_AUTHORITY, "Provided authority is not known.  MSAL will only make requests to known authorities");
        } else {
            Logger.info(TAG + methodName, "Cloud is known.");
            known = true;
        }
    }
    return new KnownAuthorityResult(known, clientException);
}
Also used : ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException)

Example 27 with ClientException

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

the class BrokerMsalController method saveMsaAccountToCache.

/**
 * Checks if the account returns is a MSA Account and sets single on state in cache
 */
private void saveMsaAccountToCache(@NonNull final Bundle resultBundle, @SuppressWarnings(WarningType.rawtype_warning) @NonNull final MsalOAuth2TokenCache msalOAuth2TokenCache) throws BaseException {
    final String methodName = ":saveMsaAccountToCache";
    final BrokerResult brokerResult = new MsalBrokerResultAdapter().brokerResultFromBundle(resultBundle);
    if (resultBundle.getBoolean(AuthenticationConstants.Broker.BROKER_REQUEST_V2_SUCCESS) && AzureActiveDirectoryAudience.MSA_MEGA_TENANT_ID.equalsIgnoreCase(brokerResult.getTenantId())) {
        Logger.info(TAG + methodName, "Result returned for MSA Account, saving to cache");
        if (StringUtil.isEmpty(brokerResult.getClientInfo())) {
            Logger.error(TAG + methodName, "ClientInfo is empty.", null);
            throw new ClientException(ErrorStrings.UNKNOWN_ERROR, "ClientInfo is empty.");
        }
        try {
            final ClientInfo clientInfo = new ClientInfo(brokerResult.getClientInfo());
            final MicrosoftStsAccount microsoftStsAccount = new MicrosoftStsAccount(new IDToken(brokerResult.getIdToken()), clientInfo);
            microsoftStsAccount.setEnvironment(brokerResult.getEnvironment());
            final MicrosoftRefreshToken microsoftRefreshToken = new MicrosoftRefreshToken(brokerResult.getRefreshToken(), clientInfo, brokerResult.getScope(), brokerResult.getClientId(), brokerResult.getEnvironment(), brokerResult.getFamilyId());
            msalOAuth2TokenCacheSetSingleSignOnState(msalOAuth2TokenCache, microsoftStsAccount, microsoftRefreshToken);
        } catch (ServiceException e) {
            Logger.errorPII(TAG + methodName, "Exception while creating Idtoken or ClientInfo," + " cannot save MSA account tokens", e);
            throw new ClientException(ErrorStrings.INVALID_JWT, e.getMessage(), e);
        }
    }
}
Also used : BrokerResult(com.microsoft.identity.common.internal.broker.BrokerResult) MsalBrokerResultAdapter(com.microsoft.identity.common.internal.result.MsalBrokerResultAdapter) MicrosoftStsAccount(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsAccount) MicrosoftRefreshToken(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftRefreshToken) ServiceException(com.microsoft.identity.common.exception.ServiceException) IDToken(com.microsoft.identity.common.internal.providers.oauth2.IDToken) ClientException(com.microsoft.identity.common.exception.ClientException) ClientInfo(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)

Example 28 with ClientException

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

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

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

the class AndroidKeystoreAsymmetricRsaKeyFactory method generateAsymmetricKey.

@Override
public synchronized AsymmetricRsaKey generateAsymmetricKey(@NonNull final String alias) throws ClientException {
    final Exception exception;
    final String errCode;
    try {
        return new AndroidKeystoreAsymmetricRsaKey(mContext, new DevicePopManager(alias), alias);
    } catch (final KeyStoreException e) {
        exception = e;
        errCode = KEYSTORE_NOT_INITIALIZED;
    } catch (final CertificateException e) {
        exception = e;
        errCode = CERTIFICATE_LOAD_FAILURE;
    } catch (final NoSuchAlgorithmException e) {
        exception = e;
        errCode = NO_SUCH_ALGORITHM;
    } catch (final IOException e) {
        exception = e;
        errCode = IO_ERROR;
    }
    throw new ClientException(errCode, exception.getMessage(), exception);
}
Also used : CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) ClientException(com.microsoft.identity.common.exception.ClientException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException)

Aggregations

ClientException (com.microsoft.identity.common.exception.ClientException)74 IOException (java.io.IOException)23 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)23 InvalidKeyException (java.security.InvalidKeyException)18 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)17 KeyStoreException (java.security.KeyStoreException)17 BadPaddingException (javax.crypto.BadPaddingException)17 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)17 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)17 UnrecoverableEntryException (java.security.UnrecoverableEntryException)15 CertificateException (java.security.cert.CertificateException)13 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 SignatureException (java.security.SignatureException)11 KeyPermanentlyInvalidatedException (android.security.keystore.KeyPermanentlyInvalidatedException)10 StrongBoxUnavailableException (android.security.keystore.StrongBoxUnavailableException)10 NonNull (androidx.annotation.NonNull)10 JOSEException (com.nimbusds.jose.JOSEException)10 NoSuchProviderException (java.security.NoSuchProviderException)10 ProviderException (java.security.ProviderException)10 JSONException (org.json.JSONException)10