Search in sources :

Example 1 with ClientCertificateMetadata

use of com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata 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 2 with ClientCertificateMetadata

use of com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata 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)

Example 3 with ClientCertificateMetadata

use of com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata in project microsoft-authentication-library-common-for-android by AzureAD.

the class KeyVaultAuthHelper method createTokenRequestWithClientAssertion.

private TokenRequest createTokenRequestWithClientAssertion() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, IOException {
    CertificateCredential certificateCredential = new CertificateCredential.CertificateCredentialBuilder(CLIENT_ID).clientCertificateMetadata(new ClientCertificateMetadata(CERTIFICATE_ALIAS, null)).keyStoreConfiguration(new KeyStoreConfiguration(KEYSTORE_TYPE, KEYSTORE_PROVIDER, null)).build();
    MicrosoftClientAssertion assertion = new MicrosoftClientAssertion(MSSTS_CLIENT_ASSERTION_AUDIENCE, certificateCredential);
    TokenRequest tr = new MicrosoftStsTokenRequest();
    tr.setClientAssertionType(assertion.getClientAssertionType());
    tr.setClientAssertion(assertion.getClientAssertion());
    tr.setClientId(CLIENT_ID);
    tr.setScope(SCOPE);
    return tr;
}
Also used : CertificateCredential(com.microsoft.identity.common.internal.providers.keys.CertificateCredential) MicrosoftClientAssertion(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftClientAssertion) MicrosoftStsTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenRequest) ClientCertificateMetadata(com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) MicrosoftStsTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenRequest) KeyStoreConfiguration(com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration)

Example 4 with ClientCertificateMetadata

use of com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata in project microsoft-authentication-library-common-for-android by AzureAD.

the class CertificateCredentialBuilder method test_CertificateBuilder_LookupCertificateWithPassword_IsCorrect.

@Test
public void test_CertificateBuilder_LookupCertificateWithPassword_IsCorrect() throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, NoSuchProviderException, IOException, CertificateException {
    PowerMockito.mockStatic(KeyStore.class);
    when(KeyStore.getInstance(WINDOWS_MY_KEYSTORE, WINDOWS_KEYSTORE_PROVIDER)).thenReturn(keyStoreMock);
    CertificateCredential cred = new CertificateCredential.CertificateCredentialBuilder(CLIENT_ID).keyStoreConfiguration(new KeyStoreConfiguration(WINDOWS_MY_KEYSTORE, WINDOWS_KEYSTORE_PROVIDER, null)).clientCertificateMetadata(new ClientCertificateMetadata(CERTIFICATE_ALIAS, CERTIFICATE_PASSWORD)).build();
    assertEquals(privateKey, cred.getPrivateKey());
    assertEquals(certificate, cred.getPublicCertificate());
    assertEquals(CLIENT_ID, cred.getClientId());
}
Also used : CertificateCredential(com.microsoft.identity.common.internal.providers.keys.CertificateCredential) ClientCertificateMetadata(com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata) KeyStoreConfiguration(com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with ClientCertificateMetadata

use of com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata in project microsoft-authentication-library-common-for-android by AzureAD.

the class CertificateCredentialBuilder method test_CertificateBuilder_LookupCertificate_IsCorrect.

@Test
public void test_CertificateBuilder_LookupCertificate_IsCorrect() throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, NoSuchProviderException, IOException, CertificateException {
    PowerMockito.mockStatic(KeyStore.class);
    when(KeyStore.getInstance(WINDOWS_MY_KEYSTORE, WINDOWS_KEYSTORE_PROVIDER)).thenReturn(keyStoreMock);
    CertificateCredential cred = new CertificateCredential.CertificateCredentialBuilder(CLIENT_ID).keyStoreConfiguration(new KeyStoreConfiguration(WINDOWS_MY_KEYSTORE, WINDOWS_KEYSTORE_PROVIDER, null)).clientCertificateMetadata(new ClientCertificateMetadata(CERTIFICATE_ALIAS, null)).build();
    assertEquals(privateKey, cred.getPrivateKey());
    assertEquals(certificate, cred.getPublicCertificate());
    assertEquals(CLIENT_ID, cred.getClientId());
}
Also used : CertificateCredential(com.microsoft.identity.common.internal.providers.keys.CertificateCredential) ClientCertificateMetadata(com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata) KeyStoreConfiguration(com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CertificateCredential (com.microsoft.identity.common.internal.providers.keys.CertificateCredential)5 ClientCertificateMetadata (com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata)5 KeyStoreConfiguration (com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration)5 Test (org.junit.Test)4 MicrosoftClientAssertion (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftClientAssertion)3 ClientException (com.microsoft.identity.common.exception.ClientException)2 MicrosoftStsTokenRequest (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsTokenRequest)2 OAuth2Strategy (com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy)2 OAuth2StrategyParameters (com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters)2 TokenRequest (com.microsoft.identity.common.internal.providers.oauth2.TokenRequest)2 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 AzureActiveDirectoryOAuth2Configuration (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Configuration)1 AzureActiveDirectoryOAuth2Strategy (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy)1 AzureActiveDirectoryTokenRequest (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryTokenRequest)1 MicrosoftStsOAuth2Configuration (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Configuration)1 MicrosoftStsOAuth2Strategy (com.microsoft.identity.common.internal.providers.microsoft.microsoftsts.MicrosoftStsOAuth2Strategy)1