Search in sources :

Example 61 with ClientException

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

the class MsalBrokerResultAdapter method authenticationResultFromBundle.

@Override
@NonNull
public ILocalAuthenticationResult authenticationResultFromBundle(@NonNull final Bundle resultBundle) throws ClientException {
    final BrokerResult brokerResult = brokerResultFromBundle(resultBundle);
    Logger.info(TAG, "Broker Result returned from Bundle, constructing authentication result");
    final List<ICacheRecord> tenantProfileCacheRecords = brokerResult.getTenantProfileData();
    if (tenantProfileCacheRecords == null) {
        Logger.error(TAG, "getTenantProfileData is null", null);
        throw new ClientException(INVALID_BROKER_BUNDLE, "getTenantProfileData is null.");
    }
    return new LocalAuthenticationResult(tenantProfileCacheRecords.get(0), tenantProfileCacheRecords, SdkType.MSAL, brokerResult.isServicedFromCache());
}
Also used : BrokerResult(com.microsoft.identity.common.internal.broker.BrokerResult) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) ClientException(com.microsoft.identity.common.exception.ClientException) NonNull(androidx.annotation.NonNull)

Example 62 with ClientException

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

the class MsalBrokerResultAdapter method verifyHelloFromResultBundle.

@NonNull
public String verifyHelloFromResultBundle(@Nullable final Bundle bundle) throws ClientException {
    final String methodName = ":verifyHelloFromResultBundle";
    // This means that the Broker doesn't support hello().
    if (bundle == null) {
        Logger.warn(TAG + methodName, "The hello result bundle is null.");
        throw new ClientException(ErrorStrings.UNSUPPORTED_BROKER_VERSION_ERROR_CODE, ErrorStrings.UNSUPPORTED_BROKER_VERSION_ERROR_MESSAGE);
    }
    final String negotiatedBrokerProtocolVersion = bundle.getString(AuthenticationConstants.Broker.NEGOTIATED_BP_VERSION_KEY);
    if (!StringUtil.isEmpty(negotiatedBrokerProtocolVersion)) {
        Logger.info(TAG + methodName, "Able to establish the connect, " + "the broker protocol version in common is [" + negotiatedBrokerProtocolVersion + "]");
        return negotiatedBrokerProtocolVersion;
    }
    if (!StringUtil.isEmpty(bundle.getString(AuthenticationConstants.OAuth2.ERROR)) && !StringUtil.isEmpty(bundle.getString(AuthenticationConstants.OAuth2.ERROR_DESCRIPTION))) {
        final String errorCode = bundle.getString(AuthenticationConstants.OAuth2.ERROR);
        final String errorMessage = bundle.getString(AuthenticationConstants.OAuth2.ERROR_DESCRIPTION);
        throw new ClientException(errorCode, errorMessage);
    }
    final Object resultObject = bundle.get(AuthenticationConstants.Broker.BROKER_RESULT_V2);
    if (resultObject instanceof BrokerResult) {
        // for the back compatibility purpose to version 3.0.4 and 3.0.6.
        final BrokerResult brokerResult = (BrokerResult) resultObject;
        throw new ClientException(brokerResult.getErrorCode(), brokerResult.getErrorMessage());
    }
    // This means that the Broker doesn't support hello().
    Logger.warn(TAG + methodName, "The result bundle is not in a recognizable format.");
    throw new ClientException(ErrorStrings.UNSUPPORTED_BROKER_VERSION_ERROR_CODE, ErrorStrings.UNSUPPORTED_BROKER_VERSION_ERROR_MESSAGE);
}
Also used : BrokerResult(com.microsoft.identity.common.internal.broker.BrokerResult) GzipUtil.compressString(com.microsoft.identity.common.internal.util.GzipUtil.compressString) ClientException(com.microsoft.identity.common.exception.ClientException) NonNull(androidx.annotation.NonNull)

Example 63 with ClientException

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

the class MsalBrokerResultAdapter method getAccountsFromResultBundle.

