Search in sources :

Example 1 with ClientCredentialParameters

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();
}
Also used : ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters)

Example 2 with ClientCredentialParameters

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);
}
Also used : ClientCredentialFactory(com.microsoft.aad.msal4j.ClientCredentialFactory) IClientCredential(com.microsoft.aad.msal4j.IClientCredential) MalformedURLException(java.net.MalformedURLException) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) ITokenCacheAccessAspect(com.microsoft.aad.msal4j.ITokenCacheAccessAspect) ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) Logger(org.apache.logging.log4j.Logger) ITokenCacheAccessContext(com.microsoft.aad.msal4j.ITokenCacheAccessContext) SilentParameters(com.microsoft.aad.msal4j.SilentParameters) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) Tracing(org.olat.core.logging.Tracing) MsalException(com.microsoft.aad.msal4j.MsalException) ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) SilentParameters(com.microsoft.aad.msal4j.SilentParameters) MsalException(com.microsoft.aad.msal4j.MsalException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters) MalformedURLException(java.net.MalformedURLException) MsalException(com.microsoft.aad.msal4j.MsalException)

Example 3 with ClientCredentialParameters

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;
}
Also used : IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) ClientCredentialFactory(com.microsoft.aad.msal4j.ClientCredentialFactory) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters) WebProxy(microsoft.exchange.webservices.data.core.WebProxy) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ServiceLocalException(microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException) ServiceVersionException(microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceResponseException(microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ExchangeService(microsoft.exchange.webservices.data.core.ExchangeService) WebProxyCredentials(microsoft.exchange.webservices.data.credential.WebProxyCredentials) ImpersonatedUserId(microsoft.exchange.webservices.data.misc.ImpersonatedUserId) ExchangeCredentials(microsoft.exchange.webservices.data.credential.ExchangeCredentials) WebCredentials(microsoft.exchange.webservices.data.credential.WebCredentials)

Example 4 with ClientCredentialParameters

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();
}
Also used : ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) IClientCredential(com.microsoft.aad.msal4j.IClientCredential) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters)

Example 5 with ClientCredentialParameters

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();
}
Also used : MalformedURLException(java.net.MalformedURLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IClientCredential(com.microsoft.aad.msal4j.IClientCredential) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters)

Aggregations

ClientCredentialParameters (com.microsoft.aad.msal4j.ClientCredentialParameters)5 ConfidentialClientApplication (com.microsoft.aad.msal4j.ConfidentialClientApplication)3 IClientCredential (com.microsoft.aad.msal4j.IClientCredential)3 MalformedURLException (java.net.MalformedURLException)3 ClientCredentialFactory (com.microsoft.aad.msal4j.ClientCredentialFactory)2 IAuthenticationResult (com.microsoft.aad.msal4j.IAuthenticationResult)2 ITokenCacheAccessAspect (com.microsoft.aad.msal4j.ITokenCacheAccessAspect)1 ITokenCacheAccessContext (com.microsoft.aad.msal4j.ITokenCacheAccessContext)1 MsalException (com.microsoft.aad.msal4j.MsalException)1 SilentParameters (com.microsoft.aad.msal4j.SilentParameters)1 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExchangeService (microsoft.exchange.webservices.data.core.ExchangeService)1 WebProxy (microsoft.exchange.webservices.data.core.WebProxy)1 ServiceLocalException (microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException)1