Search in sources :

Example 1 with ConfidentialClientApplication

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

Example 2 with ConfidentialClientApplication

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();
}
Also used : ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) MalformedURLException(java.net.MalformedURLException) MessageFormat(java.text.MessageFormat) IClientCredential(com.microsoft.aad.msal4j.IClientCredential)

Example 3 with ConfidentialClientApplication

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();
}
Also used : ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) MalformedURLException(java.net.MalformedURLException) MessageFormat(java.text.MessageFormat) IClientCredential(com.microsoft.aad.msal4j.IClientCredential)

Example 4 with ConfidentialClientApplication

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();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) IClientCredential(com.microsoft.aad.msal4j.IClientCredential) ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 5 with ConfidentialClientApplication

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);
}
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)

Aggregations

ConfidentialClientApplication (com.microsoft.aad.msal4j.ConfidentialClientApplication)7 IClientCredential (com.microsoft.aad.msal4j.IClientCredential)6 MalformedURLException (java.net.MalformedURLException)5 ClientCredentialParameters (com.microsoft.aad.msal4j.ClientCredentialParameters)3 IAuthenticationResult (com.microsoft.aad.msal4j.IAuthenticationResult)3 MessageFormat (java.text.MessageFormat)2 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 ClientCredentialFactory (com.microsoft.aad.msal4j.ClientCredentialFactory)1 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 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Logger (org.apache.logging.log4j.Logger)1 Tracing (org.olat.core.logging.Tracing)1