1818
1919class AadInstanceDiscoveryProvider {
2020
21- private final static String DEFAULT_TRUSTED_HOST = "login.microsoftonline.com" ;
22- private final static String AUTHORIZE_ENDPOINT_TEMPLATE = "https://{host}/{tenant}/oauth2/v2.0/authorize" ;
23- private final static String INSTANCE_DISCOVERY_ENDPOINT_TEMPLATE = "https://{host}:{port}/common/discovery/instance" ;
24- private final static String INSTANCE_DISCOVERY_REQUEST_PARAMETERS_TEMPLATE = "?api-version=1.1&authorization_endpoint={authorizeEndpoint}" ;
25- private final static String HOST_TEMPLATE_WITH_REGION = "{region}.login.microsoft.com" ;
26- private final static String SOVEREIGN_HOST_TEMPLATE_WITH_REGION = "{region}.{host}" ;
27- private final static String REGION_NAME = "REGION_NAME" ;
28- private final static int PORT_NOT_SET = -1 ;
21+ private static final String DEFAULT_TRUSTED_HOST = "login.microsoftonline.com" ;
22+ private static final String AUTHORIZE_ENDPOINT_TEMPLATE = "https://{host}/{tenant}/oauth2/v2.0/authorize" ;
23+ private static final String INSTANCE_DISCOVERY_ENDPOINT_TEMPLATE = "https://{host}:{port}/common/discovery/instance" ;
24+ private static final String INSTANCE_DISCOVERY_REQUEST_PARAMETERS_TEMPLATE = "?api-version=1.1&authorization_endpoint={authorizeEndpoint}" ;
25+ private static final String HOST_TEMPLATE_WITH_REGION = "{region}.login.microsoft.com" ;
26+ private static final String SOVEREIGN_HOST_TEMPLATE_WITH_REGION = "{region}.{host}" ;
27+ private static final String REGION_NAME = "REGION_NAME" ;
28+ private static final int PORT_NOT_SET = -1 ;
2929
3030 // For information of the current api-version refer: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service#versioning
3131 private static final String DEFAULT_API_VERSION = "2020-06-01" ;
@@ -62,11 +62,10 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
6262 boolean validateAuthority ,
6363 MsalRequest msalRequest ,
6464 ServiceBundle serviceBundle ) {
65-
6665 String host = authorityUrl .getHost ();
6766
68- //If instanceDiscovery flag set to false, cache a basic instance metadata entry to skip future lookups
69- if (! msalRequest .application ().instanceDiscovery ()) {
67+ //If instanceDiscovery flag set to false OR this is a managed identity scenario , cache a basic instance metadata entry to skip this and future lookups
68+ if (msalRequest . application () instanceof ManagedIdentityApplication || !(( AbstractClientApplicationBase ) msalRequest .application () ).instanceDiscovery ()) {
7069 if (cache .get (host ) == null ) {
7170 log .debug ("Instance discovery set to false, caching a default entry." );
7271 cacheInstanceDiscoveryMetadata (host );
@@ -75,8 +74,8 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
7574 }
7675
7776 //If a region was set by an app developer or previously found through autodetection, adjust the authority host to use it
78- if (shouldUseRegionalEndpoint (msalRequest ) && msalRequest .application ().azureRegion () != null ) {
79- host = getRegionalizedHost (authorityUrl .getHost (), msalRequest .application ().azureRegion ());
77+ if (shouldUseRegionalEndpoint (msalRequest ) && (( AbstractClientApplicationBase ) msalRequest .application () ).azureRegion () != null ) {
78+ host = getRegionalizedHost (authorityUrl .getHost (), (( AbstractClientApplicationBase ) msalRequest .application () ).azureRegion ());
8079 }
8180
8281 //If there is no cached instance metadata, do instance discovery cache the result
@@ -91,18 +90,18 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
9190
9291 //If region autodetection is enabled and a specific region was not already set, set the application's
9392 // region to the discovered region so that future requests can skip the IMDS endpoint call
94- if (msalRequest .application ().azureRegion () == null
95- && msalRequest .application ().autoDetectRegion ()
93+ if ((( AbstractClientApplicationBase ) msalRequest .application () ).azureRegion () == null
94+ && (( AbstractClientApplicationBase ) msalRequest .application () ).autoDetectRegion ()
9695 && detectedRegion != null ) {
9796 log .debug (String .format ("Region autodetection found %s, this region will be used for future calls." , detectedRegion ));
9897
99- msalRequest .application ().azureRegion = detectedRegion ;
100- host = getRegionalizedHost (authorityUrl .getHost (), msalRequest .application ().azureRegion ());
98+ (( AbstractClientApplicationBase ) msalRequest .application () ).azureRegion = detectedRegion ;
99+ host = getRegionalizedHost (authorityUrl .getHost (), (( AbstractClientApplicationBase ) msalRequest .application () ).azureRegion ());
101100 }
102101
103102 cacheRegionInstanceMetadata (authorityUrl .getHost (), host );
104103 serviceBundle .getServerSideTelemetry ().getCurrentRequest ().regionOutcome (
105- determineRegionOutcome (detectedRegion , msalRequest .application ().azureRegion (), msalRequest .application ().autoDetectRegion ()));
104+ determineRegionOutcome (detectedRegion , (( AbstractClientApplicationBase ) msalRequest .application ()) .azureRegion (), (( AbstractClientApplicationBase ) msalRequest .application () ).autoDetectRegion ()));
106105 }
107106
108107 doInstanceDiscoveryAndCache (authorityUrl , validateAuthority , msalRequest , serviceBundle );
@@ -160,7 +159,8 @@ static void cacheInstanceDiscoveryMetadata(String host) {
160159
161160
162161 private static boolean shouldUseRegionalEndpoint (MsalRequest msalRequest ){
163- if (msalRequest .application ().azureRegion () != null || msalRequest .application ().autoDetectRegion ()){
162+ if (((AbstractClientApplicationBase ) msalRequest .application ()).azureRegion () != null
163+ || ((AbstractClientApplicationBase ) msalRequest .application ()).autoDetectRegion ()){
164164 //This class type check is a quick and dirty fix to accommodate changes to the internal workings of the region API
165165 //
166166 //ESTS-R only supports a small, but growing, number of scenarios, and the original design failed silently whenever
@@ -296,7 +296,7 @@ private static IHttpResponse executeRequest(String requestUrl, Map<String, Strin
296296 requestUrl ,
297297 headers );
298298
299- return HttpHelper .executeHttpRequest (
299+ return serviceBundle . getHttpHelper () .executeHttpRequest (
300300 httpRequest ,
301301 msalRequest .requestContext (),
302302 serviceBundle );
0 commit comments