Search in sources :

Example 11 with ClientException

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

the class MicrosoftStsOAuth2Strategy method getDeviceAtPopThumbprint.

/**
 * Gets the at/pop device credential's thumbprint.
 *
 * @return The at/pop device credential thumbprint.
 */
@Nullable
public String getDeviceAtPopThumbprint() {
    String atPoPKid = null;
    IDevicePopManager devicePopManager = null;
    try {
        devicePopManager = Device.getDevicePoPManagerInstance();
    } catch (final ClientException e) {
        Logger.error(TAG, e.getMessage(), e);
    }
    if (null != devicePopManager) {
        if (devicePopManager.asymmetricKeyExists()) {
            try {
                atPoPKid = devicePopManager.getAsymmetricKeyThumbprint();
            } catch (final ClientException e) {
                Logger.error(TAG, "Key exists. But failed to load thumbprint.", e);
                throw new RuntimeException(e);
            }
        } else {
            // something has gone seriously wrong.
            throw new RuntimeException("Symmetric keys do not exist.");
        }
    } else {
        Logger.warn(TAG, "DevicePopManager does not exist.");
    }
    return atPoPKid;
}
Also used : IDevicePopManager(com.microsoft.identity.common.internal.platform.IDevicePopManager) ClientException(com.microsoft.identity.common.exception.ClientException) Nullable(androidx.annotation.Nullable)

Example 12 with ClientException

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

the class MicrosoftStsOAuth2Strategy method performPKeyAuthRequest.

private HttpResponse performPKeyAuthRequest(@NonNull final HttpResponse response, @NonNull final MicrosoftStsTokenRequest request) throws IOException, ClientException {
    final String methodName = "#performPkeyAuthRequest";
    final String requestBody = ObjectMapper.serializeObjectToFormUrlEncoded(request);
    final Map<String, String> headers = new TreeMap<>();
    headers.put("client-request-id", DiagnosticContext.getRequestContext().get(DiagnosticContext.CORRELATION_ID));
    headers.putAll(Device.getPlatformIdParameters());
    headers.put(AuthenticationConstants.SdkPlatformFields.PRODUCT, DiagnosticContext.getRequestContext().get(AuthenticationConstants.SdkPlatformFields.PRODUCT));
    headers.put(AuthenticationConstants.SdkPlatformFields.VERSION, Device.getProductVersion());
    headers.put(AuthenticationConstants.AAD.APP_PACKAGE_NAME, request.getClientAppName());
    headers.put(AuthenticationConstants.AAD.APP_VERSION, request.getClientAppVersion());
    final String challengeHeader = response.getHeaders().get(CHALLENGE_REQUEST_HEADER).get(0);
    Logger.info(TAG + methodName, "Device certificate challenge request. ");
    Logger.infoPII(TAG + methodName, "Challenge header: " + challengeHeader);
    try {
        final PKeyAuthChallengeFactory factory = new PKeyAuthChallengeFactory();
        final URL authority = StringExtensions.getUrl(mTokenEndpoint);
        final PKeyAuthChallenge pkeyAuthChallenge = factory.getPKeyAuthChallenge(challengeHeader, authority.toString());
        headers.putAll(PKeyAuthChallengeHandler.getChallengeHeader(pkeyAuthChallenge));
        headers.put(HttpConstants.HeaderField.CONTENT_TYPE, TOKEN_REQUEST_CONTENT_TYPE);
        return httpClient.post(authority, headers, requestBody.getBytes(ObjectMapper.ENCODING_SCHEME));
    } catch (final UnsupportedEncodingException exception) {
        throw new ClientException(ErrorStrings.UNSUPPORTED_ENCODING, "Unsupported encoding", exception);
    }
}
Also used : PKeyAuthChallenge(com.microsoft.identity.common.internal.ui.webview.challengehandlers.PKeyAuthChallenge) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PKeyAuthChallengeFactory(com.microsoft.identity.common.internal.ui.webview.challengehandlers.PKeyAuthChallengeFactory) ClientException(com.microsoft.identity.common.exception.ClientException) TreeMap(java.util.TreeMap) URL(java.net.URL)

Example 13 with ClientException

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

