Search in sources :

Example 1 with IntegratedWindowsAuthenticationParameters

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

the class IntegratedWindowsAuthenticationFlow method acquireTokenIntegratedWindowsAuth.

private static IAuthenticationResult acquireTokenIntegratedWindowsAuth(PublicClientApplication pca, Set<String> scope, IAccount account, String username) throws Exception {
    IAuthenticationResult result;
    try {
        SilentParameters silentParameters = SilentParameters.builder(scope).account(account).build();
        // Try to acquire token silently. This will fail on the first acquireTokenIntegratedWindowsAuth() call
        // because the token cache does not have any data for the user you are trying to acquire a token for
        result = pca.acquireTokenSilently(silentParameters).join();
        System.out.println("==acquireTokenSilently call succeeded");
    } catch (Exception ex) {
        if (ex.getCause() instanceof MsalException) {
            System.out.println("==acquireTokenSilently call failed: " + ex.getCause());
            IntegratedWindowsAuthenticationParameters parameters = IntegratedWindowsAuthenticationParameters.builder(scope, username).build();
            // Try to acquire a token using Integrated Windows Authentication (IWA). You will need to generate a Kerberos ticket.
            // If successful, you should see the token and account information printed out to console
            result = pca.acquireToken(parameters).join();
            System.out.println("==Integrated Windows Authentication flow succeeded");
        } else {
            // Handle other exceptions accordingly
            throw ex;
        }
    }
    return result;
}
Also used : IntegratedWindowsAuthenticationParameters(com.microsoft.aad.msal4j.IntegratedWindowsAuthenticationParameters) SilentParameters(com.microsoft.aad.msal4j.SilentParameters) MsalException(com.microsoft.aad.msal4j.MsalException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) IOException(java.io.IOException) MsalException(com.microsoft.aad.msal4j.MsalException)

Aggregations

IAuthenticationResult (com.microsoft.aad.msal4j.IAuthenticationResult)1 IntegratedWindowsAuthenticationParameters (com.microsoft.aad.msal4j.IntegratedWindowsAuthenticationParameters)1 MsalException (com.microsoft.aad.msal4j.MsalException)1 SilentParameters (com.microsoft.aad.msal4j.SilentParameters)1 IOException (java.io.IOException)1