Search in sources :

Example 6 with PublicClientApplication

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

the class UsernamePasswordFlow method main.

public static void main(String[] args) throws Exception {
    setUpSampleData();
    PublicClientApplication pca = PublicClientApplication.builder(clientId).authority(authority).build();
    // Get list of accounts from the application's token cache, and search them for the configured username
    // getAccounts() will be empty on this first call, as accounts are added to the cache when acquiring a token
    Set<IAccount> accountsInCache = pca.getAccounts().join();
    IAccount account = getAccountByUsername(accountsInCache, username);
    // Attempt to acquire token when user's account is not in the application's token cache
    IAuthenticationResult result = acquireTokenUsernamePassword(pca, scope, account, username, password);
    System.out.println("Account username: " + result.account().username());
    System.out.println("Access token:     " + result.accessToken());
    System.out.println("Id token:         " + result.idToken());
    System.out.println();
    accountsInCache = pca.getAccounts().join();
    account = getAccountByUsername(accountsInCache, username);
    // Attempt to acquire token again, now that the user's account and a token are in the application's token cache
    result = acquireTokenUsernamePassword(pca, scope, account, username, password);
    System.out.println("Account username: " + result.account().username());
    System.out.println("Access token:     " + result.accessToken());
    System.out.println("Id token:         " + result.idToken());
}
Also used : IAccount(com.microsoft.aad.msal4j.IAccount) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) PublicClientApplication(com.microsoft.aad.msal4j.PublicClientApplication)

Example 7 with PublicClientApplication

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

the class IntegratedWindowsAuthenticationFlow method main.

public static void main(String[] args) throws Exception {
    setUpSampleData();
    PublicClientApplication pca = PublicClientApplication.builder(clientId).authority(authority).build();
    Set<IAccount> accountsInCache = pca.getAccounts().join();
    IAccount account = getAccountByUsername(accountsInCache, username);
    // Attempt to acquire token when user's account is not in the application's token cache
    IAuthenticationResult result = acquireTokenIntegratedWindowsAuth(pca, scope, account, username);
    System.out.println("Account username: " + result.account().username());
    System.out.println("Access token:     " + result.accessToken());
    System.out.println("Id token:         " + result.idToken());
    System.out.println();
    // Get list of accounts from the application's token cache, and search them for the configured username
    // getAccounts() will be empty on this first call, as accounts are added to the cache when acquiring a token
    accountsInCache = pca.getAccounts().join();
    account = getAccountByUsername(accountsInCache, username);
    // Attempt to acquire token again, now that the user's account and a token are in the application's token cache
    result = acquireTokenIntegratedWindowsAuth(pca, scope, account, username);
    System.out.println("Account username: " + result.account().username());
    System.out.println("Access token:     " + result.accessToken());
    System.out.println("Id token:         " + result.idToken());
}
Also used : IAccount(com.microsoft.aad.msal4j.IAccount) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) PublicClientApplication(com.microsoft.aad.msal4j.PublicClientApplication)

Example 8 with PublicClientApplication

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

the class FedauthCommon method getFedauthInfo.

/**
 * Get Fedauth info
 */