the class SecretKeyAccessor method decrypt.

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public byte[] decrypt(@NonNull final byte[] ciphertext) throws ClientException {
    final String errCode;
    final Exception exception;
    try {
        final KeyStore.SecretKeyEntry entry = mKeyManager.getEntry();
        final SecretKey key = entry.getSecretKey();
        final Cipher c = Cipher.getInstance(suite.cipher().name());
        final GCMParameterSpec ivSpec = new GCMParameterSpec(128, ciphertext, 0, 12);
        c.init(Cipher.DECRYPT_MODE, key, ivSpec);
        final byte[] out = Arrays.copyOfRange(ciphertext, 12, ciphertext.length);
        return c.doFinal(out);
    } catch (final UnrecoverableEntryException e) {
        errCode = INVALID_PROTECTION_PARAMS;
        exception = e;
    } catch (final NoSuchAlgorithmException e) {
        errCode = NO_SUCH_ALGORITHM;
        exception = e;
    } catch (final KeyStoreException e) {
        errCode = KEYSTORE_NOT_INITIALIZED;
        exception = e;
    } catch (final NoSuchPaddingException e) {
        errCode = NO_SUCH_PADDING;
        exception = e;
    } catch (IllegalBlockSizeException e) {
        errCode = INVALID_BLOCK_SIZE;
        exception = e;
    } catch (final BadPaddingException e) {
        errCode = BAD_PADDING;
        exception = e;
    } catch (final InvalidKeyException e) {
        errCode = INVALID_KEY;
        exception = e;
    } catch (final InvalidAlgorithmParameterException e) {
        errCode = INVALID_ALG_PARAMETER;
        exception = e;
    }
    throw new ClientException(errCode, exception.getMessage(), exception);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ClientException(com.microsoft.identity.common.exception.ClientException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) UnrecoverableEntryException(java.security.UnrecoverableEntryException) Cipher(javax.crypto.Cipher) ClientException(com.microsoft.identity.common.exception.ClientException) RequiresApi(androidx.annotation.RequiresApi)

Example 14 with ClientException

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

the class AzureActiveDirectoryClientCredentialsGrantTest method test_ClientCredentials.

@Test
public void test_ClientCredentials() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, IOException {
    final CertificateCredential credential = new CertificateCredential.CertificateCredentialBuilder(CLIENT_ID).clientCertificateMetadata(new ClientCertificateMetadata(CERTIFICATE_ALIAS, null)).keyStoreConfiguration(new KeyStoreConfiguration(KEYSTORE_TYPE, KEYSTORE_PROVIDER, null)).build();
    final String audience = AAD_CLIENT_ASSERTION_AUDIENCE;
    final MicrosoftClientAssertion assertion = new MicrosoftClientAssertion(audience, credential);
    final AzureActiveDirectoryTokenRequest tr = new AzureActiveDirectoryTokenRequest();
    tr.setClientAssertionType(assertion.getClientAssertionType());
    tr.setClientAssertion(assertion.getClientAssertion());
    tr.setClientId(CLIENT_ID);
    tr.setResourceId(RESOURCE);
    tr.setGrantType(GRANT_TYPE);
    final OAuth2StrategyParameters options = new OAuth2StrategyParameters();
    final OAuth2Strategy strategy = new AzureActiveDirectoryOAuth2Strategy(new AzureActiveDirectoryOAuth2Configuration(), options);
    try {
        final TokenResult tokenResult = strategy.requestToken(tr);
        assertEquals(true, tokenResult.getSuccess());
    } catch (final ClientException exception) {
        fail("Unexpected exception.");
    }
}
Also used : AzureActiveDirectoryOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy) MicrosoftClientAssertion(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftClientAssertion) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) AzureActiveDirectoryTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryTokenRequest) ClientCertificateMetadata(com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata) OAuth2StrategyParameters(com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters) KeyStoreConfiguration(com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration) OAuth2Strategy(com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy) AzureActiveDirectoryOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy) CertificateCredential(com.microsoft.identity.common.internal.providers.keys.CertificateCredential) AzureActiveDirectoryOAuth2Configuration(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Configuration) ClientException(com.microsoft.identity.common.exception.ClientException) Test(org.junit.Test)

Example 15 with ClientException

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

the class MicrosoftSTSClientCredentialsGrantTest method test_ClientCredentials.

@Test
public void test_ClientCredentials() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, IOException {
    final CertificateCredential credential = new CertificateCredential.CertificateCredentialBuilder(CLIENT_ID).clientCertificateMetadata(new ClientCertificateMetadata(CERTIFICATE_ALIAS, null)).keyStoreConfiguration(new KeyStoreConfiguration(KEYSTORE_TYPE, KEYSTORE_PROVIDER, null)).build();
    final String audience = MSSTS_CLIENT_ASSERTION_AUDIENCE;
    final MicrosoftClientAssertion assertion = new MicrosoftClientAssertion(audience, credential);
    final TokenRequest tr = new MicrosoftStsTokenRequest();
    tr.setClientAssertionType(assertion.getClientAssertionType());
    tr.setClientAssertion(assertion.getClientAssertion());
    tr.setClientId(CLIENT_ID);
    tr.setScope(SCOPE);
    tr.setGrantType(GRANT_TYPE);
    final OAuth2StrategyParameters options = new OAuth2StrategyParameters();
    final OAuth2Strategy strategy = new MicrosoftStsOAuth2Strategy(new MicrosoftStsOAuth2Configuration(), options);
    try {
        final TokenResult tokenResult = strategy.requestToken(tr);
        assertEquals(true, tokenResult.getSuccess());
    } catch (final ClientException exception) {
        fail("Unexpected exception.");
    }
}
Also used : MicrosoftClientAssertion(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftClientAssertion) TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) ClientCertificateMetadata(com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata) OAuth2StrategyParameters(com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters) MicrosoftStsOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Strategy) KeyStoreConfiguration(com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration) MicrosoftStsOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Strategy) OAuth2Strategy(com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy) CertificateCredential(com.microsoft.identity.common.internal.providers.keys.CertificateCredential) MicrosoftStsTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenRequest) MicrosoftStsOAuth2Configuration(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Configuration) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) MicrosoftStsTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenRequest) ClientException(com.microsoft.identity.common.exception.ClientException) Test(org.junit.Test)

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