@oxyhq/core 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Auto-detect the FAPI (IdP) URL from the current browser hostname.
3
+ *
4
+ * This is the canonical cross-domain IdP-resolution primitive for the Oxy
5
+ * ecosystem. Both candidate cross-domain SSO designs derive `auth.<rp-apex>`
6
+ * through this helper; do not fork it.
7
+ *
8
+ * Clerk-style multi-domain SSO depends on the IdP being reachable on a
9
+ * subdomain of the RP's own apex (e.g. `auth.mention.earth` CNAMEd to the
10
+ * central Oxy IdP). That way every FedCM endpoint, the session cookie,
11
+ * and any popup/redirect target are same-site with the RP — the only way
12
+ * to get first-party cookies in Safari ITP and Firefox Total Cookie
13
+ * Protection.
14
+ *
15
+ * This helper computes `https://auth.<rp-apex>` from
16
+ * `window.location.hostname` so a consuming app doesn't have to pass
17
+ * `authWebUrl` explicitly. Returns `undefined` for environments where
18
+ * auto-detection would be wrong:
19
+ *
20
+ * - SSR / non-browser (no `window`).
21
+ * - `localhost`, `127.0.0.1`, IPv4/IPv6 literals.
22
+ * - Hostnames with fewer than two labels.
23
+ * - Hostnames whose trailing two labels form a known multi-part public
24
+ * suffix (e.g. `co.uk`), where the naive `labels.slice(-2)` apex would be
25
+ * an attacker-registrable suffix like `auth.co.uk` rather than the real
26
+ * registrable domain.
27
+ *
28
+ * When the page is already loaded ON the IdP itself (`auth.<anything>`),
29
+ * the helper returns the current origin so the SDK keeps everything
30
+ * same-origin instead of hopping to a different IdP host.
31
+ *
32
+ * The IdP backend independently derives `iss`, `provider_urls`, and the
33
+ * `fedcm.json` icon URLs from the request host
34
+ * (`packages/auth/server/index.ts`), so an honest CNAME pair is all that
35
+ * is required for end-to-end FedCM correctness — no per-RP config.
36
+ */
37
+
38
+ /**
39
+ * Known multi-part public suffixes where the registrable domain is the LAST
40
+ * THREE labels, not two. Deriving an apex from `labels.slice(-2)` against any
41
+ * of these would yield an attacker-registrable suffix (e.g. `auth.co.uk`),
42
+ * so we bail out instead.
43
+ *
44
+ * This is intentionally a small, explicit allow-list rather than the full
45
+ * Public Suffix List — it covers the suffixes the Oxy ecosystem's RPs use.
46
+ * Any multi-part-TLD RP MUST extend this set (or wire in a proper PSL check)
47
+ * before relying on this helper, otherwise auto-detection silently bails to
48
+ * `undefined` and the consumer must pass `authWebUrl` explicitly.
49
+ */
50
+ const MULTIPART_TLDS: ReadonlySet<string> = new Set([
51
+ 'co.uk',
52
+ 'com.au',
53
+ 'co.jp',
54
+ 'co.nz',
55
+ 'com.br',
56
+ 'co.za',
57
+ 'com.mx',
58
+ 'co.in',
59
+ 'co.kr',
60
+ 'com.sg',
61
+ ]);
62
+
63
+ export function autoDetectAuthWebUrl(
64
+ location: Pick<Location, 'hostname' | 'protocol'> | undefined =
65
+ typeof window !== 'undefined' ? window.location : undefined
66
+ ): string | undefined {
67
+ if (!location) return undefined;
68
+ const { hostname, protocol } = location;
69
+ if (!hostname) return undefined;
70
+ if (protocol !== 'https:' && protocol !== 'http:') return undefined;
71
+ if (hostname === 'localhost' || hostname === '127.0.0.1') return undefined;
72
+ if (/^\d+\.\d+\.\d+\.\d+$/.test(hostname)) return undefined;
73
+ if (hostname.startsWith('[')) return undefined;
74
+ if (hostname.startsWith('auth.')) {
75
+ return `${protocol}//${hostname}`;
76
+ }
77
+ const labels = hostname.split('.');
78
+ if (labels.length < 2) return undefined;
79
+ if (MULTIPART_TLDS.has(labels.slice(-2).join('.'))) return undefined;
80
+ const apex = labels.slice(-2).join('.');
81
+ return `${protocol}//auth.${apex}`;
82
+ }