Search in sources :

Example 1 with AzureActiveDirectoryOAuth2Strategy

use of com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy 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 AzureActiveDirectoryOAuth2Strategy

use of com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy in project azure-activedirectory-library-for-android by AzureAD.

the class TokenCacheAccessor method updateTokenCacheUsingCommonCache.

void updateTokenCacheUsingCommonCache(final AuthenticationRequest request, final AuthenticationResult result) throws MalformedURLException {
    AzureActiveDirectory ad = new AzureActiveDirectory();
    AzureActiveDirectoryTokenResponse tokenResponse = CoreAdapter.asAadTokenResponse(result);
    AzureActiveDirectoryOAuth2Configuration config = new AzureActiveDirectoryOAuth2Configuration();
    config.setAuthorityHostValidationEnabled(this.isValidateAuthorityHost());
    if (null != mAuthority) {
        config.setAuthorityUrl(new URL(mAuthority));
    }
    AzureActiveDirectoryOAuth2Strategy strategy = ad.createOAuth2Strategy(config);
    AzureActiveDirectoryAuthorizationRequest.Builder aadAuthRequestBuilder = new AzureActiveDirectoryAuthorizationRequest.Builder();
    aadAuthRequestBuilder.setClientId(request.getClientId()).setResource(request.getResource()).setScope(request.getResource()).setRedirectUri(request.getRedirectUri()).setLoginHint(request.getLoginHint()).setCorrelationId(request.getCorrelationId());
    if (null != mAuthority) {
        aadAuthRequestBuilder.setAuthority(new URL(mAuthority));
    }
    mCommonCache.save(strategy, aadAuthRequestBuilder.build(), tokenResponse);
}
Also used : AzureActiveDirectoryOAuth2Strategy(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy) AzureActiveDirectoryTokenResponse(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryTokenResponse) AzureActiveDirectoryOAuth2Configuration(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Configuration) AzureActiveDirectoryAuthorizationRequest(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAuthorizationRequest) AzureActiveDirectory(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectory) URL(java.net.URL)

Aggregations

AzureActiveDirectoryOAuth2Configuration (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Configuration)2 AzureActiveDirectoryOAuth2Strategy (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryOAuth2Strategy)2 ClientException (com.microsoft.identity.common.exception.ClientException)1 CertificateCredential (com.microsoft.identity.common.internal.providers.keys.CertificateCredential)1 ClientCertificateMetadata (com.microsoft.identity.common.internal.providers.keys.ClientCertificateMetadata)1 KeyStoreConfiguration (com.microsoft.identity.common.internal.providers.keys.KeyStoreConfiguration)1 MicrosoftClientAssertion (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftClientAssertion)1 AzureActiveDirectory (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectory)1 AzureActiveDirectoryAuthorizationRequest (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAuthorizationRequest)1 AzureActiveDirectoryTokenRequest (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryTokenRequest)1 AzureActiveDirectoryTokenResponse (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryTokenResponse)1 OAuth2Strategy (com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy)1 OAuth2StrategyParameters (com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters)1 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)1 URL (java.net.URL)1 Test (org.junit.Test)1