use of com.microsoft.aad.msal4j.ClientCredentialParameters 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.ClientCredentialParameters 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);
}
use of com.microsoft.aad.msal4j.ClientCredentialParameters in project iaf by ibissource.
the class ExchangeFileSystem method createConnection.
@Override
protected ExchangeService createConnection() throws FileSystemException {
ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
if (client != null) {
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(Collections.singleton(SCOPE)).build();
CompletableFuture<IAuthenticationResult> future = client.acquireToken(clientCredentialParam);
try {
String token = future.get().accessToken();
// use OAuth Bearer token authentication
exchangeService.getHttpHeaders().put("Authorization", "Bearer " + token);
} catch (Exception e) {
throw new FileSystemException("Could not generate access token!", e);
}
exchangeService.setImpersonatedUserId(new ImpersonatedUserId(ConnectingIdType.SmtpAddress, getMailAddress()));
exchangeService.getHttpHeaders().put("X-AnchorMailbox", getMailAddress());
} else {
CredentialFactory cf = getCredentials();
// use deprecated Basic Authentication. Support will end 2021-Q3!
log.warn("Using deprecated Basic Authentication method for authentication to Exchange Web Services");
ExchangeCredentials credentials = new WebCredentials(cf.getUsername(), cf.getPassword());
exchangeService.setCredentials(credentials);
}
if (StringUtils.isNotEmpty(getProxyHost()) && (StringUtils.isNotEmpty(getProxyAuthAlias()) || StringUtils.isNotEmpty(getProxyUsername()) || StringUtils.isNotEmpty(getProxyPassword()))) {
CredentialFactory proxyCf = new CredentialFactory(getProxyAuthAlias(), getProxyUsername(), getProxyPassword());
WebProxyCredentials webProxyCredentials = new WebProxyCredentials(proxyCf.getUsername(), proxyCf.getPassword(), getProxyDomain());
WebProxy webProxy = new WebProxy(getProxyHost(), getProxyPort(), webProxyCredentials);
exchangeService.setWebProxy(webProxy);
}
RedirectionUrlCallback redirectionUrlCallback = new RedirectionUrlCallback() {
@Override
public boolean autodiscoverRedirectionUrlValidationCallback(String redirectionUrl) {
if (isValidateAllRedirectUrls()) {
log.debug("validated redirection url [" + redirectionUrl + "]");
return true;
}
log.debug("did not validate redirection url [" + redirectionUrl + "]");
return super.autodiscoverRedirectionUrlValidationCallback(redirectionUrl);
}
};
if (StringUtils.isEmpty(getUrl())) {
log.debug("performing autodiscovery for [" + getMailAddress() + "]");
try {
exchangeService.autodiscoverUrl(getMailAddress(), redirectionUrlCallback);
// TODO call setUrl() here to avoid repeated autodiscovery
} catch (Exception e) {
throw new FileSystemException("cannot autodiscover for [" + getMailAddress() + "]", e);
}
} else {
try {
exchangeService.setUrl(new URI(getUrl()));
} catch (URISyntaxException e) {
throw new FileSystemException("cannot set URL [" + getUrl() + "]", e);
}
}
log.debug("using url [" + exchangeService.getUrl() + "]");
return exchangeService;
}
use of com.microsoft.aad.msal4j.ClientCredentialParameters 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();
}
use of com.microsoft.aad.msal4j.ClientCredentialParameters in project OpenUnison by TremoloSecurity.
the class AttributeChange method loadCredentials.
private void loadCredentials() throws ProvisioningException {
IClientCredential c;
try {
app = ConfidentialClientApplication.builder(clientId, ClientCredentialFactory.createFromSecret(this.clientSecret)).authority(this.authority).build();
} catch (MalformedURLException e) {
throw new ProvisioningException("Could not obtain confidential client application", e);
}
ClientCredentialParameters parameters = ClientCredentialParameters.builder(this.clientScopes).build();
azureAuthToken = app.acquireToken(parameters).join();
this.oauth2Token = azureAuthToken.accessToken();
}
Aggregations