static void getFedauthInfo() {
    try {
        final PublicClientApplication clientApplication = PublicClientApplication.builder(fedauthClientId).executorService(Executors.newFixedThreadPool(1)).authority(stsurl).build();
        final CompletableFuture<IAuthenticationResult> future = clientApplication.acquireToken(UserNamePasswordParameters.builder(Collections.singleton(spn + "/.default"), azureUserName, azurePassword.toCharArray()).build());
        final IAuthenticationResult authenticationResult = future.get();
        secondsBeforeExpiration = TimeUnit.MILLISECONDS.toSeconds(authenticationResult.expiresOnDate().getTime() - new Date().getTime());
        accessToken = authenticationResult.accessToken();
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) PublicClientApplication(com.microsoft.aad.msal4j.PublicClientApplication) Date(java.util.Date) SQLException(java.sql.SQLException) SQLServerException(com.microsoft.sqlserver.jdbc.SQLServerException)

Example 9 with PublicClientApplication

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

the class SQLServerMSAL4JUtils method getSqlFedAuthTokenInteractive.

static SqlFedAuthToken getSqlFedAuthTokenInteractive(SqlFedAuthInfo fedAuthInfo, String user, String authenticationString) throws SQLServerException {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    try {
        PublicClientApplication pca = PublicClientApplication.builder(ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID).executorService(executorService).setTokenCacheAccessAspect(PersistentTokenCacheAccessAspect.getInstance()).authority(fedAuthInfo.stsurl).logPii((logger.isLoggable(Level.FINE))).build();
        CompletableFuture<IAuthenticationResult> future = null;
        IAuthenticationResult authenticationResult = null;
        // try to acquire token silently if user account found in cache
        try {
            Set<IAccount> accountsInCache = pca.getAccounts().join();
            if (null != accountsInCache && !accountsInCache.isEmpty() && null != user && !user.isEmpty()) {
                IAccount account = getAccountByUsername(accountsInCache, user);
                if (null != account) {
                    if (logger.isLoggable(Level.FINE)) {
                        logger.fine(logger.toString() + "Silent authentication for user:" + user);
                    }
                    SilentParameters silentParameters = SilentParameters.builder(Collections.singleton(fedAuthInfo.spn + SLASH_DEFAULT), account).build();
                    future = pca.acquireTokenSilently(silentParameters);
                }
            }
        } catch (MsalInteractionRequiredException e) {
        // not an error, need to get token interactively
        }
        if (null != future) {
            authenticationResult = future.get();
        } else {
            // acquire token interactively with system browser
            if (logger.isLoggable(Level.FINE)) {
                logger.fine(logger.toString() + "Interactive authentication");
            }
            InteractiveRequestParameters parameters = InteractiveRequestParameters.builder(new URI(REDIRECTURI)).systemBrowserOptions(SystemBrowserOptions.builder().htmlMessageSuccess(SQLServerResource.getResource("R_MSALAuthComplete")).build()).loginHint(user).scopes(Collections.singleton(fedAuthInfo.spn + SLASH_DEFAULT)).build();
            future = pca.acquireToken(parameters);
            authenticationResult = future.get();
        }
        return new SqlFedAuthToken(authenticationResult.accessToken(), authenticationResult.expiresOnDate());
    } catch (MalformedURLException | InterruptedException | URISyntaxException e) {
        throw new SQLServerException(e.getMessage(), e);
    } catch (ExecutionException e) {
        throw getCorrectedException(e, user, authenticationString);
    } finally {
        executorService.shutdown();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IAccount(com.microsoft.aad.msal4j.IAccount) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) PublicClientApplication(com.microsoft.aad.msal4j.PublicClientApplication) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) SilentParameters(com.microsoft.aad.msal4j.SilentParameters) ExecutorService(java.util.concurrent.ExecutorService) MsalInteractionRequiredException(com.microsoft.aad.msal4j.MsalInteractionRequiredException) ExecutionException(java.util.concurrent.ExecutionException) InteractiveRequestParameters(com.microsoft.aad.msal4j.InteractiveRequestParameters)

Example 10 with PublicClientApplication

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

the class SQLServerMSAL4JUtils method getSqlFedAuthToken.

static SqlFedAuthToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String user, String password, String authenticationString) throws SQLServerException {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    try {
        final PublicClientApplication pca = PublicClientApplication.builder(ActiveDirectoryAuthentication.JDBC_FEDAUTH_CLIENT_ID).executorService(executorService).authority(fedAuthInfo.stsurl).build();
        final CompletableFuture<IAuthenticationResult> future = pca.acquireToken(UserNamePasswordParameters.builder(Collections.singleton(fedAuthInfo.spn + SLASH_DEFAULT), user, password.toCharArray()).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, user, authenticationString);
    } finally {
        executorService.shutdown();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) ExecutorService(java.util.concurrent.ExecutorService) PublicClientApplication(com.microsoft.aad.msal4j.PublicClientApplication) ExecutionException(java.util.concurrent.ExecutionException)

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