@@ -70,16 +70,34 @@ private String getMsiErrorResponseNoRetry() {
70
70
return "{\" statusCode\" :\" 123\" ,\" message\" :\" Not one of the retryable error responses\" ,\" correlationId\" :\" 7d0c9763-ff1d-4842-a3f3-6d49e64f4513\" }" ;
71
71
}
72
72
73
+ private HttpRequest expectedRequest (ManagedIdentitySourceType source , String resource , boolean hasClaims , boolean hasCapabilities , String expectedTokenHash ) {
74
+ return expectedRequest (source , resource , ManagedIdentityId .systemAssigned (), hasClaims , hasCapabilities , expectedTokenHash );
75
+ }
76
+
77
+ private HttpRequest expectedRequest (ManagedIdentitySourceType source , String resource , ManagedIdentityId id ) {
78
+ return expectedRequest (source , resource , id , false , false , null );
79
+ }
80
+
73
81
private HttpRequest expectedRequest (ManagedIdentitySourceType source , String resource ) {
74
- return expectedRequest (source , resource , ManagedIdentityId .systemAssigned ());
82
+ return expectedRequest (source , resource , ManagedIdentityId .systemAssigned (), false , false , null );
75
83
}
76
84
77
85
private HttpRequest expectedRequest (ManagedIdentitySourceType source , String resource ,
78
- ManagedIdentityId id ) {
86
+ ManagedIdentityId id , boolean hasClaims , boolean hasCapabilities , String expectedTokenHash ) {
79
87
String endpoint = null ;
80
88
Map <String , String > headers = new HashMap <>();
81
89
Map <String , List <String >> queryParameters = new HashMap <>();
82
90
91
+ if (Constants .TOKEN_REVOCATION_SUPPORTED_ENVIRONMENTS .contains (source )) {
92
+ if (hasCapabilities ) {
93
+ queryParameters .put (Constants .CLIENT_CAPABILITY_REQUEST_PARAM , Collections .singletonList ("cp1" ));
94
+ }
95
+
96
+ if (hasClaims ) {
97
+ queryParameters .put (Constants .TOKEN_HASH_CLAIM , Collections .singletonList (expectedTokenHash ));
98
+ }
99
+ }
100
+
83
101
switch (source ) {
84
102
case APP_SERVICE :
85
103
endpoint = appServiceEndpoint ;
@@ -93,12 +111,6 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
93
111
headers .put ("Metadata" , "true" );
94
112
queryParameters .put ("resource" , Collections .singletonList (resource ));
95
113
break ;
96
- case IMDS :
97
- endpoint = IMDS_ENDPOINT ;
98
- queryParameters .put ("api-version" , Collections .singletonList ("2018-02-01" ));
99
- queryParameters .put ("resource" , Collections .singletonList (resource ));
100
- headers .put ("Metadata" , "true" );
101
- break ;
102
114
case AZURE_ARC :
103
115
endpoint = azureArcEndpoint ;
104
116
queryParameters .put ("api-version" , Collections .singletonList ("2019-11-01" ));
@@ -111,6 +123,7 @@ private HttpRequest expectedRequest(ManagedIdentitySourceType source, String res
111
123
queryParameters .put ("resource" , Collections .singletonList (resource ));
112
124
headers .put ("secret" , "secret" );
113
125
break ;
126
+ case IMDS :
114
127
case NONE :
115
128
case DEFAULT_TO_IMDS :
116
129
endpoint = IMDS_ENDPOINT ;
@@ -657,6 +670,9 @@ void managedIdentityTest_WithClaims(ManagedIdentitySourceType source, String end
657
670
assertNotNull (result .accessToken ());
658
671
assertEquals (TokenSource .CACHE , result .metadata ().tokenSource ());
659
672
673
+ String expectedTokenHash = StringHelper .createSha256HashHexString (result .accessToken ());
674
+ when (httpClientMock .send (expectedRequest (source , resource , true , false , expectedTokenHash ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
675
+
660
676
// Third call, when claims are passed bypass the cache.
661
677
result = miApp .acquireTokenForManagedIdentity (
662
678
ManagedIdentityParameters .builder (resource )
@@ -669,6 +685,46 @@ void managedIdentityTest_WithClaims(ManagedIdentitySourceType source, String end
669
685
verify (httpClientMock , times (2 )).send (any ());
670
686
}
671
687
688
+ @ ParameterizedTest
689
+ @ MethodSource ("com.microsoft.aad.msal4j.ManagedIdentityTestDataProvider#createDataError" )
690
+ void managedIdentityTest_WithCapabilitiesOnly (ManagedIdentitySourceType source , String endpoint ) throws Exception {
691
+ IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
692
+ ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
693
+ DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
694
+ if (source == SERVICE_FABRIC ) {
695
+ ServiceFabricManagedIdentitySource .setHttpClient (httpClientMock );
696
+ }
697
+
698
+ when (httpClientMock .send (expectedRequest (source , resource , false , true , null ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
699
+
700
+ miApp = ManagedIdentityApplication
701
+ .builder (ManagedIdentityId .systemAssigned ())
702
+ .httpClient (httpClientMock )
703
+ .clientCapabilities (singletonList ("cp1" ))
704
+ .build ();
705
+
706
+ // Clear caching to avoid cross test pollution.
707
+ miApp .tokenCache ().accessTokens .clear ();
708
+
709
+ // First call, get the token from the identity provider.
710
+ IAuthenticationResult result = miApp .acquireTokenForManagedIdentity (
711
+ ManagedIdentityParameters .builder (resource )
712
+ .build ()).get ();
713
+
714
+ assertNotNull (result .accessToken ());
715
+ assertEquals (TokenSource .IDENTITY_PROVIDER , result .metadata ().tokenSource ());
716
+
717
+ // Second call, get the token from the cache without passing the claims.
718
+ result = miApp .acquireTokenForManagedIdentity (
719
+ ManagedIdentityParameters .builder (resource )
720
+ .build ()).get ();
721
+
722
+ assertNotNull (result .accessToken ());
723
+ assertEquals (TokenSource .CACHE , result .metadata ().tokenSource ());
724
+
725
+ verify (httpClientMock , times (1 )).send (any ());
726
+ }
727
+
672
728
@ ParameterizedTest
673
729
@ MethodSource ("com.microsoft.aad.msal4j.ManagedIdentityTestDataProvider#createDataError" )
674
730
void managedIdentity_ClaimsAndCapabilities (ManagedIdentitySourceType source , String endpoint ) throws Exception {
@@ -679,7 +735,7 @@ void managedIdentity_ClaimsAndCapabilities(ManagedIdentitySourceType source, Str
679
735
ServiceFabricManagedIdentitySource .setHttpClient (httpClientMock );
680
736
}
681
737
682
- when (httpClientMock .send (expectedRequest (source , resource ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
738
+ when (httpClientMock .send (expectedRequest (source , resource , false , true , null ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
683
739
684
740
miApp = ManagedIdentityApplication
685
741
.builder (ManagedIdentityId .systemAssigned ())
@@ -707,6 +763,9 @@ void managedIdentity_ClaimsAndCapabilities(ManagedIdentitySourceType source, Str
707
763
assertNotNull (result .accessToken ());
708
764
assertEquals (TokenSource .CACHE , result .metadata ().tokenSource ());
709
765
766
+ String expectedTokenHash = StringHelper .createSha256HashHexString (result .accessToken ());
767
+ when (httpClientMock .send (expectedRequest (source , resource , true , true , expectedTokenHash ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
768
+
710
769
// Third call, when claims are passed bypass the cache.
711
770
result = miApp .acquireTokenForManagedIdentity (
712
771
ManagedIdentityParameters .builder (resource )
@@ -715,8 +774,6 @@ void managedIdentity_ClaimsAndCapabilities(ManagedIdentitySourceType source, Str
715
774
716
775
assertNotNull (result .accessToken ());
717
776
assertEquals (TokenSource .IDENTITY_PROVIDER , result .metadata ().tokenSource ());
718
-
719
- verify (httpClientMock , times (2 )).send (any ());
720
777
}
721
778
722
779
@ ParameterizedTest
0 commit comments