|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.aad.msal4jbrokers; |
| 5 | + |
| 6 | +import com.microsoft.aad.msal4j.IAuthenticationResult; |
| 7 | +import com.microsoft.aad.msal4j.IBroker; |
| 8 | +import com.microsoft.aad.msal4j.InteractiveRequestParameters; |
| 9 | +import com.microsoft.aad.msal4j.PublicClientApplication; |
| 10 | +import com.microsoft.aad.msal4j.SilentParameters; |
| 11 | +import com.microsoft.aad.msal4j.UserNamePasswordParameters; |
| 12 | +import com.microsoft.aad.msal4j.MsalClientException; |
| 13 | +import com.microsoft.aad.msal4j.AuthenticationErrorCode; |
| 14 | +import com.microsoft.aad.msal4j.IAccount; |
| 15 | +import com.microsoft.azure.javamsalruntime.Account; |
| 16 | +import com.microsoft.azure.javamsalruntime.AuthParameters; |
| 17 | +import com.microsoft.azure.javamsalruntime.AuthResult; |
| 18 | +import com.microsoft.azure.javamsalruntime.MsalInteropException; |
| 19 | +import com.microsoft.azure.javamsalruntime.MsalRuntimeInterop; |
| 20 | +import com.microsoft.azure.javamsalruntime.ReadAccountResult; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | + |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.ExecutionException; |
| 26 | + |
| 27 | +public class MsalRuntimeBroker implements IBroker { |
| 28 | + private static final Logger LOG = LoggerFactory.getLogger(MsalRuntimeBroker.class); |
| 29 | + |
| 30 | + private static MsalRuntimeInterop interop; |
| 31 | + |
| 32 | + static { |
| 33 | + try { |
| 34 | + //MsalRuntimeInterop performs various initialization steps in a similar static block, |
| 35 | + // so when an MsalRuntimeBroker is created this will cause the interop layer to initialize |
| 36 | + interop = new MsalRuntimeInterop(); |
| 37 | + } catch (MsalInteropException e) { |
| 38 | + throw new MsalClientException(String.format("Could not initialize MSALRuntime: %s", e.getErrorMessage()), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, SilentParameters parameters) { |
| 44 | + Account accountResult = null; |
| 45 | + |
| 46 | + //If request has an account ID, MSALRuntime likely has data cached for that account that we can retrieve |
| 47 | + if (parameters.account() != null) { |
| 48 | + try { |
| 49 | + accountResult = ((ReadAccountResult) interop.readAccountById(parameters.account().homeAccountId(), application.correlationId()).get()).getAccount(); |
| 50 | + } catch (InterruptedException | ExecutionException ex) { |
| 51 | + throw new MsalClientException(String.format("MSALRuntime async operation interrupted when waiting for result: %s", ex.getMessage()), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + AuthParameters authParameters = new AuthParameters |
| 57 | + .AuthParametersBuilder(application.clientId(), |
| 58 | + application.authority(), |
| 59 | + String.join(" ", parameters.scopes())) |
| 60 | + .build(); |
| 61 | + |
| 62 | + if (accountResult == null) { |
| 63 | + return interop.signInSilently(authParameters, application.correlationId()) |
| 64 | + .thenCompose(acctResult -> interop.acquireTokenSilently(authParameters, application.correlationId(), ((AuthResult) acctResult).getAccount())) |
| 65 | + .thenApply(authResult -> parseBrokerAuthResult( |
| 66 | + application.authority(), |
| 67 | + ((AuthResult) authResult).getIdToken(), |
| 68 | + ((AuthResult) authResult).getAccessToken(), |
| 69 | + ((AuthResult) authResult).getAccount().getAccountId(), |
| 70 | + ((AuthResult) authResult).getAccount().getClientInfo(), |
| 71 | + ((AuthResult) authResult).getAccessTokenExpirationTime())); |
| 72 | + } else { |
| 73 | + return interop.acquireTokenSilently(authParameters, application.correlationId(), accountResult) |
| 74 | + .thenApply(authResult -> parseBrokerAuthResult(application.authority(), |
| 75 | + ((AuthResult) authResult).getIdToken(), |
| 76 | + ((AuthResult) authResult).getAccessToken(), |
| 77 | + ((AuthResult) authResult).getAccount().getAccountId(), |
| 78 | + ((AuthResult) authResult).getAccount().getClientInfo(), |
| 79 | + ((AuthResult) authResult).getAccessTokenExpirationTime()) |
| 80 | + |
| 81 | + ); |
| 82 | + } |
| 83 | + } catch (MsalInteropException interopException) { |
| 84 | + throw new MsalClientException(interopException.getErrorMessage(), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, InteractiveRequestParameters parameters) { |
| 90 | + try { |
| 91 | + AuthParameters authParameters = new AuthParameters |
| 92 | + .AuthParametersBuilder(application.clientId(), |
| 93 | + application.authority(), |
| 94 | + String.join(" ", parameters.scopes())) |
| 95 | + .build(); |
| 96 | + |
| 97 | + return interop.signInInteractively(parameters.windowHandle(), authParameters, application.correlationId(), parameters.loginHint()) |
| 98 | + .thenCompose(acctResult -> interop.acquireTokenInteractively(parameters.windowHandle(), authParameters, application.correlationId(), ((AuthResult) acctResult).getAccount())) |
| 99 | + .thenApply(authResult -> parseBrokerAuthResult( |
| 100 | + application.authority(), |
| 101 | + ((AuthResult) authResult).getIdToken(), |
| 102 | + ((AuthResult) authResult).getAccessToken(), |
| 103 | + ((AuthResult) authResult).getAccount().getAccountId(), |
| 104 | + ((AuthResult) authResult).getAccount().getClientInfo(), |
| 105 | + ((AuthResult) authResult).getAccessTokenExpirationTime()) |
| 106 | + ); |
| 107 | + } catch (MsalInteropException interopException) { |
| 108 | + throw new MsalClientException(interopException.getErrorMessage(), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @deprecated |
| 114 | + */ |
| 115 | + @Deprecated |
| 116 | + @Override |
| 117 | + public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, UserNamePasswordParameters parameters) { |
| 118 | + try { |
| 119 | + AuthParameters authParameters = |
| 120 | + new AuthParameters |
| 121 | + .AuthParametersBuilder(application.clientId(), |
| 122 | + application.authority(), |
| 123 | + String.join(" ", parameters.scopes())) |
| 124 | + .build(); |
| 125 | + |
| 126 | + authParameters.setUsernamePassword(parameters.username(), new String(parameters.password())); |
| 127 | + |
| 128 | + return interop.signInSilently(authParameters, application.correlationId()) |
| 129 | + .thenCompose(acctResult -> interop.acquireTokenSilently(authParameters, application.correlationId(), ((AuthResult) acctResult).getAccount())) |
| 130 | + .thenApply(authResult -> parseBrokerAuthResult( |
| 131 | + application.authority(), |
| 132 | + ((AuthResult) authResult).getIdToken(), |
| 133 | + ((AuthResult) authResult).getAccessToken(), |
| 134 | + ((AuthResult) authResult).getAccount().getAccountId(), |
| 135 | + ((AuthResult) authResult).getAccount().getClientInfo(), |
| 136 | + ((AuthResult) authResult).getAccessTokenExpirationTime())); |
| 137 | + } catch (MsalInteropException interopException) { |
| 138 | + throw new MsalClientException(interopException.getErrorMessage(), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void removeAccount(PublicClientApplication application, IAccount msalJavaAccount) { |
| 144 | + try { |
| 145 | + Account msalRuntimeAccount = ((ReadAccountResult) interop.readAccountById(msalJavaAccount.homeAccountId(), application.correlationId()).get()).getAccount(); |
| 146 | + |
| 147 | + if (msalRuntimeAccount != null) { |
| 148 | + interop.signOutSilently(application.clientId(), application.correlationId(), msalRuntimeAccount); |
| 149 | + } |
| 150 | + } catch (MsalInteropException interopException) { |
| 151 | + throw new MsalClientException(interopException.getErrorMessage(), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); |
| 152 | + } catch (InterruptedException | ExecutionException ex) { |
| 153 | + throw new MsalClientException(String.format("MSALRuntime async operation interrupted when waiting for result: %s", ex.getMessage()), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Calls MSALRuntime's startup API. If MSALRuntime started successfully, we can assume that the broker is available for use. |
| 159 | + * |
| 160 | + * If an exception is thrown when trying to start MSALRuntime, we assume that we cannot use the broker and will not make any more attempts to do so. |
| 161 | + * |
| 162 | + * @return boolean representing whether or not MSALRuntime started successfully |
| 163 | + */ |
| 164 | + @Override |
| 165 | + public boolean isBrokerAvailable() { |
| 166 | + try { |
| 167 | + interop.startupMsalRuntime(); |
| 168 | + |
| 169 | + LOG.info("MSALRuntime started successfully. MSAL Java will use MSALRuntime in all supported broker flows."); |
| 170 | + |
| 171 | + return true; |
| 172 | + } catch (MsalInteropException e) { |
| 173 | + LOG.warn("Exception thrown when trying to start MSALRuntime: {}", e.getErrorMessage()); |
| 174 | + LOG.warn("MSALRuntime could not be started. MSAL Java will fall back to non-broker flows."); |
| 175 | + |
| 176 | + return false; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments