Skip to content

Commit 502127a

Browse files
committed
Fix test issue caused by static HttpClient and cleanup validation logic
1 parent 10e66ff commit 502127a

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class OidcAuthority extends Authority {
1010
//Part of the OpenIdConnect standard, this is appended to the authority to create the endpoint that has OIDC metadata
1111
static final String WELL_KNOWN_OPENID_CONFIGURATION = ".well-known/openid-configuration";
1212
private static final String AUTHORITY_FORMAT = "https://%s/%s/";
13+
private static final String CIAM_AUTHORITY_FORMAT = "https://%s.ciamlogin.com/%s";
14+
1315
String issuerFromOidcDiscovery;
1416

1517
OidcAuthority(URL authorityUrl) throws MalformedURLException {
@@ -45,32 +47,25 @@ boolean isIssuerValid() {
4547
return false;
4648
}
4749

48-
// Normalize issuer by removing trailing slashes
49-
String normalizedIssuer = issuerFromOidcDiscovery;
50-
while (normalizedIssuer.endsWith("/")) {
51-
normalizedIssuer = normalizedIssuer.substring(0, normalizedIssuer.length() - 1);
52-
}
53-
5450
// Case 1: Check against canonicalAuthorityUrl without the well-known segment
5551
String authorityWithoutWellKnown = canonicalAuthorityUrl.toString();
5652
if (authorityWithoutWellKnown.endsWith(WELL_KNOWN_OPENID_CONFIGURATION)) {
5753
authorityWithoutWellKnown = authorityWithoutWellKnown.substring(0,
5854
authorityWithoutWellKnown.length() - WELL_KNOWN_OPENID_CONFIGURATION.length());
5955

60-
// Remove trailing slash if present
61-
if (authorityWithoutWellKnown.endsWith("/")) {
62-
authorityWithoutWellKnown = authorityWithoutWellKnown.substring(0, authorityWithoutWellKnown.length() - 1);
63-
}
56+
// Normalize both URLs to ensure consistent comparison
57+
String normalizedAuthority = Authority.enforceTrailingSlash(authorityWithoutWellKnown);
58+
String normalizedIssuer = Authority.enforceTrailingSlash(issuerFromOidcDiscovery);
6459

65-
if (normalizedIssuer.equals(authorityWithoutWellKnown)) {
60+
if (normalizedIssuer.equals(normalizedAuthority)) {
6661
return true;
6762
}
6863
}
6964

70-
// Case 2: Check CIAM format: "https://{tenant}.ciamlogin.com/{tenant}/"
71-
if (tenant != null && !tenant.isEmpty()) {
72-
String ciamPattern = "https://" + tenant + ".ciamlogin.com/" + tenant;
73-
return normalizedIssuer.startsWith(ciamPattern);
65+
// Case 2: Check CIAM format: "https://{tenant}.ciamlogin.com/{tenant}"
66+
if (!StringHelper.isNullOrBlank(tenant)) {
67+
String ciamPattern = String.format(CIAM_AUTHORITY_FORMAT, tenant, tenant);
68+
return issuerFromOidcDiscovery.startsWith(ciamPattern);
7469
}
7570

7671
return false;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,9 @@ static void setHttpClient(IHttpClient client) {
124124
httpClient = client;
125125
httpHelper = new HttpHelper(httpClient, new ManagedIdentityRetryPolicy());
126126
}
127+
128+
static void resetHttpClient() {
129+
httpClient = new DefaultHttpClientManagedIdentity(null, null, null, null);
130+
httpHelper = new HttpHelper(httpClient, new ManagedIdentityRetryPolicy());
131+
}
127132
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ static void resetRetryPolicies() {
5252
IMDSRetryPolicy.resetToDefaults();
5353
}
5454

55+
@AfterAll
56+
static void resetServiceFabricHttpClient() {
57+
ServiceFabricManagedIdentitySource.resetHttpClient();
58+
}
59+
5560
private String getSuccessfulResponse(String resource) {
5661
long expiresOn = (System.currentTimeMillis() / 1000) + (24 * 3600);//A long-lived, 24 hour token
5762
return "{\"access_token\":\"accesstoken\",\"expires_on\":\"" + expiresOn + "\",\"resource\":\"" + resource + "\",\"token_type\":" +

0 commit comments

Comments
 (0)