use of com.microsoft.aad.msal4j.MsalInteractionRequiredException 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.MsalInteractionRequiredException 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