13
13
import org .slf4j .Logger ;
14
14
import org .slf4j .LoggerFactory ;
15
15
16
+ import java .util .UUID ;
16
17
import java .util .concurrent .CompletableFuture ;
17
18
import java .util .concurrent .ExecutionException ;
18
19
@@ -34,12 +35,13 @@ public class MsalRuntimeBroker implements IBroker {
34
35
35
36
@ Override
36
37
public CompletableFuture <IAuthenticationResult > acquireToken (PublicClientApplication application , SilentParameters parameters ) {
38
+ String correlationID = application .correlationId () == null ? generateCorrelationID () : application .correlationId ();
37
39
Account accountResult = null ;
38
40
39
41
//If request has an account ID, MSALRuntime likely has data cached for that account that we can retrieve
40
42
if (parameters .account () != null ) {
41
43
try {
42
- accountResult = ((ReadAccountResult ) interop .readAccountById (parameters .account ().homeAccountId ().split ("\\ ." )[0 ], application . correlationId () ).get ()).getAccount ();
44
+ accountResult = ((ReadAccountResult ) interop .readAccountById (parameters .account ().homeAccountId ().split ("\\ ." )[0 ], correlationID ).get ()).getAccount ();
43
45
} catch (InterruptedException | ExecutionException ex ) {
44
46
throw new MsalClientException (String .format ("MSALRuntime async operation interrupted when waiting for result: %s" , ex .getMessage ()), AuthenticationErrorCode .MSALRUNTIME_INTEROP_ERROR );
45
47
}
@@ -62,8 +64,8 @@ public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplica
62
64
AuthParameters authParameters = authParamsBuilder .build ();
63
65
64
66
if (accountResult == null ) {
65
- return interop .signInSilently (authParameters , application . correlationId () )
66
- .thenCompose (acctResult -> interop .acquireTokenSilently (authParameters , application . correlationId () , ((AuthResult ) acctResult ).getAccount ()))
67
+ return interop .signInSilently (authParameters , correlationID )
68
+ .thenCompose (acctResult -> interop .acquireTokenSilently (authParameters , correlationID , ((AuthResult ) acctResult ).getAccount ()))
67
69
.thenApply (authResult -> parseBrokerAuthResult (
68
70
application .authority (),
69
71
((AuthResult ) authResult ).getIdToken (),
@@ -73,7 +75,7 @@ public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplica
73
75
((AuthResult ) authResult ).getAccessTokenExpirationTime (),
74
76
((AuthResult ) authResult ).isPopAuthorization ()));
75
77
} else {
76
- return interop .acquireTokenSilently (authParameters , application . correlationId () , accountResult )
78
+ return interop .acquireTokenSilently (authParameters , correlationID , accountResult )
77
79
.thenApply (authResult -> parseBrokerAuthResult (application .authority (),
78
80
((AuthResult ) authResult ).getIdToken (),
79
81
((AuthResult ) authResult ).getAccessToken (),
@@ -89,6 +91,8 @@ public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplica
89
91
90
92
@ Override
91
93
public CompletableFuture <IAuthenticationResult > acquireToken (PublicClientApplication application , InteractiveRequestParameters parameters ) {
94
+ String correlationID = application .correlationId () == null ? generateCorrelationID () : application .correlationId ();
95
+
92
96
try {
93
97
AuthParameters .AuthParametersBuilder authParamsBuilder = new AuthParameters .
94
98
AuthParametersBuilder (application .clientId (),
@@ -106,8 +110,8 @@ public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplica
106
110
107
111
AuthParameters authParameters = authParamsBuilder .build ();
108
112
109
- return interop .signInInteractively (parameters .windowHandle (), authParameters , application . correlationId () , parameters .loginHint ())
110
- .thenCompose (acctResult -> interop .acquireTokenInteractively (parameters .windowHandle (), authParameters , application . correlationId () , ((AuthResult ) acctResult ).getAccount ()))
113
+ return interop .signInInteractively (parameters .windowHandle (), authParameters , correlationID , parameters .loginHint ())
114
+ .thenCompose (acctResult -> interop .acquireTokenInteractively (parameters .windowHandle (), authParameters , correlationID , ((AuthResult ) acctResult ).getAccount ()))
111
115
.thenApply (authResult -> parseBrokerAuthResult (
112
116
application .authority (),
113
117
((AuthResult ) authResult ).getIdToken (),
@@ -127,6 +131,8 @@ public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplica
127
131
@ Deprecated
128
132
@ Override
129
133
public CompletableFuture <IAuthenticationResult > acquireToken (PublicClientApplication application , UserNamePasswordParameters parameters ) {
134
+ String correlationID = application .correlationId () == null ? generateCorrelationID () : application .correlationId ();
135
+
130
136
try {
131
137
AuthParameters .AuthParametersBuilder authParamsBuilder = new AuthParameters .
132
138
AuthParametersBuilder (application .clientId (),
@@ -143,8 +149,8 @@ public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplica
143
149
144
150
AuthParameters authParameters = authParamsBuilder .build ();
145
151
146
- return interop .signInSilently (authParameters , application . correlationId () )
147
- .thenCompose (acctResult -> interop .acquireTokenSilently (authParameters , application . correlationId () , ((AuthResult ) acctResult ).getAccount ()))
152
+ return interop .signInSilently (authParameters , correlationID )
153
+ .thenCompose (acctResult -> interop .acquireTokenSilently (authParameters , correlationID , ((AuthResult ) acctResult ).getAccount ()))
148
154
.thenApply (authResult -> parseBrokerAuthResult (
149
155
application .authority (),
150
156
((AuthResult ) authResult ).getIdToken (),
@@ -160,11 +166,13 @@ public CompletableFuture<IAuthenticationResult> acquireToken(PublicClientApplica
160
166
161
167
@ Override
162
168
public void removeAccount (PublicClientApplication application , IAccount msalJavaAccount ) {
169
+ String correlationID = application .correlationId () == null ? generateCorrelationID () : application .correlationId ();
170
+
163
171
try {
164
- Account msalRuntimeAccount = ((ReadAccountResult ) interop .readAccountById (msalJavaAccount .homeAccountId ().split ("\\ ." )[0 ], application . correlationId () ).get ()).getAccount ();
172
+ Account msalRuntimeAccount = ((ReadAccountResult ) interop .readAccountById (msalJavaAccount .homeAccountId ().split ("\\ ." )[0 ], correlationID ).get ()).getAccount ();
165
173
166
174
if (msalRuntimeAccount != null ) {
167
- interop .signOutSilently (application .clientId (), application . correlationId () , msalRuntimeAccount );
175
+ interop .signOutSilently (application .clientId (), correlationID , msalRuntimeAccount );
168
176
}
169
177
} catch (MsalInteropException interopException ) {
170
178
throw new MsalClientException (interopException .getErrorMessage (), AuthenticationErrorCode .MSALRUNTIME_INTEROP_ERROR );
@@ -233,4 +241,9 @@ public void enableBrokerPIILogging(boolean enablePII) {
233
241
throw new MsalClientException (String .format ("Error occurred when calling MSALRuntime PII logging API: %s" , ex .getMessage ()), AuthenticationErrorCode .MSALRUNTIME_INTEROP_ERROR );
234
242
}
235
243
}
244
+
245
+ //Generates a random correlation ID, used when a correlation ID was not set at the application level
246
+ private String generateCorrelationID () {
247
+ return UUID .randomUUID ().toString ();
248
+ }
236
249
}
0 commit comments