use of com.microsoft.aad.msal4j.IAccount in project mssql-jdbc by microsoft.
the class SQLServerMSAL4JUtils method getSqlFedAuthTokenInteractive.
static SqlFedAuthToken getSqlFedAuthTokenInteractive(SqlFedAuthInfo fedAuthInfo, String user, String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
PublicClientApplication pca = PublicClientApplication.builder(ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID).executorService(executorService).setTokenCacheAccessAspect(PersistentTokenCacheAccessAspect.getInstance()).authority(fedAuthInfo.stsurl).logPii((logger.isLoggable(Level.FINE))).build();
CompletableFuture<IAuthenticationResult> future = null;
IAuthenticationResult authenticationResult = null;
// try to acquire token silently if user account found in cache
try {
Set<IAccount> accountsInCache = pca.getAccounts().join();
if (null != accountsInCache && !accountsInCache.isEmpty() && null != user && !user.isEmpty()) {
IAccount account = getAccountByUsername(accountsInCache, user);
if (null != account) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(logger.toString() + "Silent authentication for user:" + user);
}
SilentParameters silentParameters = SilentParameters.builder(Collections.singleton(fedAuthInfo.spn + SLASH_DEFAULT), account).build();
future = pca.acquireTokenSilently(silentParameters);
}
}
} catch (MsalInteractionRequiredException e) {
// not an error, need to get token interactively
}
if (null != future) {
authenticationResult = future.get();
} else {
// acquire token interactively with system browser
if (logger.isLoggable(Level.FINE)) {
logger.fine(logger.toString() + "Interactive authentication");
}
InteractiveRequestParameters parameters = InteractiveRequestParameters.builder(new URI(REDIRECTURI)).systemBrowserOptions(SystemBrowserOptions.builder().htmlMessageSuccess(SQLServerResource.getResource("R_MSALAuthComplete")).build()).loginHint(user).scopes(Collections.singleton(fedAuthInfo.spn + SLASH_DEFAULT)).build();
future = pca.acquireToken(parameters);
authenticationResult = future.get();
}
return new SqlFedAuthToken(authenticationResult.accessToken(), authenticationResult.expiresOnDate());
} catch (MalformedURLException | InterruptedException | URISyntaxException e) {
throw new SQLServerException(e.getMessage(), e);
} catch (ExecutionException e) {
throw getCorrectedException(e, user, authenticationString);
} finally {
executorService.shutdown();
}
}
use of com.microsoft.aad.msal4j.IAccount in project microsoft-authentication-library-for-java by AzureAD.
the class InteractiveFlowB2C method acquireTokenInteractiveB2C.
private static IAuthenticationResult acquireTokenInteractiveB2C() 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).b2cAuthority(AUTHORITY).setTokenCacheAccessAspect(tokenCacheAspect).build();
Set<IAccount> accountsInCache = pca.getAccounts().join();
// Use 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) {
// For B2C, you have to specify a port for the redirect URL
InteractiveRequestParameters parameters = InteractiveRequestParameters.builder(new URI("http://localhost:8080")).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;
}
use of com.microsoft.aad.msal4j.IAccount in project microsoft-authentication-library-for-java by AzureAD.
the class UsernamePasswordFlow method main.
public static void main(String[] args) throws Exception {
setUpSampleData();
PublicClientApplication pca = PublicClientApplication.builder(clientId).authority(authority).build();
// Get list of accounts from the application's token cache, and search them for the configured username
// getAccounts() will be empty on this first call, as accounts are added to the cache when acquiring a token
Set<IAccount> accountsInCache = pca.getAccounts().join();
IAccount account = getAccountByUsername(accountsInCache, username);
// Attempt to acquire token when user's account is not in the application's token cache
IAuthenticationResult result = acquireTokenUsernamePassword(pca, scope, account, username, password);
System.out.println("Account username: " + result.account().username());
System.out.println("Access token: " + result.accessToken());
System.out.println("Id token: " + result.idToken());
System.out.println();
accountsInCache = pca.getAccounts().join();
account = getAccountByUsername(accountsInCache, username);
// Attempt to acquire token again, now that the user's account and a token are in the application's token cache
result = acquireTokenUsernamePassword(pca, scope, account, username, password);
System.out.println("Account username: " + result.account().username());
System.out.println("Access token: " + result.accessToken());
System.out.println("Id token: " + result.idToken());
}
use of com.microsoft.aad.msal4j.IAccount in project microsoft-authentication-library-for-java by AzureAD.
the class IntegratedWindowsAuthenticationFlow method main.
public static void main(String[] args) throws Exception {
setUpSampleData();
PublicClientApplication pca = PublicClientApplication.builder(clientId).authority(authority).build();
Set<IAccount> accountsInCache = pca.getAccounts().join();
IAccount account = getAccountByUsername(accountsInCache, username);
// Attempt to acquire token when user's account is not in the application's token cache
IAuthenticationResult result = acquireTokenIntegratedWindowsAuth(pca, scope, account, username);
System.out.println("Account username: " + result.account().username());
System.out.println("Access token: " + result.accessToken());
System.out.println("Id token: " + result.idToken());
System.out.println();
// Get list of accounts from the application's token cache, and search them for the configured username
// getAccounts() will be empty on this first call, as accounts are added to the cache when acquiring a token
accountsInCache = pca.getAccounts().join();
account = getAccountByUsername(accountsInCache, username);
// Attempt to acquire token again, now that the user's account and a token are in the application's token cache
result = acquireTokenIntegratedWindowsAuth(pca, scope, account, username);
System.out.println("Account username: " + result.account().username());
System.out.println("Access token: " + result.accessToken());
System.out.println("Id token: " + result.idToken());
}
use of com.microsoft.aad.msal4j.IAccount in project mssql-jdbc by Microsoft.
the class SQLServerMSAL4JUtils method getSqlFedAuthTokenInteractive.
static SqlFedAuthToken getSqlFedAuthTokenInteractive(SqlFedAuthInfo fedAuthInfo, String user, String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
PublicClientApplication pca = PublicClientApplication.builder(ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID).executorService(executorService).setTokenCacheAccessAspect(PersistentTokenCacheAccessAspect.getInstance()).authority(fedAuthInfo.stsurl).logPii((logger.isLoggable(Level.FINE))).build();
CompletableFuture<IAuthenticationResult> future = null;
IAuthenticationResult authenticationResult = null;
// try to acquire token silently if user account found in cache
try {
Set<IAccount> accountsInCache = pca.getAccounts().join();
if (null != accountsInCache && !accountsInCache.isEmpty() && null != user && !user.isEmpty()) {
IAccount account = getAccountByUsername(accountsInCache, user);
if (null != account) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(logger.toString() + "Silent authentication for user:" + user);
}
SilentParameters silentParameters = SilentParameters.builder(Collections.singleton(fedAuthInfo.spn + SLASH_DEFAULT), account).build();
future = pca.acquireTokenSilently(silentParameters);
}
}
} catch (MsalInteractionRequiredException e) {
// not an error, need to get token interactively
}
if (null != future) {
authenticationResult = future.get();
} else {
// acquire token interactively with system browser
if (logger.isLoggable(Level.FINE)) {
logger.fine(logger.toString() + "Interactive authentication");
}
InteractiveRequestParameters parameters = InteractiveRequestParameters.builder(new URI(REDIRECTURI)).systemBrowserOptions(SystemBrowserOptions.builder().htmlMessageSuccess(SQLServerResource.getResource("R_MSALAuthComplete")).build()).loginHint(user).scopes(Collections.singleton(fedAuthInfo.spn + SLASH_DEFAULT)).build();
future = pca.acquireToken(parameters);
authenticationResult = future.get();
}
return new SqlFedAuthToken(authenticationResult.accessToken(), authenticationResult.expiresOnDate());
} catch (MalformedURLException | InterruptedException | URISyntaxException e) {
throw new SQLServerException(e.getMessage(), e);
} catch (ExecutionException e) {
throw getCorrectedException(e, user, authenticationString);
} finally {
executorService.shutdown();
}
}
Aggregations