Search in sources :

Example 11 with PublicClientApplication

use of com.microsoft.aad.msal4j.PublicClientApplication in project mssql-jdbc by microsoft.

the class SQLServerMSAL4JUtils method getSqlFedAuthTokenIntegrated.

static SqlFedAuthToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo, String authenticationString) throws SQLServerException {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    try {
        /*
             * principal name does not matter, what matters is the realm name it gets the username in
             * principal_name@realm_name format
             */
        KerberosPrincipal kerberosPrincipal = new KerberosPrincipal("username");
        String user = kerberosPrincipal.getName();
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(logger.toString() + " realm name is:" + kerberosPrincipal.getRealm());
        }
        final PublicClientApplication pca = PublicClientApplication.builder(ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID).executorService(executorService).authority(fedAuthInfo.stsurl).build();
        final CompletableFuture<IAuthenticationResult> future = pca.acquireToken(IntegratedWindowsAuthenticationParameters.builder(Collections.singleton(fedAuthInfo.spn + SLASH_DEFAULT), user).build());
        final IAuthenticationResult authenticationResult = future.get();
        return new SqlFedAuthToken(authenticationResult.accessToken(), authenticationResult.expiresOnDate());
    } catch (InterruptedException | IOException e) {
        throw new SQLServerException(e.getMessage(), e);
    } catch (ExecutionException e) {
        throw getCorrectedException(e, "", authenticationString);
    } finally {
        executorService.shutdown();
    }
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) ExecutorService(java.util.concurrent.ExecutorService) PublicClientApplication(com.microsoft.aad.msal4j.PublicClientApplication) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with PublicClientApplication

use of com.microsoft.aad.msal4j.PublicClientApplication in project microsoft-authentication-library-for-java by AzureAD.

the class InteractiveFlow method acquireTokenInteractive.

private static IAuthenticationResult acquireTokenInteractive() throws Exception {
    // Load token cache from file and initialize token cache aspect. The token cache will have
    // dummy data, so the acquireTokenSilently call will fail.
    TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("sample_cache.json");
    PublicClientApplication pca = PublicClientApplication.builder(CLIENT_ID).authority(AUTHORITY).setTokenCacheAccessAspect(tokenCacheAspect).build();
    Set<IAccount> accountsInCache = pca.getAccounts().join();
    // Take first account in the cache. In a production application, you would filter
    // accountsInCache to get the right account for the user authenticating.
    IAccount account = accountsInCache.iterator().next();
    IAuthenticationResult result;
    try {
        SilentParameters silentParameters = SilentParameters.builder(SCOPE, account).build();
        // try to acquire token silently. This call will fail since the token cache
        // does not have any data for the user you are trying to acquire a token for
        result = pca.acquireTokenSilently(silentParameters).join();
    } catch (Exception ex) {
        if (ex.getCause() instanceof MsalException) {
            InteractiveRequestParameters parameters = InteractiveRequestParameters.builder(new URI("http://localhost")).scopes(SCOPE).build();
            // Try to acquire a token interactively with system browser. If successful, you should see
            // the token and account information printed out to console
            result = pca.acquireToken(parameters).join();
        } else {
            // Handle other exceptions accordingly
            throw ex;
        }
    }
    return result;
}
Also used : SilentParameters(com.microsoft.aad.msal4j.SilentParameters) IAccount(com.microsoft.aad.msal4j.IAccount) MsalException(com.microsoft.aad.msal4j.MsalException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) PublicClientApplication(com.microsoft.aad.msal4j.PublicClientApplication) URI(java.net.URI) MsalException(com.microsoft.aad.msal4j.MsalException) InteractiveRequestParameters(com.microsoft.aad.msal4j.InteractiveRequestParameters)

Aggregations

IAuthenticationResult (com.microsoft.aad.msal4j.IAuthenticationResult)12 PublicClientApplication (com.microsoft.aad.msal4j.PublicClientApplication)12 IAccount (com.microsoft.aad.msal4j.IAccount)6 ExecutionException (java.util.concurrent.ExecutionException)6 ExecutorService (java.util.concurrent.ExecutorService)6 InteractiveRequestParameters (com.microsoft.aad.msal4j.InteractiveRequestParameters)4 SilentParameters (com.microsoft.aad.msal4j.SilentParameters)4 MalformedURLException (java.net.MalformedURLException)4 URI (java.net.URI)4 MsalException (com.microsoft.aad.msal4j.MsalException)2 MsalInteractionRequiredException (com.microsoft.aad.msal4j.MsalInteractionRequiredException)2 SQLServerException (com.microsoft.sqlserver.jdbc.SQLServerException)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 KerberosPrincipal (javax.security.auth.kerberos.KerberosPrincipal)2