@NonNull
public List<ICacheRecord> getAccountsFromResultBundle(@NonNull final Bundle bundle) throws BaseException {
    String accountJson;
    final byte[] compressedData = bundle.getByteArray(BROKER_ACCOUNTS_COMPRESSED);
    if (compressedData != null) {
        try {
            accountJson = GzipUtil.decompressBytesToString(compressedData);
        } catch (IOException e) {
            Logger.error(TAG, " Failed to decompress account list to bytes", e);
            throw new ClientException(INVALID_BROKER_BUNDLE, " Failed to decompress account list to bytes.");
        }
    } else {
        accountJson = bundle.getString(BROKER_ACCOUNTS);
    }
    if (StringUtil.isEmpty(accountJson)) {
        throw new MsalBrokerResultAdapter().getBaseExceptionFromBundle(bundle);
    }
    return JsonExtensions.getICacheRecordListFromJsonString(accountJson);
}
Also used : GzipUtil.compressString(com.microsoft.identity.common.internal.util.GzipUtil.compressString) IOException(java.io.IOException) ClientException(com.microsoft.identity.common.exception.ClientException) NonNull(androidx.annotation.NonNull)

Example 64 with ClientException

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

the class MsalBrokerResultAdapter method getIntentForInteractiveRequestFromResultBundle.

@NonNull
public Intent getIntentForInteractiveRequestFromResultBundle(@NonNull final Bundle resultBundle, @NonNull final String negotiatedBrokerProtocolVersion) throws ClientException {
    final Bundle interactiveRequestBundle = extractInteractiveRequestBundleFromResultBundle(resultBundle);
    final String packageName = interactiveRequestBundle.getString(BROKER_PACKAGE_NAME);
    final String className = interactiveRequestBundle.getString(BROKER_ACTIVITY_NAME);
    if (StringUtil.isEmpty(packageName) || StringUtil.isEmpty(className)) {
        throw new ClientException(INVALID_BROKER_BUNDLE, "Content of Authorization Intent's package and class name should not be null.");
    }
    final Intent intent = new Intent();
    intent.setPackage(packageName);
    intent.setClassName(packageName, className);
    intent.putExtras(interactiveRequestBundle);
    intent.putExtra(NEGOTIATED_BP_VERSION_KEY, negotiatedBrokerProtocolVersion);
    return intent;
}
Also used : Bundle(android.os.Bundle) Intent(android.content.Intent) GzipUtil.compressString(com.microsoft.identity.common.internal.util.GzipUtil.compressString) ClientException(com.microsoft.identity.common.exception.ClientException) NonNull(androidx.annotation.NonNull)

Example 65 with ClientException

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

the class JWSBuilder method sign.

/**
 * Signs the input with the private key.
 *
 * @param privateKey the key to sign input with
 * @param input      the data that needs to be signed
 * @return String signed string
 */
private static String sign(RSAPrivateKey privateKey, final byte[] input) throws ClientException {
    final Signature signer;
    try {
        signer = Signature.getInstance(JWS_ALGORITHM);
        signer.initSign(privateKey);
        signer.update(input);
        return StringExtensions.encodeBase64URLSafeString(signer.sign());
    } catch (InvalidKeyException e) {
        throw new ClientException(ErrorStrings.KEY_CHAIN_PRIVATE_KEY_EXCEPTION, "Invalid private RSA key: " + e.getMessage(), e);
    } catch (SignatureException e) {
        throw new ClientException(ErrorStrings.SIGNATURE_EXCEPTION, "RSA signature exception: " + e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new ClientException(ErrorStrings.UNSUPPORTED_ENCODING, "Unsupported encoding", e);
    } catch (NoSuchAlgorithmException e) {
        throw new ClientException(ErrorStrings.NO_SUCH_ALGORITHM, "Unsupported RSA algorithm: " + e.getMessage(), e);
    }
}
Also used : Signature(java.security.Signature) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientException(com.microsoft.identity.common.exception.ClientException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

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