18
18
19
19
class AadInstanceDiscoveryProvider {
20
20
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 ;
29
29
30
30
// For information of the current api-version refer: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service#versioning
31
31
private static final String DEFAULT_API_VERSION = "2020-06-01" ;
@@ -64,8 +64,8 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
64
64
ServiceBundle serviceBundle ) {
65
65
String host = authorityUrl .getHost ();
66
66
67
- //If instanceDiscovery flag set to false, cache a basic instance metadata entry to skip future lookups
68
- 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 ()) {
69
69
if (cache .get (host ) == null ) {
70
70
log .debug ("Instance discovery set to false, caching a default entry." );
71
71
cacheInstanceDiscoveryMetadata (host );
@@ -74,8 +74,8 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
74
74
}
75
75
76
76
//If a region was set by an app developer or previously found through autodetection, adjust the authority host to use it
77
- if (shouldUseRegionalEndpoint (msalRequest ) && msalRequest .application ().azureRegion () != null ) {
78
- 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 ());
79
79
}
80
80
81
81
//If there is no cached instance metadata, do instance discovery cache the result
@@ -90,18 +90,18 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
90
90
91
91
//If region autodetection is enabled and a specific region was not already set, set the application's
92
92
// region to the discovered region so that future requests can skip the IMDS endpoint call
93
- if (msalRequest .application ().azureRegion () == null
94
- && msalRequest .application ().autoDetectRegion ()
93
+ if ((( AbstractClientApplicationBase ) msalRequest .application () ).azureRegion () == null
94
+ && (( AbstractClientApplicationBase ) msalRequest .application () ).autoDetectRegion ()
95
95
&& detectedRegion != null ) {
96
96
log .debug (String .format ("Region autodetection found %s, this region will be used for future calls." , detectedRegion ));
97
97
98
- msalRequest .application ().azureRegion = detectedRegion ;
99
- host = getRegionalizedHost (authorityUrl .getHost (), msalRequest .application ().azureRegion ());
98
+ (( AbstractClientApplicationBase ) msalRequest .application () ).azureRegion = detectedRegion ;
99
+ host = getRegionalizedHost (authorityUrl .getHost (), (( AbstractClientApplicationBase ) msalRequest .application () ).azureRegion ());
100
100
}
101
101
102
102
cacheRegionInstanceMetadata (authorityUrl .getHost (), host );
103
103
serviceBundle .getServerSideTelemetry ().getCurrentRequest ().regionOutcome (
104
- determineRegionOutcome (detectedRegion , msalRequest .application ().azureRegion (), msalRequest .application ().autoDetectRegion ()));
104
+ determineRegionOutcome (detectedRegion , (( AbstractClientApplicationBase ) msalRequest .application ()) .azureRegion (), (( AbstractClientApplicationBase ) msalRequest .application () ).autoDetectRegion ()));
105
105
}
106
106
107
107
doInstanceDiscoveryAndCache (authorityUrl , validateAuthority , msalRequest , serviceBundle );
0 commit comments