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