Skip to content

Commit 44b8bc4

Browse files
authored
Merge pull request #2106 from AzureAD/optimize-knownAuthorities
knownAuthorities enhancements
2 parents f3ee40e + 14016c8 commit 44b8bc4

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "patch",
3+
"comment": "knownAuthorities enhancements (#2106)",
4+
"packageName": "@azure/msal-common",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch",
7+
"date": "2020-08-12T17:27:42.806Z"
8+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ export class TrustedAuthority {
7575
*/
7676
public static createCloudDiscoveryMetadataFromKnownAuthorities(knownAuthorities: Array<string>): void {
7777
knownAuthorities.forEach(authority => {
78-
this.TrustedHostList[authority.toLowerCase()] = {
79-
preferred_cache: authority.toLowerCase(),
80-
preferred_network: authority.toLowerCase(),
81-
aliases: [authority.toLowerCase()]
78+
const authorityDomain = UrlString.getDomainFromUrl(authority).toLowerCase();
79+
this.TrustedHostList[authorityDomain] = {
80+
preferred_cache: authorityDomain,
81+
preferred_network: authorityDomain,
82+
aliases: [authorityDomain]
8283
};
8384
});
8485
}

lib/msal-common/src/url/UrlString.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ export class UrlString {
140140
return urlComponents;
141141
}
142142

143+
static getDomainFromUrl(url: string): string {
144+
const regEx = RegExp("^([^:/?#]+://)?([^/?#]*)");
145+
146+
const match = url.match(regEx);
147+
148+
if (!match) {
149+
throw ClientConfigurationError.createUrlParseError(`Given url string: ${url}`);
150+
}
151+
152+
return match[2];
153+
}
154+
143155
/**
144156
* Parses hash string from given string. Returns empty string if no hash symbol is found.
145157
* @param hashString

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe("TrustedAuthority.ts Class", function () {
126126
});
127127
});
128128

129-
it("createcloudDiscoveryMetadataFromKnownAuthorities creates metadata object for each host passed in", () => {
129+
it("createCloudDiscoveryMetadataFromKnownAuthorities creates metadata object for each host passed in", () => {
130130
const testAuthorities = ["contoso.b2clogin.com"]
131131
TrustedAuthority.createCloudDiscoveryMetadataFromKnownAuthorities(testAuthorities);
132132

@@ -140,6 +140,27 @@ describe("TrustedAuthority.ts Class", function () {
140140
expect(savedcloudDiscoveryMetadata.preferred_network).to.eq(testAuthorities[0]);
141141
});
142142

143+
it("createCloudDiscoveryMetadataFromKnownAuthorities creates metadata object when passed host includes more than just domain", () => {
144+
const testDomains = ["contoso1.com", "contoso2.com", "contoso3.com", "contoso4.com", "contoso5.com"]
145+
const testAuthorities = ["https://contoso1.com", "contoso2.com/additionalPage", "contoso3.com#customHash", "contoso4.com?customQueryParam", "http://contoso5.com/additionalPage/anotherPage/"];
146+
TrustedAuthority.createCloudDiscoveryMetadataFromKnownAuthorities(testAuthorities);
147+
148+
testDomains.forEach((authority) => {
149+
expect(TrustedAuthority.IsInTrustedHostList(authority)).to.be.true;
150+
const savedcloudDiscoveryMetadata = TrustedAuthority.getCloudDiscoveryMetadata(authority);
151+
savedcloudDiscoveryMetadata.aliases.forEach((alias) => {
152+
expect(alias).to.eq(authority);
153+
});
154+
expect(savedcloudDiscoveryMetadata.preferred_cache).to.eq(authority);
155+
expect(savedcloudDiscoveryMetadata.preferred_network).to.eq(authority);
156+
});
157+
158+
testAuthorities.forEach((authority) => {
159+
expect(TrustedAuthority.IsInTrustedHostList(authority)).to.be.false;
160+
expect(TrustedAuthority.getCloudDiscoveryMetadata(authority)).to.be.null;
161+
});
162+
});
163+
143164
it("getTrustedHostList", () => {
144165
sinon.stub(TrustedAuthority, <any>"TrustedHostList").get(() => {
145166
return {"fabrikamb2c.b2clogin.com": cloudDiscoveryMetadata[0]}

lib/msal-common/test/url/UrlString.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { UrlString } from "../../src/url/UrlString";
44
import { ClientConfigurationError, ClientConfigurationErrorMessage } from "../../src/error/ClientConfigurationError";
55
import { IUri } from "../../src/url/IUri";
66
import sinon from "sinon";
7+
import { IdToken } from "../../src/account/IdToken";
78

89
describe("UrlString.ts Class Unit Tests", () => {
910

@@ -156,4 +157,32 @@ describe("UrlString.ts Class Unit Tests", () => {
156157
const exampleUnknownHash = "#param1=value1&param2=value2&param3=value3";
157158
expect(UrlString.hashContainsKnownProperties(exampleUnknownHash)).to.be.false;
158159
});
160+
161+
describe("getDomainFromUrl tests", () => {
162+
it("tests domain is returned when provided url includes protocol", () => {
163+
expect(UrlString.getDomainFromUrl("https://domain.com")).to.eq("domain.com");
164+
expect(UrlString.getDomainFromUrl("https://domain.com/")).to.eq("domain.com");
165+
expect(UrlString.getDomainFromUrl("http://domain.com")).to.eq("domain.com");
166+
});
167+
168+
it("tests domain is returned when only domain is provided", () => {
169+
expect(UrlString.getDomainFromUrl("domain.com/")).to.eq("domain.com");
170+
expect(UrlString.getDomainFromUrl("domain.com")).to.eq("domain.com");
171+
});
172+
173+
it("tests domain is returned when provided url is not homepage", () => {
174+
expect(UrlString.getDomainFromUrl("domain.com/page")).to.eq("domain.com");
175+
expect(UrlString.getDomainFromUrl("domain.com/index.html")).to.eq("domain.com");
176+
});
177+
178+
it("tests domain is returned when provided url includes hash", () => {
179+
expect(UrlString.getDomainFromUrl("domain.com#customHash")).to.eq("domain.com");
180+
expect(UrlString.getDomainFromUrl("domain.com/#customHash")).to.eq("domain.com");
181+
});
182+
183+
it("tests domain is returned when provided url includes query string", () => {
184+
expect(UrlString.getDomainFromUrl("domain.com?queryString=1")).to.eq("domain.com");
185+
expect(UrlString.getDomainFromUrl("domain.com/?queryString=1")).to.eq("domain.com");
186+
});
187+
});
159188
});

0 commit comments

Comments
 (0)