1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ package com .microsoft .aad .msal4j ;
5
+
6
+ import java .util .Collections ;
7
+ import java .util .HashMap ;
8
+ import java .util .List ;
9
+ import java .util .Map ;
10
+
11
+ import org .junit .jupiter .api .Test ;
12
+ import org .junit .jupiter .api .extension .ExtendWith ;
13
+ import org .mockito .junit .jupiter .MockitoExtension ;
14
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
15
+ import static org .mockito .ArgumentMatchers .any ;
16
+ import static org .mockito .Mockito .*;
17
+ import static org .mockito .Mockito .times ;
18
+
19
+ @ ExtendWith (MockitoExtension .class )
20
+ class OnBehalfOfTests {
21
+
22
+ private String getSuccessfulResponse () {
23
+ return "{\" access_token\" :\" accessToken\" ,\" expires_in\" : \" " + 60 *60 *1000 +"\" ,\" token_type\" :" +
24
+ "\" Bearer\" ,\" client_id\" :\" client_id\" ,\" Content-Type\" :\" text/html; charset=utf-8\" }" ;
25
+ }
26
+
27
+ private HttpResponse expectedResponse (int statusCode , String response ) {
28
+ Map <String , List <String >> headers = new HashMap <String , List <String >>();
29
+ headers .put ("Content-Type" , Collections .singletonList ("application/json" ));
30
+
31
+ HttpResponse httpResponse = new HttpResponse ();
32
+ httpResponse .statusCode (statusCode );
33
+ httpResponse .body (response );
34
+ httpResponse .addHeaders (headers );
35
+
36
+ return httpResponse ;
37
+ }
38
+
39
+ @ Test
40
+ void OnBehalfOf_InternalCacheLookup_Success () throws Exception {
41
+ DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
42
+
43
+ when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ()));
44
+
45
+ ConfidentialClientApplication cca =
46
+ ConfidentialClientApplication .builder ("clientId" , ClientCredentialFactory .createFromSecret ("password" ))
47
+ .authority ("https://login.microsoftonline.com/tenant/" )
48
+ .instanceDiscovery (false )
49
+ .validateAuthority (false )
50
+ .httpClient (httpClientMock )
51
+ .build ();
52
+
53
+ OnBehalfOfParameters parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedToken )).build ();
54
+
55
+ IAuthenticationResult result = cca .acquireToken (parameters ).get ();
56
+ IAuthenticationResult result2 = cca .acquireToken (parameters ).get ();
57
+
58
+ //OBO flow should perform an internal cache lookup, so similar parameters should only cause one HTTP client call
59
+ assertEquals (result .accessToken (), result2 .accessToken ());
60
+ verify (httpClientMock , times (1 )).send (any ());
61
+ }
62
+
63
+ @ Test
64
+ void OnBehalfOf_TenantOverride () throws Exception {
65
+ DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
66
+
67
+ when (httpClientMock .send (any (HttpRequest .class ))).thenReturn (expectedResponse (200 , getSuccessfulResponse ()));
68
+
69
+ ConfidentialClientApplication cca =
70
+ ConfidentialClientApplication .builder ("clientId" , ClientCredentialFactory .createFromSecret ("password" ))
71
+ .authority ("https://login.microsoftonline.com/tenant" )
72
+ .instanceDiscovery (false )
73
+ .validateAuthority (false )
74
+ .httpClient (httpClientMock )
75
+ .build ();
76
+
77
+ OnBehalfOfParameters parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedToken )).build ();
78
+ //The two acquireToken calls have the same parameters and should only cause one call from the HTTP client
79
+
80
+ cca .acquireToken (parameters ).get ();
81
+ cca .acquireToken (parameters ).get ();
82
+ verify (httpClientMock , times (1 )).send (any ());
83
+
84
+ parameters = OnBehalfOfParameters .builder (Collections .singleton ("scopes" ), new UserAssertion (TestHelper .signedToken )).tenant ("otherTenant" ).build ();
85
+ //Overriding the tenant parameter in the request should lead to a new token call being made, but followup calls should not
86
+ cca .acquireToken (parameters ).get ();
87
+ cca .acquireToken (parameters ).get ();
88
+ verify (httpClientMock , times (2 )).send (any ());
89
+ }
90
+ }
0 commit comments