Search in sources :

Example 6 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 7 with ConfidentialClientApplication

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

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