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;
}
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);
}
}
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);
}
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.");
}
}
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.");
}
}
Aggregations