Skip to content

Commit 0112055

Browse files
committed
Small improvements
1 parent 30a7804 commit 0112055

3 files changed

Lines changed: 10 additions & 39 deletions

File tree

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AadInstanceDiscoveryProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static AadInstanceDiscoveryResponse sendInstanceDiscoveryRequest(URL authorityUr
246246
AadInstanceDiscoveryResponse response = JsonHelper.convertJsonStringToJsonSerializableObject(httpResponse.body(), AadInstanceDiscoveryResponse::fromJson);
247247

248248
if (httpResponse.statusCode() != HttpStatus.HTTP_OK) {
249-
if (httpResponse.statusCode() == HttpStatus.HTTP_BAD_REQUEST && response.error().equals("invalid_instance")) {
249+
if (httpResponse.statusCode() == HttpStatus.HTTP_BAD_REQUEST && response.error().equals(AuthenticationErrorCode.INVALID_INSTANCE)) {
250250
// instance discovery failed due to an invalid authority, throw an exception.
251251
throw MsalServiceExceptionFactory.fromHttpResponse(httpResponse);
252252
}
@@ -355,10 +355,9 @@ private static void doInstanceDiscoveryAndCache(URL authorityUrl,
355355
try {
356356
aadInstanceDiscoveryResponse = sendInstanceDiscoveryRequest(authorityUrl, msalRequest, serviceBundle);
357357
} catch (MsalServiceException ex) {
358-
// Only propagate "invalid_instance" — this means the authority itself is invalid.
359-
// All other HTTP-level errors (500, 502, 404, etc.) should fall through to the
360-
// fallback path, matching the behavior of MSAL .NET's InstanceDiscoveryManager.
361-
if ("invalid_instance".equals(ex.errorCode())) {
358+
// Throw "invalid_instance" errors: this means the authority itself is invalid.
359+
// All other HTTP-level errors (500, 502, 404, etc.) should fall through to the fallback path.
360+
if (ex.errorCode().equals(AuthenticationErrorCode.INVALID_INSTANCE)) {
362361
throw ex;
363362
}
364363
LOG.warn("Instance discovery request failed with a service error. " +

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AuthenticationErrorCode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,10 @@ public class AuthenticationErrorCode {
160160
public static final String CRYPTO_ERROR = "crypto_error";
161161

162162
public static final String INVALID_TIMESTAMP_FORMAT = "invalid_timestamp_format";
163+
164+
/**
165+
* Indicates that instance discovery failed because the authority is not a valid instance.
166+
* This is returned by the instance discovery endpoint when the provided authority host is unknown.
167+
*/
168+
public static final String INVALID_INSTANCE = "invalid_instance";
163169
}

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/KnownMetadataProviderTest.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,6 @@ void getMetadataEntry_publicCloudAliases_resolvesToSameEntry() {
6060
assertSame(entry3, entry4);
6161
}
6262

63-
@Test
64-
void getMetadataEntry_chinaCloud_returnsCorrectAliases() {
65-
// Act
66-
InstanceDiscoveryMetadataEntry entry = KnownMetadataProvider.getMetadataEntry("login.chinacloudapi.cn");
67-
68-
// Assert
69-
assertNotNull(entry);
70-
assertEquals("login.partner.microsoftonline.cn", entry.preferredNetwork());
71-
assertEquals("login.partner.microsoftonline.cn", entry.preferredCache());
72-
assertEquals(2, entry.aliases().size());
73-
assertTrue(entry.aliases().contains("login.partner.microsoftonline.cn"));
74-
assertTrue(entry.aliases().contains("login.chinacloudapi.cn"));
75-
76-
// Both aliases should resolve to the same entry
77-
assertSame(entry, KnownMetadataProvider.getMetadataEntry("login.partner.microsoftonline.cn"));
78-
}
79-
80-
@Test
81-
void getMetadataEntry_usGov_returnsCorrectAliases() {
82-
// Act
83-
InstanceDiscoveryMetadataEntry entry = KnownMetadataProvider.getMetadataEntry("login.microsoftonline.us");
84-
85-
// Assert
86-
assertNotNull(entry);
87-
assertEquals("login.microsoftonline.us", entry.preferredNetwork());
88-
assertEquals("login.microsoftonline.us", entry.preferredCache());
89-
assertEquals(2, entry.aliases().size());
90-
assertTrue(entry.aliases().contains("login.microsoftonline.us"));
91-
assertTrue(entry.aliases().contains("login.usgovcloudapi.net"));
92-
93-
// Both aliases should resolve to the same entry
94-
assertSame(entry, KnownMetadataProvider.getMetadataEntry("login.usgovcloudapi.net"));
95-
}
96-
9763
@Test
9864
void getMetadataEntry_unknownHost_returnsNull() {
9965
assertNull(KnownMetadataProvider.getMetadataEntry("custom.authority.example.com"));

0 commit comments

Comments
 (0)