33
44package com .microsoft .aad .msal4j ;
55
6- import com .nimbusds .jwt .JWTParser ;
7-
8- import java .net .URL ;
6+ import java .util .Set ;
97import java .util .concurrent .CompletableFuture ;
108
119/**
1210 * Used to define the basic set of methods that all Brokers must implement
1311 *
14- * All methods are marked as default so they can be referenced by MSAL Java without an implementation,
15- * and most will simply throw an exception if not overridden by an IBroker implementation
12+ * All methods are so they can be referenced by MSAL Java without an implementation, and by default simply throw an
13+ * exception saying that a broker implementation is missing
1614 */
1715public interface IBroker {
1816
17+ /**
18+ * checks if a IBroker implementation exists
19+ */
20+
21+ default boolean isAvailable (){
22+ return false ;
23+ }
1924 /**
2025 * Acquire a token silently, i.e. without direct user interaction
2126 *
2227 * This may be accomplished by returning tokens from a token cache, using cached refresh tokens to get new tokens,
2328 * or via any authentication flow where a user is not prompted to enter credentials
29+ *
30+ * @param requestParameters MsalRequest object which contains everything needed for the broker implementation to make a request
31+ * @return IBroker implementations will return an AuthenticationResult object
2432 */
25- default CompletableFuture < IAuthenticationResult > acquireToken (PublicClientApplication application , SilentParameters requestParameters ) {
33+ default IAuthenticationResult acquireToken (PublicClientApplication application , SilentParameters requestParameters ) {
2634 throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
2735 }
2836
2937 /**
3038 * Acquire a token interactively, by prompting users to enter their credentials in some way
39+ *
40+ * @param requestParameters MsalRequest object which contains everything needed for the broker implementation to make a request
41+ * @return IBroker implementations will return an AuthenticationResult object
3142 */
32- default CompletableFuture < IAuthenticationResult > acquireToken (PublicClientApplication application , InteractiveRequestParameters parameters ) {
43+ default IAuthenticationResult acquireToken (PublicClientApplication application , InteractiveRequestParameters requestParameters ) {
3344 throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
3445 }
3546
3647 /**
3748 * Acquire a token silently, i.e. without direct user interaction, using username/password authentication
49+ *
50+ * @param requestParameters MsalRequest object which contains everything needed for the broker implementation to make a request
51+ * @return IBroker implementations will return an AuthenticationResult object
3852 */
39- default CompletableFuture < IAuthenticationResult > acquireToken (PublicClientApplication application , UserNamePasswordParameters parameters ) {
53+ default IAuthenticationResult acquireToken (PublicClientApplication application , UserNamePasswordParameters requestParameters ) {
4054 throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
4155 }
4256
43- default void removeAccount (PublicClientApplication application , IAccount account ) throws MsalClientException {
57+ default CompletableFuture removeAccount (IAccount account ) {
4458 throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
4559 }
46-
47- default boolean isBrokerAvailable () {
48- throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
49- }
50-
51- /**
52- * MSAL Java's AuthenticationResult requires several package-private classes that a broker implementation can't access,
53- * so this helper method can be used to create AuthenticationResults from within the MSAL Java package
54- */
55- default IAuthenticationResult parseBrokerAuthResult (String authority , String idToken , String accessToken ,
56- String accountId , String clientInfo ,
57- long accessTokenExpirationTime ) {
58-
59- AuthenticationResult .AuthenticationResultBuilder builder = AuthenticationResult .builder ();
60-
61- try {
62- if (idToken != null ) {
63- builder .idToken (idToken );
64- if (accountId != null ) {
65- String idTokenJson =
66- JWTParser .parse (idToken ).getParsedParts ()[1 ].decodeToString ();
67- //TODO: need to figure out if 'policy' field is relevant for brokers
68- builder .accountCacheEntity (AccountCacheEntity .create (clientInfo ,
69- Authority .createAuthority (new URL (authority )), JsonHelper .convertJsonToObject (idTokenJson ,
70- IdToken .class ), null ));
71- }
72- }
73- if (accessToken != null ) {
74- builder .accessToken (accessToken );
75- builder .expiresOn (accessTokenExpirationTime );
76- }
77- } catch (Exception e ) {
78- throw new MsalClientException (String .format ("Exception when converting broker result to MSAL Java AuthenticationResult: %s" , e .getMessage ()), AuthenticationErrorCode .MSALJAVA_BROKERS_ERROR );
79- }
80- return builder .build ();
81- }
8260}
0 commit comments