3
3
4
4
package com .microsoft .aad .msal4j ;
5
5
6
- import com .nimbusds .jwt .JWTParser ;
7
-
8
- import java .net .URL ;
6
+ import java .util .Set ;
9
7
import java .util .concurrent .CompletableFuture ;
10
8
11
9
/**
12
10
* Used to define the basic set of methods that all Brokers must implement
13
11
*
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
16
14
*/
17
15
public interface IBroker {
18
16
17
+ /**
18
+ * checks if a IBroker implementation exists
19
+ */
20
+
21
+ default boolean isAvailable (){
22
+ return false ;
23
+ }
19
24
/**
20
25
* Acquire a token silently, i.e. without direct user interaction
21
26
*
22
27
* This may be accomplished by returning tokens from a token cache, using cached refresh tokens to get new tokens,
23
28
* 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
24
32
*/
25
- default CompletableFuture < IAuthenticationResult > acquireToken (PublicClientApplication application , SilentParameters requestParameters ) {
33
+ default IAuthenticationResult acquireToken (PublicClientApplication application , SilentParameters requestParameters ) {
26
34
throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
27
35
}
28
36
29
37
/**
30
38
* 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
31
42
*/
32
- default CompletableFuture < IAuthenticationResult > acquireToken (PublicClientApplication application , InteractiveRequestParameters parameters ) {
43
+ default IAuthenticationResult acquireToken (PublicClientApplication application , InteractiveRequestParameters requestParameters ) {
33
44
throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
34
45
}
35
46
36
47
/**
37
48
* 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
38
52
*/
39
- default CompletableFuture < IAuthenticationResult > acquireToken (PublicClientApplication application , UserNamePasswordParameters parameters ) {
53
+ default IAuthenticationResult acquireToken (PublicClientApplication application , UserNamePasswordParameters requestParameters ) {
40
54
throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
41
55
}
42
56
43
- default void removeAccount (PublicClientApplication application , IAccount account ) throws MsalClientException {
57
+ default CompletableFuture removeAccount (IAccount account ) {
44
58
throw new MsalClientException ("Broker implementation missing" , AuthenticationErrorCode .MISSING_BROKER );
45
59
}
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
- }
82
60
}
0 commit comments