Search in sources :

Example 1 with IClientCredential

use of com.microsoft.aad.msal4j.IClientCredential 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 2 with IClientCredential

use of com.microsoft.aad.msal4j.IClientCredential 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 IClientCredential

use of com.microsoft.aad.msal4j.IClientCredential 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 4 with IClientCredential

use of com.microsoft.aad.msal4j.IClientCredential 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 IClientCredential

use of com.microsoft.aad.msal4j.IClientCredential 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)

Aggregations

IClientCredential (com.microsoft.aad.msal4j.IClientCredential)6 ConfidentialClientApplication (com.microsoft.aad.msal4j.ConfidentialClientApplication)5 MalformedURLException (java.net.MalformedURLException)5 ClientCredentialParameters (com.microsoft.aad.msal4j.ClientCredentialParameters)2 IAuthenticationResult (com.microsoft.aad.msal4j.IAuthenticationResult)2 MessageFormat (java.text.MessageFormat)2 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1