Search in sources :

Example 6 with SilentParameters

use of com.microsoft.aad.msal4j.SilentParameters in project azure-kusto-java by Azure.

the class AadAuthenticationHelperTest method checkCloudSettingsNormal.

@Test
@DisplayName("validate cloud settings for the standard cloud")
void checkCloudSettingsNormal() throws URISyntaxException, DataServiceException, DataClientException {
    ConnectionStringBuilder csb = ConnectionStringBuilder.createWithUserPrompt("https://normal.resource.uri", "auth_id", "");
    PublicAppTokenProviderBase aadAuthenticationHelper = (PublicAppTokenProviderBase) TokenProviderFactory.createTokenProvider(csb);
    CloudInfo.manuallyAddToCache("https://normal.resource.uri", CloudInfo.DEFAULT_CLOUD);
    aadAuthenticationHelper.initializeCloudInfo();
    aadAuthenticationHelper.setRequiredMembersBasedOnCloudInfo();
    String authorityUrl = CloudInfo.DEFAULT_PUBLIC_LOGIN_URL + "/auth_id/";
    assertEquals(CloudInfo.DEFAULT_KUSTO_CLIENT_APP_ID, aadAuthenticationHelper.clientApplication.clientId());
    assertEquals(authorityUrl, aadAuthenticationHelper.clientApplication.authority());
    assertEquals(authorityUrl, aadAuthenticationHelper.aadAuthorityUrl);
    HashSet<String> scopes = new HashSet<>(Collections.singletonList(CloudInfo.DEFAULT_KUSTO_SERVICE_RESOURCE_ID + "/.default"));
    assertEquals(scopes, aadAuthenticationHelper.scopes);
    SilentParameters silentParametersNormalUser = aadAuthenticationHelper.getSilentParameters(new HashSet<>(Collections.singletonList(new MockAccount("c0327b6e-814d-4194-8e7f-9fc7a1e5dea9.115d58c9-f699-44e0-8a53-e1861542e510", "", "", null))));
    assertEquals(scopes, silentParametersNormalUser.scopes());
    assertEquals(authorityUrl, silentParametersNormalUser.authorityUrl());
    SilentParameters silentParametersMsaUser = aadAuthenticationHelper.getSilentParameters(new HashSet<>(Collections.singletonList(new MockAccount("c0327b6e-814d-4194-8e7f-9fc7a1e5dea9.9188040d-6c67-4c5b-b112-36a304b66dad", "", "", null))));
    assertEquals(scopes, silentParametersMsaUser.scopes());
    assertEquals(CloudInfo.DEFAULT_FIRST_PARTY_AUTHORITY_URL, silentParametersMsaUser.authorityUrl());
}
Also used : SilentParameters(com.microsoft.aad.msal4j.SilentParameters) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 7 with SilentParameters

use of com.microsoft.aad.msal4j.SilentParameters in project azure-kusto-java by Azure.

the class AadAuthenticationHelperTest method checkCloudSettingsAbnormal.

@Test
@DisplayName("validate cloud settings for non-standard cloud")
void checkCloudSettingsAbnormal() throws URISyntaxException, DataServiceException, DataClientException {
    ConnectionStringBuilder csb = ConnectionStringBuilder.createWithUserPrompt("https://weird.resource.uri", "weird_auth_id", "");
    PublicAppTokenProviderBase aadAuthenticationHelper = (PublicAppTokenProviderBase) TokenProviderFactory.createTokenProvider(csb);
    CloudInfo.manuallyAddToCache("https://weird.resource.uri", new CloudInfo(true, "https://nostandard-login-input", "non_standard_client_id", "", "https://aaaa.kusto.bbbb.com", "first_party_url"));
    aadAuthenticationHelper.initializeCloudInfo();
    aadAuthenticationHelper.setRequiredMembersBasedOnCloudInfo();
    assertEquals("non_standard_client_id", aadAuthenticationHelper.clientApplication.clientId());
    assertEquals("https://nostandard-login-input/weird_auth_id/", aadAuthenticationHelper.clientApplication.authority());
    assertEquals("https://nostandard-login-input/weird_auth_id/", aadAuthenticationHelper.aadAuthorityUrl);
    HashSet<String> scopes = new HashSet<>(Collections.singletonList("https://aaaa.kustomfa.bbbb.com/.default"));
    assertEquals(scopes, aadAuthenticationHelper.scopes);
    SilentParameters silentParametersNormalUser = aadAuthenticationHelper.getSilentParameters(new HashSet<>(Collections.singletonList(new MockAccount("c0327b6e-814d-4194-8e7f-9fc7a1e5dea9.115d58c9-f699-44e0-8a53-e1861542e510", "", "", null))));
    assertEquals(scopes, silentParametersNormalUser.scopes());
    assertEquals("https://nostandard-login-input/weird_auth_id/", silentParametersNormalUser.authorityUrl());
    SilentParameters silentParametersMsaUser = aadAuthenticationHelper.getSilentParameters(new HashSet<>(Collections.singletonList(new MockAccount("c0327b6e-814d-4194-8e7f-9fc7a1e5dea9.9188040d-6c67-4c5b-b112-36a304b66dad", "", "", null))));
    assertEquals(scopes, silentParametersMsaUser.scopes());
    assertEquals("first_party_url", silentParametersMsaUser.authorityUrl());
}
Also used : SilentParameters(com.microsoft.aad.msal4j.SilentParameters) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 8 with SilentParameters

