Search in sources :

Example 1 with MsalException

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

the class InteractiveFlowB2C method acquireTokenInteractiveB2C.

private static IAuthenticationResult acquireTokenInteractiveB2C() 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).b2cAuthority(AUTHORITY).setTokenCacheAccessAspect(tokenCacheAspect).build();
    Set<IAccount> accountsInCache = pca.getAccounts().join();
    // Use 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) {
            // For B2C, you have to specify a port for the redirect URL
            InteractiveRequestParameters parameters = InteractiveRequestParameters.builder(new URI("http://localhost:8080")).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)

Example 2 with MsalException

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

the class UsernamePasswordFlow method acquireTokenUsernamePassword.

private static IAuthenticationResult acquireTokenUsernamePassword(PublicClientApplication pca, Set<String> scope, IAccount account, String username, String password) throws Exception {
    IAuthenticationResult result;
    try {
        SilentParameters silentParameters = SilentParameters.builder(scope).account(account).build();
        // Try to acquire token silently. This will fail on the first acquireTokenUsernamePassword() 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());
            UserNamePasswordParameters parameters = UserNamePasswordParameters.builder(scope, username, password.toCharArray()).build();
            // Try to acquire a token via username/password. If successful, you should see
            // the token and account information printed out to console
            result = pca.acquireToken(parameters).join();
            System.out.println("==username/password flow succeeded");
        } else {
            // Handle other exceptions accordingly
            throw ex;
        }
    }
    return result;
}
Also used : SilentParameters(com.microsoft.aad.msal4j.SilentParameters) MsalException(com.microsoft.aad.msal4j.MsalException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) UserNamePasswordParameters(com.microsoft.aad.msal4j.UserNamePasswordParameters) IOException(java.io.IOException) MsalException(com.microsoft.aad.msal4j.MsalException)

Example 3 with MsalException

use of com.microsoft.aad.msal4j.MsalException in project OpenOLAT by OpenOLAT.

the class MicrosoftGraphAccessTokenManager method connect.

private CompletableFuture<String> connect(String id, String secret, String tenant) {
    ConfidentialClientApplication cca = createClientApplication(id, secret, tenant);
    CompletableFuture<IAuthenticationResult> result = null;
    if (cca != null) {
        try {
            if (cache.isEmpty()) {
                ClientCredentialParameters parameters = ClientCredentialParameters.builder(SCOPES).build();
                result = cca.acquireToken(parameters);
            } else {
                SilentParameters silentParameters = SilentParameters.builder(SCOPES).build();
                // try to acquire token silently. This call will fail since the token cache does not
                // have a token for the application you are requesting an access token for
                result = cca.acquireTokenSilently(silentParameters);
            }
        } catch (Exception ex) {
            if (ex.getCause() instanceof MsalException) {
                ClientCredentialParameters parameters = ClientCredentialParameters.builder(SCOPES).build();
                result = cca.acquireToken(parameters);
            } else {
                log.error("", ex);
            }
        }
    }
    if (result != null) {
        return result.handleAsync((res, ex) -> {
            if (ex != null && (ex instanceof MsalException || ex.getCause() instanceof MsalException)) {
                ClientCredentialParameters parameters = ClientCredentialParameters.builder(SCOPES).build();
                return cca.acquireToken(parameters).join();
            }
            return res;
        }).thenApply(IAuthenticationResult::accessToken);
    }
    return CompletableFuture.completedFuture((String) null);
}
Also used : ClientCredentialFactory(com.microsoft.aad.msal4j.ClientCredentialFactory) IClientCredential(com.microsoft.aad.msal4j.IClientCredential) MalformedURLException(java.net.MalformedURLException) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) ITokenCacheAccessAspect(com.microsoft.aad.msal4j.ITokenCacheAccessAspect) ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) Logger(org.apache.logging.log4j.Logger) ITokenCacheAccessContext(com.microsoft.aad.msal4j.ITokenCacheAccessContext) SilentParameters(com.microsoft.aad.msal4j.SilentParameters) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) Tracing(org.olat.core.logging.Tracing) MsalException(com.microsoft.aad.msal4j.MsalException) ConfidentialClientApplication(com.microsoft.aad.msal4j.ConfidentialClientApplication) SilentParameters(com.microsoft.aad.msal4j.SilentParameters) MsalException(com.microsoft.aad.msal4j.MsalException) IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters) MalformedURLException(java.net.MalformedURLException) MsalException(com.microsoft.aad.msal4j.MsalException)

Example 4 with MsalException

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

Example 5 with MsalException

use of com.microsoft.aad.msal4j.MsalException 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)5 MsalException (com.microsoft.aad.msal4j.MsalException)5 SilentParameters (com.microsoft.aad.msal4j.SilentParameters)5 IAccount (com.microsoft.aad.msal4j.IAccount)2 InteractiveRequestParameters (com.microsoft.aad.msal4j.InteractiveRequestParameters)2 PublicClientApplication (com.microsoft.aad.msal4j.PublicClientApplication)2 IOException (java.io.IOException)2 URI (java.net.URI)2 ClientCredentialFactory (com.microsoft.aad.msal4j.ClientCredentialFactory)1 ClientCredentialParameters (com.microsoft.aad.msal4j.ClientCredentialParameters)1 ConfidentialClientApplication (com.microsoft.aad.msal4j.ConfidentialClientApplication)1 IClientCredential (com.microsoft.aad.msal4j.IClientCredential)1 ITokenCacheAccessAspect (com.microsoft.aad.msal4j.ITokenCacheAccessAspect)1 ITokenCacheAccessContext (com.microsoft.aad.msal4j.ITokenCacheAccessContext)1 IntegratedWindowsAuthenticationParameters (com.microsoft.aad.msal4j.IntegratedWindowsAuthenticationParameters)1 UserNamePasswordParameters (com.microsoft.aad.msal4j.UserNamePasswordParameters)1 MalformedURLException (java.net.MalformedURLException)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Logger (org.apache.logging.log4j.Logger)1