Skip to content

Commit 06c901b

Browse files
committed
Use public suffix list for auth site labels
1 parent fb88870 commit 06c901b

5 files changed

Lines changed: 29 additions & 35 deletions

File tree

‎.changeset/tidy-domains-fix.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@onkernel/managed-auth-react": patch
3+
---
4+
5+
Use the public suffix list to derive the site label shown on the initial auth screen.

‎bun.lock‎

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/managed-auth-react/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"react-dom": ">=18"
5555
},
5656
"dependencies": {
57-
"clsx": "^2.1.1"
57+
"clsx": "^2.1.1",
58+
"tldts": "7.4.15"
5859
},
5960
"devDependencies": {
6061
"@types/react": "^18",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { extractPrimaryDomainLabel } from "./profile-name";
3+
4+
describe("extractPrimaryDomainLabel", () => {
5+
test("uses the registrable domain for subdomains of multi-label suffixes", () => {
6+
expect(extractPrimaryDomainLabel("login.clalit.co.il")).toBe("clalit");
7+
expect(extractPrimaryDomainLabel("auth.example.co.uk")).toBe("example");
8+
});
9+
10+
test("uses the registrable domain for regular suffixes", () => {
11+
expect(extractPrimaryDomainLabel("login.example.com")).toBe("example");
12+
expect(extractPrimaryDomainLabel("example.com")).toBe("example");
13+
});
14+
});
Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
1-
const MULTI_PART_TLDS = new Set([
2-
"co.uk",
3-
"co.jp",
4-
"co.kr",
5-
"co.nz",
6-
"co.za",
7-
"co.in",
8-
"co.id",
9-
"co.th",
10-
"com.au",
11-
"com.br",
12-
"com.cn",
13-
"com.mx",
14-
"com.tw",
15-
"com.sg",
16-
"com.ar",
17-
"com.co",
18-
"com.tr",
19-
"org.uk",
20-
"org.au",
21-
"net.au",
22-
"ac.uk",
23-
"gov.uk",
24-
"ne.jp",
25-
"or.jp",
26-
]);
1+
import { parse } from "tldts";
272

283
export function extractDomainName(targetDomain: string): string {
294
return targetDomain
@@ -33,12 +8,6 @@ export function extractDomainName(targetDomain: string): string {
338
}
349

3510
export function extractPrimaryDomainLabel(hostname: string): string {
36-
const parts = hostname.split(".");
37-
if (parts.length <= 1) return parts[0] || hostname;
38-
if (parts.length === 2) return parts[0];
39-
const lastTwo = parts.slice(-2).join(".");
40-
const tldLength = MULTI_PART_TLDS.has(lastTwo.toLowerCase()) ? 2 : 1;
41-
const primaryIndex = parts.length - tldLength - 1;
42-
if (primaryIndex >= 0) return parts[primaryIndex];
43-
return parts[0];
11+
const domain = parse(hostname).domain;
12+
return domain ? domain.split(".")[0] : hostname.split(".")[0] || hostname;
4413
}

0 commit comments

Comments
 (0)