Repository navigation
Expand file tree
/
Copy pathenterpriseOAuthTarget.ts
More file actions
66 lines (59 loc) · 1.89 KB
/
Copy pathenterpriseOAuthTarget.ts
File metadata and controls
66 lines (59 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
export function isSupportedEnterpriseOAuthTarget(baseUrl: string): boolean {
try {
const url = new URL(baseUrl);
const hostname = url.hostname.toLowerCase();
const authority = readHttpsAuthority(baseUrl);
return (
authority !== null &&
authority.length > 0 &&
url.protocol === "https:" &&
isDnsDomain(hostname) &&
!isStackOverflowTeamsHostname(hostname) &&
!authority.includes("@") &&
!hasExplicitPort(authority) &&
url.username === "" &&
url.password === "" &&
url.port === ""
);
} catch {
return false;
}
}
export function normalizeOAuthBaseUrl(baseUrl: string): string {
const url = new URL(baseUrl);
return `${url.protocol}//${url.host}`;
}
function readHttpsAuthority(value: string): string | null {
return value.trim().match(/^https:\/\/([^/?#]*)/i)?.[1] ?? null;
}
function hasExplicitPort(authority: string): boolean {
if (authority.startsWith("[")) {
const closingBracket = authority.indexOf("]");
return closingBracket >= 0 && authority.slice(closingBracket + 1).startsWith(":");
}
return authority.includes(":");
}
function isStackOverflowTeamsHostname(hostname: string): boolean {
const canonicalHostname = hostname.replace(/\.$/, "");
return (
canonicalHostname === "stackoverflowteams.com" ||
canonicalHostname.endsWith(".stackoverflowteams.com")
);
}
function isDnsDomain(hostname: string): boolean {
const canonicalHostname = hostname.replace(/\.$/, "");
if (
!canonicalHostname.includes(".") ||
canonicalHostname.endsWith(".localhost") ||
canonicalHostname.startsWith("[") ||
/^\d{1,3}(?:\.\d{1,3}){3}$/.test(canonicalHostname)
) {
return false;
}
return canonicalHostname.length <= 253 && canonicalHostname.split(".").every(
(label) =>
label.length >= 1 &&
label.length <= 63 &&
/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(label),
);
}