-
Notifications
You must be signed in to change notification settings - Fork 155
Add IBroker implementation for MSALRuntime #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e9a44d5
Add IBroker implementation for MSALRuntime
Avery-Dunn 31aa38d
Merge branches 'avdunn/msalruntime-broker' and 'dev' of https://githu…
Avery-Dunn 8a8df73
Remove dll used during testing
Avery-Dunn 5b8e235
Integrate broker steps to relevant flows in PublicClientApplication
Avery-Dunn ade0717
Add logic to cancel MsalRuntimeFutures
Avery-Dunn 77e9304
Expand javadocs and exception handling
Avery-Dunn 45b4a40
Address code review comments
Avery-Dunn 3df9af2
Simplify future chaining, address code review comments
Avery-Dunn 2fd56ec
Reorganize future chaining, fix testing issues
Avery-Dunn fdf9ef7
Adjust how broker availability is checked
Avery-Dunn 9152677
Create automated test
Avery-Dunn dc79a19
Adjust startup logic
Avery-Dunn ab4f576
Correct version number for interop
Avery-Dunn a518eda
Correct broker versioning
Avery-Dunn 40da5b0
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
Avery-Dunn 9349e7e
Move broker tests to MSAL Java package
Avery-Dunn b95362b
Remove usage of msal4j-brokers from msal4j
Avery-Dunn f684388
Add missing SLFJ dependency
Avery-Dunn 0ba0522
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
Avery-Dunn 8e73565
Use newest msal4j
Avery-Dunn de9c85b
Bump javamsalruntime version number
Avery-Dunn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
msal4j-brokers/src/main/java/com/microsoft/aad/msal4jbrokers/MSALRuntimeBroker.java
This file was deleted.
Oops, something went wrong.
179 changes: 179 additions & 0 deletions
179
msal4j-brokers/src/main/java/com/microsoft/aad/msal4jbrokers/MsalRuntimeBroker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.aad.msal4jbrokers; | ||
|
|
||
| import com.microsoft.aad.msal4j.IAuthenticationResult; | ||
| import com.microsoft.aad.msal4j.IBroker; | ||
| import com.microsoft.aad.msal4j.InteractiveRequestParameters; | ||
| import com.microsoft.aad.msal4j.PublicClientApplication; | ||
| import com.microsoft.aad.msal4j.SilentParameters; | ||
| import com.microsoft.aad.msal4j.UserNamePasswordParameters; | ||
| import com.microsoft.aad.msal4j.MsalClientException; | ||
| import com.microsoft.aad.msal4j.AuthenticationErrorCode; | ||
| import com.microsoft.aad.msal4j.IAccount; | ||
| import com.microsoft.azure.javamsalruntime.Account; | ||
| import com.microsoft.azure.javamsalruntime.AuthParameters; | ||
| import com.microsoft.azure.javamsalruntime.AuthResult; | ||
| import com.microsoft.azure.javamsalruntime.MsalInteropException; | ||
| import com.microsoft.azure.javamsalruntime.MsalRuntimeInterop; | ||
| import com.microsoft.azure.javamsalruntime.ReadAccountResult; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| public class MsalRuntimeBroker implements IBroker { | ||
| private static final Logger LOG = LoggerFactory.getLogger(MsalRuntimeBroker.class); | ||
|
|
||
| private static MsalRuntimeInterop interop; | ||
|
|
||
| static { | ||
| try { | ||
| //MsalRuntimeInterop performs various initialization steps in a similar static block, | ||
| // so when an MsalRuntimeBroker is created this will cause the interop layer to initialize | ||
| interop = new MsalRuntimeInterop(); | ||
Avery-Dunn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (MsalInteropException e) { | ||
| throw new MsalClientException(String.format("Could not initialize MSALRuntime: %s", e.getErrorMessage()), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, SilentParameters parameters) { | ||
| Account accountResult = null; | ||
|
|
||
| //If request has an account ID, MSALRuntime likely has data cached for that account that we can retrieve | ||
| if (parameters.account() != null) { | ||
| try { | ||
| accountResult = ((ReadAccountResult) interop.readAccountById(parameters.account().homeAccountId(), application.correlationId()).get()).getAccount(); | ||
| } catch (InterruptedException | ExecutionException ex) { | ||
| throw new MsalClientException(String.format("MSALRuntime async operation interrupted when waiting for result: %s", ex.getMessage()), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| AuthParameters authParameters = new AuthParameters | ||
| .AuthParametersBuilder(application.clientId(), | ||
| application.authority(), | ||
| String.join(" ", parameters.scopes())) | ||
| .build(); | ||
|
|
||
| if (accountResult == null) { | ||
Avery-Dunn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return interop.signInSilently(authParameters, application.correlationId()) | ||
| .thenCompose(acctResult -> interop.acquireTokenSilently(authParameters, application.correlationId(), ((AuthResult) acctResult).getAccount())) | ||
| .thenApply(authResult -> parseBrokerAuthResult( | ||
| application.authority(), | ||
| ((AuthResult) authResult).getIdToken(), | ||
| ((AuthResult) authResult).getAccessToken(), | ||
| ((AuthResult) authResult).getAccount().getAccountId(), | ||
| ((AuthResult) authResult).getAccount().getClientInfo(), | ||
| ((AuthResult) authResult).getAccessTokenExpirationTime())); | ||
| } else { | ||
| return interop.acquireTokenSilently(authParameters, application.correlationId(), accountResult) | ||
| .thenApply(authResult -> parseBrokerAuthResult(application.authority(), | ||
| ((AuthResult) authResult).getIdToken(), | ||
| ((AuthResult) authResult).getAccessToken(), | ||
| ((AuthResult) authResult).getAccount().getAccountId(), | ||
| ((AuthResult) authResult).getAccount().getClientInfo(), | ||
| ((AuthResult) authResult).getAccessTokenExpirationTime()) | ||
|
|
||
| ); | ||
| } | ||
| } catch (MsalInteropException interopException) { | ||
| throw new MsalClientException(interopException.getErrorMessage(), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, InteractiveRequestParameters parameters) { | ||
| try { | ||
| AuthParameters authParameters = new AuthParameters | ||
| .AuthParametersBuilder(application.clientId(), | ||
| application.authority(), | ||
| String.join(" ", parameters.scopes())) | ||
| .build(); | ||
|
|
||
| return interop.signInInteractively(parameters.windowHandle(), authParameters, application.correlationId(), parameters.loginHint()) | ||
| .thenCompose(acctResult -> interop.acquireTokenInteractively(parameters.windowHandle(), authParameters, application.correlationId(), ((AuthResult) acctResult).getAccount())) | ||
| .thenApply(authResult -> parseBrokerAuthResult( | ||
| application.authority(), | ||
| ((AuthResult) authResult).getIdToken(), | ||
| ((AuthResult) authResult).getAccessToken(), | ||
| ((AuthResult) authResult).getAccount().getAccountId(), | ||
| ((AuthResult) authResult).getAccount().getClientInfo(), | ||
| ((AuthResult) authResult).getAccessTokenExpirationTime()) | ||
| ); | ||
| } catch (MsalInteropException interopException) { | ||
| throw new MsalClientException(interopException.getErrorMessage(), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated | ||
| */ | ||
| @Deprecated | ||
| @Override | ||
| public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplication application, UserNamePasswordParameters parameters) { | ||
| try { | ||
| AuthParameters authParameters = | ||
| new AuthParameters | ||
| .AuthParametersBuilder(application.clientId(), | ||
| application.authority(), | ||
| String.join(" ", parameters.scopes())) | ||
| .build(); | ||
|
|
||
| authParameters.setUsernamePassword(parameters.username(), new String(parameters.password())); | ||
|
|
||
| return interop.signInSilently(authParameters, application.correlationId()) | ||
| .thenCompose(acctResult -> interop.acquireTokenSilently(authParameters, application.correlationId(), ((AuthResult) acctResult).getAccount())) | ||
| .thenApply(authResult -> parseBrokerAuthResult( | ||
| application.authority(), | ||
| ((AuthResult) authResult).getIdToken(), | ||
| ((AuthResult) authResult).getAccessToken(), | ||
| ((AuthResult) authResult).getAccount().getAccountId(), | ||
| ((AuthResult) authResult).getAccount().getClientInfo(), | ||
| ((AuthResult) authResult).getAccessTokenExpirationTime())); | ||
| } catch (MsalInteropException interopException) { | ||
| throw new MsalClientException(interopException.getErrorMessage(), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void removeAccount(PublicClientApplication application, IAccount msalJavaAccount) { | ||
| try { | ||
| Account msalRuntimeAccount = ((ReadAccountResult) interop.readAccountById(msalJavaAccount.homeAccountId(), application.correlationId()).get()).getAccount(); | ||
|
|
||
| if (msalRuntimeAccount != null) { | ||
| interop.signOutSilently(application.clientId(), application.correlationId(), msalRuntimeAccount); | ||
| } | ||
| } catch (MsalInteropException interopException) { | ||
| throw new MsalClientException(interopException.getErrorMessage(), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); | ||
| } catch (InterruptedException | ExecutionException ex) { | ||
| throw new MsalClientException(String.format("MSALRuntime async operation interrupted when waiting for result: %s", ex.getMessage()), AuthenticationErrorCode.MSALRUNTIME_INTEROP_ERROR); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calls MSALRuntime's startup API. If MSALRuntime started successfully, we can assume that the broker is available for use. | ||
| * | ||
| * 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. | ||
| * | ||
| * @return boolean representing whether or not MSALRuntime started successfully | ||
| */ | ||
| @Override | ||
| public boolean isBrokerAvailable() { | ||
| try { | ||
| interop.startupMsalRuntime(); | ||
|
|
||
| LOG.info("MSALRuntime started successfully. MSAL Java will use MSALRuntime in all supported broker flows."); | ||
|
|
||
| return true; | ||
| } catch (MsalInteropException e) { | ||
| LOG.warn("Exception thrown when trying to start MSALRuntime: {}", e.getErrorMessage()); | ||
| LOG.warn("MSALRuntime could not be started. MSAL Java will fall back to non-broker flows."); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.