use of com.microsoft.aad.msal4j.SilentParameters in project microsoft-authentication-library-for-java by AzureAD.

the class IntegratedWindowsAuthenticationFlow method acquireTokenIntegratedWindowsAuth.

private static IAuthenticationResult acquireTokenIntegratedWindowsAuth(PublicClientApplication pca, Set<String> scope, IAccount account, String username) throws Exception {
    IAuthenticationResult result;
    try {
        SilentParameters silentParameters = SilentParameters.builder(scope).account(account).build();
        // Try to acquire token silently. This will fail on the first acquireTokenIntegratedWindowsAuth() call
        // because the token cache does not have any data for the user you are trying to acquire a token for
        result = pca.acquireTokenSilently(silentParameters).join();
        System.out.println("==acquireTokenSilently call succeeded");
    } catch (Exception ex) {
        if (ex.getCause() instanceof MsalException) {
            System.out.println("==acquireTokenSilently call failed: " + ex.getCause());
            IntegratedWindowsAuthenticationParameters parameters = IntegratedWindowsAuthenticationParameters.builder(scope, username).build();
            // Try to acquire a token using Integrated Windows Authentication (IWA). You will need to generate a Kerberos ticket.
            // If successful, you should see the token and account information printed out to console
            result = pca.acquireToken(parameters).join();
            System.out.println("==Integrated Windows Authentication flow succeeded");
        } else {
            // Handle other exceptions accordingly
            throw ex;
        }
    }
    return result;
}
Also used : IntegratedWindowsAuthenticationParameters(com.microsoft.aad.msal4j.IntegratedWindowsAuthenticationParameters) SilentParameters(com.microsoft.aad.msal4j.SilentParameters) MsalException(com.microsoft.aad.msal4j.MsalException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) IOException(java.io.IOException) MsalException(com.microsoft.aad.msal4j.MsalException)

Example 9 with SilentParameters

use of com.microsoft.aad.msal4j.SilentParameters in project microsoft-authentication-library-for-java by AzureAD.

the class InteractiveFlow method acquireTokenInteractive.

private static IAuthenticationResult acquireTokenInteractive() throws Exception {
    // Load token cache from file and initialize token cache aspect. The token cache will have
    // dummy data, so the acquireTokenSilently call will fail.
    TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("sample_cache.json");
    PublicClientApplication pca = PublicClientApplication.builder(CLIENT_ID).authority(AUTHORITY).setTokenCacheAccessAspect(tokenCacheAspect).build();
    Set<IAccount> accountsInCache = pca.getAccounts().join();
    // Take first account in the cache. In a production application, you would filter
    // accountsInCache to get the right account for the user authenticating.
    IAccount account = accountsInCache.iterator().next();
    IAuthenticationResult result;
    try {
        SilentParameters silentParameters = SilentParameters.builder(SCOPE, account).build();
        // try to acquire token silently. This call will fail since the token cache
        // does not have any data for the user you are trying to acquire a token for
        result = pca.acquireTokenSilently(silentParameters).join();
    } catch (Exception ex) {
        if (ex.getCause() instanceof MsalException) {
            InteractiveRequestParameters parameters = InteractiveRequestParameters.builder(new URI("http://localhost")).scopes(SCOPE).build();
            // Try to acquire a token interactively with system browser. If successful, you should see
            // the token and account information printed out to console
            result = pca.acquireToken(parameters).join();
        } else {
            // Handle other exceptions accordingly
            throw ex;
        }
    }
    return result;
}
Also used : SilentParameters(com.microsoft.aad.msal4j.SilentParameters) IAccount(com.microsoft.aad.msal4j.IAccount) MsalException(com.microsoft.aad.msal4j.MsalException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) PublicClientApplication(com.microsoft.aad.msal4j.PublicClientApplication) URI(java.net.URI) MsalException(com.microsoft.aad.msal4j.MsalException) InteractiveRequestParameters(com.microsoft.aad.msal4j.InteractiveRequestParameters)

Aggregations

SilentParameters (com.microsoft.aad.msal4j.SilentParameters)9 IAuthenticationResult (com.microsoft.aad.msal4j.IAuthenticationResult)7 MsalException (com.microsoft.aad.msal4j.MsalException)5 IAccount (com.microsoft.aad.msal4j.IAccount)4 InteractiveRequestParameters (com.microsoft.aad.msal4j.InteractiveRequestParameters)4 PublicClientApplication (com.microsoft.aad.msal4j.PublicClientApplication)4 URI (java.net.URI)4 MalformedURLException (java.net.MalformedURLException)3 MsalInteractionRequiredException (com.microsoft.aad.msal4j.MsalInteractionRequiredException)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 DisplayName (org.junit.jupiter.api.DisplayName)2 Test (org.junit.jupiter.api.Test)2 ClientCredentialFactory (com.microsoft.aad.msal4j.ClientCredentialFactory)1 ClientCredentialParameters (com.microsoft.aad.msal4j.ClientCredentialParameters)1 ConfidentialClientApplication (com.microsoft.aad.msal4j.ConfidentialClientApplication)1 IClientCredential (com.microsoft.aad.msal4j.IClientCredential)1