use of com.microsoft.aad.msal4j.ConfidentialClientApplication in project ambry by linkedin.
the class ADAuthBasedStorageClient method getAccessTokenByClientCredentialGrant.
/**
* Create {@link IAuthenticationResult} using the app details.
* @param azureCloudConfig {@link AzureCloudConfig} object.
* @return {@link IAuthenticationResult} containing the access token.
* @throws MalformedURLException
* @throws InterruptedException
* @throws ExecutionException
*/
private IAuthenticationResult getAccessTokenByClientCredentialGrant(AzureCloudConfig azureCloudConfig) throws MalformedURLException, InterruptedException, ExecutionException {
// If a proxy is required, properties must either be set at the jvm level,
// or ClientSecretCredentialStorageClient should be used
ConfidentialClientApplication app = ConfidentialClientApplication.builder(azureCloudConfig.azureStorageClientId, ClientCredentialFactory.createFromSecret(azureCloudConfig.azureStorageSecret)).authority(azureCloudConfig.azureStorageAuthority).build();
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(Collections.singleton(azureCloudConfig.azureStorageScope)).build();
return app.acquireToken(clientCredentialParam).get();
}
use of com.microsoft.aad.msal4j.ConfidentialClientApplication in project mssql-jdbc by Microsoft.
the class KeyVaultTokenCredential method getConfidentialClientApplication.
/**
* Creates an instance of {@link ConfidentialClientApplication} using the provided client id and secret.
*
* @return An instance of {@link ConfidentialClientApplication}.
*/
private ConfidentialClientApplication getConfidentialClientApplication() {
if (null == clientId) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString(NULL_VALUE));
Object[] msgArgs1 = { "Client ID" };
throw new IllegalArgumentException(form.format(msgArgs1), null);
}
if (null == authorization) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString(NULL_VALUE));
Object[] msgArgs1 = { "Authorization" };
throw new IllegalArgumentException(form.format(msgArgs1), null);
}
if (null == clientSecret) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString(NULL_VALUE));
Object[] msgArgs1 = { "Client Secret" };
throw new IllegalArgumentException(form.format(msgArgs1), null);
}
// Create the credential using the MSAL factory method.
IClientCredential credential;
credential = ClientCredentialFactory.createFromSecret(clientSecret);
ConfidentialClientApplication.Builder applicationBuilder = ConfidentialClientApplication.builder(clientId, credential);
try {
applicationBuilder = applicationBuilder.authority(authorization);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return applicationBuilder.build();
}
use of com.microsoft.aad.msal4j.ConfidentialClientApplication in project mssql-jdbc by microsoft.
the class KeyVaultTokenCredential method getConfidentialClientApplication.
/**
* Creates an instance of {@link ConfidentialClientApplication} using the provided client id and secret.
*
* @return An instance of {@link ConfidentialClientApplication}.
*/
private ConfidentialClientApplication getConfidentialClientApplication() {
if (null == clientId) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString(NULL_VALUE));
Object[] msgArgs1 = { "Client ID" };
throw new IllegalArgumentException(form.format(msgArgs1), null);
}
if (null == authorization) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString(NULL_VALUE));
Object[] msgArgs1 = { "Authorization" };
throw new IllegalArgumentException(form.format(msgArgs1), null);
}
if (null == clientSecret) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString(NULL_VALUE));
Object[] msgArgs1 = { "Client Secret" };
throw new IllegalArgumentException(form.format(msgArgs1), null);
}
// Create the credential using the MSAL factory method.
IClientCredential credential;
credential = ClientCredentialFactory.createFromSecret(clientSecret);
ConfidentialClientApplication.Builder applicationBuilder = ConfidentialClientApplication.builder(clientId, credential);
try {
applicationBuilder = applicationBuilder.authority(authorization);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return applicationBuilder.build();
}
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 OpenOLAT by OpenOLAT.
the class MicrosoftGraphAccessTokenManager method connect.
private CompletableFuture<String> connect(String id, String secret, String tenant) {
ConfidentialClientApplication cca = createClientApplication(id, secret, tenant);
CompletableFuture<IAuthenticationResult> result = null;
if (cca != null) {
try {
if (cache.isEmpty()) {
ClientCredentialParameters parameters = ClientCredentialParameters.builder(SCOPES).build();
result = cca.acquireToken(parameters);
} else {
SilentParameters silentParameters = SilentParameters.builder(SCOPES).build();
// try to acquire token silently. This call will fail since the token cache does not
// have a token for the application you are requesting an access token for
result = cca.acquireTokenSilently(silentParameters);
}
} catch (Exception ex) {
if (ex.getCause() instanceof MsalException) {
ClientCredentialParameters parameters = ClientCredentialParameters.builder(SCOPES).build();
result = cca.acquireToken(parameters);
} else {
log.error("", ex);
}
}
}
if (result != null) {
return result.handleAsync((res, ex) -> {
if (ex != null && (ex instanceof MsalException || ex.getCause() instanceof MsalException)) {
ClientCredentialParameters parameters = ClientCredentialParameters.builder(SCOPES).build();
return cca.acquireToken(parameters).join();
}
return res;
}).thenApply(IAuthenticationResult::accessToken);
}
return CompletableFuture.completedFuture((String) null);
}
Aggregations