Skip to content

Commit cf22677

Browse files
committed
Fix issue with overriding tenant in B2C authority
1 parent 305d209 commit cf22677

File tree

4 files changed

+45
-30
lines changed

4 files changed

+45
-30
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ AuthenticationResult execute() throws Exception {
5252
requestAuthority = clientApplication.authenticationAuthority;
5353
}
5454

55-
if (requestAuthority.authorityType == AuthorityType.AAD) {
56-
requestAuthority = getAuthorityWithPrefNetworkHost(requestAuthority.authority());
57-
}
55+
requestAuthority = getAuthorityWithPrefNetworkHost(requestAuthority.authority());
5856

5957
try {
6058
return clientApplication.acquireTokenCommon(msalRequest, requestAuthority);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ class AcquireTokenSilentSupplier extends AuthenticationResultSupplier {
2424
@Override
2525
AuthenticationResult execute() throws Exception {
2626
boolean shouldRefresh;
27-
Authority requestAuthority = silentRequest.requestAuthority();
28-
if (requestAuthority.authorityType != AuthorityType.B2C) {
29-
requestAuthority =
30-
getAuthorityWithPrefNetworkHost(silentRequest.requestAuthority().authority());
31-
}
27+
Authority requestAuthority = getAuthorityWithPrefNetworkHost(silentRequest.requestAuthority().authority());
3228

3329
AuthenticationResult res;
3430
if (silentRequest.parameters().account() == null) {

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,31 +138,12 @@ static void validateAuthority(URL authorityUrl) {
138138
* @param originalAuthority The original authority to base the new one on
139139
* @param newTenant The new tenant to use in the authority URL
140140
* @return A new Authority instance with the specified tenant
141-
* @throws MalformedURLException If the new authority URL is invalid
142-
* @throws NullPointerException If originalAuthority or newTenant is null
143141
*/
144142
static Authority replaceTenant(Authority originalAuthority, String newTenant) throws MalformedURLException {
145-
if (originalAuthority == null) {
146-
throw new NullPointerException("originalAuthority");
147-
}
148-
if (StringHelper.isBlank(newTenant)) {
149-
throw new NullPointerException("newTenant");
150-
}
151-
152-
URL originalUrl = originalAuthority.canonicalAuthorityUrl();
153-
String host = originalUrl.getHost();
154-
String protocol = originalUrl.getProtocol();
155-
int port = originalUrl.getPort();
156-
157-
// Build path with new tenant
158-
String newAuthority = String.format("%s://%s%s/%s/",
159-
protocol,
160-
host,
161-
(port == -1 ? "" : ":" + port),
162-
newTenant);
143+
String authorityString = originalAuthority.canonicalAuthorityUrl().toString();
144+
authorityString = authorityString.replace(originalAuthority.tenant, newTenant);
163145

164-
// Create proper authority instance with the tenant-specific URL
165-
return createAuthority(new URL(newAuthority));
146+
return createAuthority(new URL(authorityString));
166147
}
167148

168149
static String getTenant(URL authorityUrl, AuthorityType authorityType) {

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,46 @@ void testClientCertificate_TenantOverride() throws Exception {
243243
"Access tokens should differ when using different tenants");
244244
}
245245

246+
@Test
247+
void testClientCertificate_TenantOverride_B2C() throws Exception {
248+
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
249+
String replacementTenant = "overrideTenant";
250+
251+
ConfidentialClientApplication cca =
252+
ConfidentialClientApplication.builder("clientId", ClientCredentialFactory.createFromCertificate(TestHelper.getPrivateKey(), TestHelper.getX509Cert()))
253+
.b2cAuthority(TestConfiguration.B2C_AUTHORITY)
254+
.instanceDiscovery(false)
255+
.validateAuthority(false)
256+
.httpClient(httpClientMock)
257+
.build();
258+
259+
when(httpClientMock.send(any(HttpRequest.class))).thenAnswer(parameters -> {
260+
HttpRequest request = parameters.getArgument(0);
261+
String requestBody = request.body();
262+
String url = request.url().toString();
263+
264+
// Extract the assertion to verify its audience claim
265+
String clientAssertion = extractClientAssertion(requestBody);
266+
267+
if (clientAssertion != null && url.contains(replacementTenant)) {
268+
HashMap<String, String> tokenResponseValues = new HashMap<>();
269+
tokenResponseValues.put("access_token", "access_token_for_" + replacementTenant);
270+
return TestHelper.expectedResponse(200, TestHelper.getSuccessfulTokenResponse(tokenResponseValues));
271+
}
272+
273+
return null;
274+
});
275+
276+
ClientCredentialParameters overrideParameters = ClientCredentialParameters.builder(Collections.singleton("scopes"))
277+
.skipCache(true)
278+
.tenant(replacementTenant)
279+
.build();
280+
IAuthenticationResult result = cca.acquireToken(overrideParameters).get();
281+
282+
assertNotNull(result);
283+
assertEquals("access_token_for_"+ replacementTenant, result.accessToken());
284+
}
285+
246286
/**
247287
* Extracts the tenant name from an authority URL
248288
* @param url The full URL containing the tenant

0 commit comments

Comments
 (0)