use of com.microsoft.aad.msal4j.ConfidentialClientApplication in project mssql-jdbc by Microsoft.
the class SQLServerMSAL4JUtils method getSqlFedAuthTokenPrincipal.
static SqlFedAuthToken getSqlFedAuthTokenPrincipal(SqlFedAuthInfo fedAuthInfo, String aadPrincipalID, String aadPrincipalSecret, String authenticationString) throws SQLServerException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
String defaultScopeSuffix = SLASH_DEFAULT;
String scope = fedAuthInfo.spn.endsWith(defaultScopeSuffix) ? fedAuthInfo.spn : fedAuthInfo.spn + defaultScopeSuffix;
Set<String> scopes = new HashSet<>();
scopes.add(scope);
IClientCredential credential = ClientCredentialFactory.createFromSecret(aadPrincipalSecret);
ConfidentialClientApplication clientApplication = ConfidentialClientApplication.builder(aadPrincipalID, credential).executorService(executorService).authority(fedAuthInfo.stsurl).build();
final CompletableFuture<IAuthenticationResult> future = clientApplication.acquireToken(ClientCredentialParameters.builder(scopes).build());
final IAuthenticationResult authenticationResult = future.get();
return new SqlFedAuthToken(authenticationResult.accessToken(), authenticationResult.expiresOnDate());
} catch (MalformedURLException | InterruptedException e) {
throw new SQLServerException(e.getMessage(), e);
} catch (ExecutionException e) {
throw getCorrectedException(e, aadPrincipalID, authenticationString);
} finally {
executorService.shutdown();
}
}
use of com.microsoft.aad.msal4j.ConfidentialClientApplication in project microsoft-authentication-library-for-java by AzureAD.
the class ClientCredentialGrant method acquireToken.
private static IAuthenticationResult acquireToken() throws Exception {
// This is the secret that is created in the Azure portal when registering the application
IClientCredential credential = ClientCredentialFactory.createFromSecret(CLIENT_SECRET);
ConfidentialClientApplication cca = ConfidentialClientApplication.builder(CLIENT_ID, credential).authority(AUTHORITY).build();
// Client credential requests will by default try to look for a valid token in the
// in-memory token cache. If found, it will return this token. If a token is not found, or the
// token is not valid, it will fall back to acquiring a token from the AAD service. Although
// not recommended unless there is a reason for doing so, you can skip the cache lookup
// by using .skipCache(true) in ClientCredentialParameters.
ClientCredentialParameters parameters = ClientCredentialParameters.builder(SCOPE).build();
return cca.acquireToken(parameters).join();
}
Aggregations