Skip to content

Commit 118c24d

Browse files
authored
Merge pull request #1836 from AzureAD/custom-domain-msal-core
Add custom authority lookup
2 parents 532a9a4 + e0159f8 commit 118c24d

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

lib/msal-core/src/authority/Authority.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class Authority {
146146
if (this.IsValidationEnabled) {
147147
const host = this.canonicalAuthorityUrlComponents.HostNameAndPort;
148148
if (TrustedAuthority.getTrustedHostList().length === 0) {
149-
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(telemetryManager, correlationId);
149+
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(this.canonicalAuthority, telemetryManager, correlationId);
150150
}
151151

152152
if (!TrustedAuthority.IsInTrustedHostList(host)) {

lib/msal-core/src/authority/TrustedAuthority.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import TelemetryManager from "../telemetry/TelemetryManager";
22
import { XhrClient, XhrResponse } from "../XHRClient";
33
import HttpEvent from "../telemetry/HttpEvent";
44
import { AAD_INSTANCE_DISCOVERY_ENDPOINT, NetworkRequestType } from "../utils/Constants";
5+
import { UrlUtils } from '../utils/UrlUtils';
56

67
export class TrustedAuthority {
78
private static TrustedHostList: Array<string> = [];
@@ -24,12 +25,13 @@ export class TrustedAuthority {
2425
* @param telemetryManager
2526
* @param correlationId
2627
*/
27-
private static async getAliases(telemetryManager: TelemetryManager, correlationId?: string): Promise<Array<any>> {
28+
private static async getAliases(authorityToVerify: string, telemetryManager: TelemetryManager, correlationId?: string): Promise<Array<any>> {
2829
const client: XhrClient = new XhrClient();
2930

3031
const httpMethod = NetworkRequestType.GET;
31-
const httpEvent: HttpEvent = telemetryManager.createAndStartHttpEvent(correlationId, httpMethod, AAD_INSTANCE_DISCOVERY_ENDPOINT, "getAliases");
32-
return client.sendRequestAsync(AAD_INSTANCE_DISCOVERY_ENDPOINT, httpMethod, true)
32+
const instanceDiscoveryEndpoint = `${AAD_INSTANCE_DISCOVERY_ENDPOINT}${authorityToVerify}oauth2/v2.0/authorize`;
33+
const httpEvent: HttpEvent = telemetryManager.createAndStartHttpEvent(correlationId, httpMethod, instanceDiscoveryEndpoint, "getAliases");
34+
return client.sendRequestAsync(instanceDiscoveryEndpoint, httpMethod, true)
3335
.then((response: XhrResponse) => {
3436
httpEvent.httpResponseStatus = response.statusCode;
3537
telemetryManager.stopEvent(httpEvent);
@@ -47,14 +49,20 @@ export class TrustedAuthority {
4749
* @param telemetryManager
4850
* @param correlationId
4951
*/
50-
public static async setTrustedAuthoritiesFromNetwork(telemetryManager: TelemetryManager, correlationId?: string): Promise<void> {
51-
const metadata = await this.getAliases(telemetryManager, correlationId);
52+
public static async setTrustedAuthoritiesFromNetwork(authorityToVerify: string, telemetryManager: TelemetryManager, correlationId?: string): Promise<void> {
53+
const metadata = await this.getAliases(authorityToVerify, telemetryManager, correlationId);
5254
metadata.forEach(function(entry: any){
5355
const authorities: Array<string> = entry.aliases;
5456
authorities.forEach(function(authority: string) {
5557
TrustedAuthority.TrustedHostList.push(authority.toLowerCase());
5658
});
5759
});
60+
61+
const host = UrlUtils.GetUrlComponents(authorityToVerify).HostNameAndPort;
62+
if (TrustedAuthority.getTrustedHostList().length && !TrustedAuthority.IsInTrustedHostList(host)){
63+
// Custom Domain scenario, host is trusted because Instance Discovery call succeeded
64+
TrustedAuthority.TrustedHostList.push(host.toLowerCase());
65+
}
5866
}
5967

6068
public static getTrustedHostList(): Array<string> {

lib/msal-core/src/utils/Constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ export enum ErrorCacheKeys {
102102
ERROR_DESC = "error.description"
103103
}
104104

105-
export const DEFAULT_AUTHORITY: string = "https://login.microsoftonline.com/common";
106-
export const AAD_INSTANCE_DISCOVERY_ENDPOINT: string = `${DEFAULT_AUTHORITY}/discovery/instance?api-version=1.1&authorization_endpoint=${DEFAULT_AUTHORITY}/oauth2/v2.0/authorize`;
105+
export const DEFAULT_AUTHORITY: string = "https://login.microsoftonline.com/common/";
106+
export const AAD_INSTANCE_DISCOVERY_ENDPOINT: string = `${DEFAULT_AUTHORITY}/discovery/instance?api-version=1.1&authorization_endpoint=`;
107107

108108
/**
109109
* @hidden

lib/msal-core/test/authority/TrustedAuthority.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ describe("TrustedAuthority.ts Class", function () {
4242
describe("setTrustedAuthoritiesFromNetwork", () => {
4343
it("Sets TrustedHostList with Authorities known to Microsoft via Instance Discovery Network Call", async () => {
4444
const countBefore = TrustedAuthority.getTrustedHostList().length;
45-
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(stubbedTelemetryManager);
45+
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(TEST_CONFIG.validAuthority + "/", stubbedTelemetryManager);
4646
const countAfter = TrustedAuthority.getTrustedHostList().length;
4747
expect(countBefore).to.be.lessThan(countAfter);
4848
});
49+
50+
it("Sets TrustedHostList with Custom Domain known to Microsoft via Instance Discovery Network Call", async () => {
51+
await TrustedAuthority.setTrustedAuthoritiesFromNetwork("https://login.windows-ppe.net/common/", stubbedTelemetryManager);
52+
expect(TrustedAuthority.IsInTrustedHostList("login.windows-ppe.net")).to.be.true;
53+
});
4954
});
5055
});

0 commit comments

Comments
 (0)