Skip to content

Add custom authority lookup #1836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/msal-core/src/authority/Authority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class Authority {
if (this.IsValidationEnabled) {
const host = this.canonicalAuthorityUrlComponents.HostNameAndPort;
if (TrustedAuthority.getTrustedHostList().length === 0) {
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(telemetryManager, correlationId);
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(this.canonicalAuthority, telemetryManager, correlationId);
}

if (!TrustedAuthority.IsInTrustedHostList(host)) {
Expand Down
18 changes: 13 additions & 5 deletions lib/msal-core/src/authority/TrustedAuthority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import TelemetryManager from "../telemetry/TelemetryManager";
import { XhrClient, XhrResponse } from "../XHRClient";
import HttpEvent from "../telemetry/HttpEvent";
import { AAD_INSTANCE_DISCOVERY_ENDPOINT, NetworkRequestType } from "../utils/Constants";
import { UrlUtils } from '../utils/UrlUtils';

export class TrustedAuthority {
private static TrustedHostList: Array<string> = [];
Expand All @@ -24,12 +25,13 @@ export class TrustedAuthority {
* @param telemetryManager
* @param correlationId
*/
private static async getAliases(telemetryManager: TelemetryManager, correlationId?: string): Promise<Array<any>> {
private static async getAliases(authorityToVerify: string, telemetryManager: TelemetryManager, correlationId?: string): Promise<Array<any>> {
const client: XhrClient = new XhrClient();

const httpMethod = NetworkRequestType.GET;
const httpEvent: HttpEvent = telemetryManager.createAndStartHttpEvent(correlationId, httpMethod, AAD_INSTANCE_DISCOVERY_ENDPOINT, "getAliases");
return client.sendRequestAsync(AAD_INSTANCE_DISCOVERY_ENDPOINT, httpMethod, true)
const instanceDiscoveryEndpoint = `${AAD_INSTANCE_DISCOVERY_ENDPOINT}${authorityToVerify}oauth2/v2.0/authorize`;
const httpEvent: HttpEvent = telemetryManager.createAndStartHttpEvent(correlationId, httpMethod, instanceDiscoveryEndpoint, "getAliases");
return client.sendRequestAsync(instanceDiscoveryEndpoint, httpMethod, true)
.then((response: XhrResponse) => {
httpEvent.httpResponseStatus = response.statusCode;
telemetryManager.stopEvent(httpEvent);
Expand All @@ -47,14 +49,20 @@ export class TrustedAuthority {
* @param telemetryManager
* @param correlationId
*/
public static async setTrustedAuthoritiesFromNetwork(telemetryManager: TelemetryManager, correlationId?: string): Promise<void> {
const metadata = await this.getAliases(telemetryManager, correlationId);
public static async setTrustedAuthoritiesFromNetwork(authorityToVerify: string, telemetryManager: TelemetryManager, correlationId?: string): Promise<void> {
const metadata = await this.getAliases(authorityToVerify, telemetryManager, correlationId);
metadata.forEach(function(entry: any){
const authorities: Array<string> = entry.aliases;
authorities.forEach(function(authority: string) {
TrustedAuthority.TrustedHostList.push(authority.toLowerCase());
});
});

const host = UrlUtils.GetUrlComponents(authorityToVerify).HostNameAndPort;
if (TrustedAuthority.getTrustedHostList().length && !TrustedAuthority.IsInTrustedHostList(host)){
// Custom Domain scenario, host is trusted because Instance Discovery call succeeded
TrustedAuthority.TrustedHostList.push(host.toLowerCase());
}
}

public static getTrustedHostList(): Array<string> {
Expand Down
4 changes: 2 additions & 2 deletions lib/msal-core/src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export enum ErrorCacheKeys {
ERROR_DESC = "error.description"
}

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

/**
* @hidden
Expand Down
7 changes: 6 additions & 1 deletion lib/msal-core/test/authority/TrustedAuthority.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ describe("TrustedAuthority.ts Class", function () {
describe("setTrustedAuthoritiesFromNetwork", () => {
it("Sets TrustedHostList with Authorities known to Microsoft via Instance Discovery Network Call", async () => {
const countBefore = TrustedAuthority.getTrustedHostList().length;
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(stubbedTelemetryManager);
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(TEST_CONFIG.validAuthority + "/", stubbedTelemetryManager);
const countAfter = TrustedAuthority.getTrustedHostList().length;
expect(countBefore).to.be.lessThan(countAfter);
});

it("Sets TrustedHostList with Custom Domain known to Microsoft via Instance Discovery Network Call", async () => {
await TrustedAuthority.setTrustedAuthoritiesFromNetwork("https://login.windows-ppe.net/common/", stubbedTelemetryManager);
expect(TrustedAuthority.IsInTrustedHostList("login.windows-ppe.net")).to.be.true;
});
});
});