@oxyhq/core 2.1.1 → 2.2.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.
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/OxyServices.base.js +15 -3
- package/dist/cjs/index.js +9 -1
- package/dist/cjs/mixins/OxyServices.sso.js +142 -0
- package/dist/cjs/mixins/index.js +4 -0
- package/dist/cjs/utils/authWebUrl.js +37 -0
- package/dist/cjs/utils/ssoReturn.js +72 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/OxyServices.base.js +15 -3
- package/dist/esm/index.js +4 -0
- package/dist/esm/mixins/OxyServices.sso.js +138 -0
- package/dist/esm/mixins/index.js +4 -0
- package/dist/esm/utils/authWebUrl.js +33 -0
- package/dist/esm/utils/ssoReturn.js +69 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/OxyServices.d.ts +2 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/mixins/OxyServices.assets.d.ts +1 -3
- package/dist/types/mixins/OxyServices.sso.d.ts +111 -0
- package/dist/types/mixins/index.d.ts +2 -1
- package/dist/types/utils/authWebUrl.d.ts +31 -0
- package/dist/types/utils/ssoReturn.d.ts +46 -0
- package/package.json +1 -1
- package/src/OxyServices.base.ts +17 -3
- package/src/OxyServices.ts +4 -0
- package/src/index.ts +7 -0
- package/src/mixins/OxyServices.sso.ts +172 -0
- package/src/mixins/__tests__/constructorAuthWebUrl.test.ts +85 -0
- package/src/mixins/__tests__/sso.test.ts +146 -0
- package/src/mixins/index.ts +6 -0
- package/src/utils/__tests__/authWebUrl.test.ts +40 -0
- package/src/utils/__tests__/ssoReturn.test.ts +120 -0
- package/src/utils/authWebUrl.ts +35 -0
- package/src/utils/ssoReturn.ts +94 -0
|
@@ -7,6 +7,7 @@ import { jwtDecode } from 'jwt-decode';
|
|
|
7
7
|
import { handleHttpError } from './utils/errorUtils.js';
|
|
8
8
|
import { HttpService } from './HttpService.js';
|
|
9
9
|
import { OxyAuthenticationError, OxyAuthenticationTimeoutError } from './OxyServices.errors.js';
|
|
10
|
+
import { resolveCentralAuthUrl } from './utils/authWebUrl.js';
|
|
10
11
|
/**
|
|
11
12
|
* Base class for OxyServices with core infrastructure
|
|
12
13
|
*/
|
|
@@ -18,10 +19,21 @@ export class OxyServicesBase {
|
|
|
18
19
|
if (!config || typeof config !== 'object') {
|
|
19
20
|
throw new Error('OxyConfig is required');
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
// Default `authWebUrl` to the CENTRAL IdP (`auth.oxy.so`) when the caller
|
|
23
|
+
// did not pin it explicitly. TRUE central cross-domain SSO (Google/Meta/
|
|
24
|
+
// Clerk style) routes every RP through the one central IdP — it owns the
|
|
25
|
+
// host-only `fedcm_session` cookie and the central session store — so the
|
|
26
|
+
// SDK no longer derives a per-apex `auth.<rp-apex>` IdP by default.
|
|
27
|
+
// `autoDetectAuthWebUrl` is still exported for any call site that opts into
|
|
28
|
+
// per-apex resolution, but it is NOT the constructor default anymore.
|
|
29
|
+
// An explicit `authWebUrl` always wins (we only fill it when absent).
|
|
30
|
+
const resolvedConfig = config.authWebUrl
|
|
31
|
+
? config
|
|
32
|
+
: { ...config, authWebUrl: resolveCentralAuthUrl(config.authWebUrl) };
|
|
33
|
+
this.config = resolvedConfig;
|
|
34
|
+
this.cloudURL = resolvedConfig.cloudURL || 'https://cloud.oxy.so';
|
|
23
35
|
// Initialize unified HTTP service (handles auth, caching, deduplication, queuing, retry)
|
|
24
|
-
this.httpService = new HttpService(
|
|
36
|
+
this.httpService = new HttpService(resolvedConfig);
|
|
25
37
|
}
|
|
26
38
|
// Test-only utility to reset tokens on this instance between jest tests
|
|
27
39
|
// Note: tokens are now per-instance, so create new instances in tests for isolation
|
package/dist/esm/index.js
CHANGED
|
@@ -100,6 +100,10 @@ export { buildAccountsArray, createQuickAccount, getAccountDisplayName, getAccou
|
|
|
100
100
|
// Cross-domain SSO infrastructure
|
|
101
101
|
// ---------------------------------------------------------------------------
|
|
102
102
|
export { autoDetectAuthWebUrl } from './utils/fapiAutoDetect.js';
|
|
103
|
+
// Central cross-domain SSO (opaque single-use code bounce via auth.oxy.so)
|
|
104
|
+
export { CENTRAL_AUTH_URL, resolveCentralAuthUrl } from './utils/authWebUrl.js';
|
|
105
|
+
export { parseSsoReturnFragment } from './utils/ssoReturn.js';
|
|
106
|
+
export { generateSsoState } from './mixins/OxyServices.sso.js';
|
|
103
107
|
export { runColdBoot } from './utils/coldBoot.js';
|
|
104
108
|
// ---------------------------------------------------------------------------
|
|
105
109
|
// Constants
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Central Cross-Domain SSO (opaque-code) Mixin
|
|
3
|
+
*
|
|
4
|
+
* Implements the Relying-Party half of TRUE central cross-domain SSO
|
|
5
|
+
* (Google/Meta/Clerk style). The central IdP at `auth.oxy.so` owns the session;
|
|
6
|
+
* an RP bounces a top-level redirect (prompt=none) to `auth.oxy.so/sso`, which
|
|
7
|
+
* returns an OPAQUE single-use code in the redirect fragment. The RP then
|
|
8
|
+
* exchanges that code here for the real session.
|
|
9
|
+
*
|
|
10
|
+
* Security properties:
|
|
11
|
+
* - NO token/JWT ever travels in a URL — only the opaque code does. The real
|
|
12
|
+
* `accessToken` is delivered exclusively in this exchange response body.
|
|
13
|
+
* - The exchange is a CORS POST with NO credentials/cookies — the opaque code
|
|
14
|
+
* is the only bearer of authority, and the central store burns it atomically
|
|
15
|
+
* (single-use). Sending no cookies keeps the request a clean, ambient-
|
|
16
|
+
* authority-free bearer exchange that the central `POST /sso/exchange`
|
|
17
|
+
* endpoint validates by `Origin` against the code's bound `clientOrigin`.
|
|
18
|
+
* - The code is minted server-side bound to the RP origin and expires in
|
|
19
|
+
* seconds, so a leaked code is useless cross-origin and short-lived.
|
|
20
|
+
*
|
|
21
|
+
* On success the mixin plants the returned access token via
|
|
22
|
+
* `httpService.setTokens(...)` — mirroring `exchangeIdTokenForSession` /
|
|
23
|
+
* `verifyChallenge` — so callers do NOT need to plant tokens manually.
|
|
24
|
+
*/
|
|
25
|
+
import { createDebugLogger } from '../shared/utils/debugUtils.js';
|
|
26
|
+
const debug = createDebugLogger('SSO');
|
|
27
|
+
/**
|
|
28
|
+
* Generate a cryptographically secure state value for the SSO bounce.
|
|
29
|
+
*
|
|
30
|
+
* Exposed as a module-level helper (in addition to the instance method below)
|
|
31
|
+
* so consumers that do not yet hold an `OxyServices` instance can still mint a
|
|
32
|
+
* bounce state. Reuses the same `crypto.randomUUID`-based generator the popup
|
|
33
|
+
* mixin uses for its CSRF state, with a `getRandomValues` fallback.
|
|
34
|
+
*/
|
|
35
|
+
export function generateSsoState() {
|
|
36
|
+
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
|
37
|
+
return crypto.randomUUID();
|
|
38
|
+
}
|
|
39
|
+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
40
|
+
const bytes = new Uint8Array(16);
|
|
41
|
+
crypto.getRandomValues(bytes);
|
|
42
|
+
return Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
|
|
43
|
+
}
|
|
44
|
+
throw new Error('No secure random source available for SSO state generation');
|
|
45
|
+
}
|
|
46
|
+
export function OxyServicesSsoMixin(Base) {
|
|
47
|
+
return class extends Base {
|
|
48
|
+
constructor(...args) {
|
|
49
|
+
super(...args);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Generate cryptographically secure state for the SSO bounce (CSRF
|
|
53
|
+
* protection). Delegates to the module-level {@link generateSsoState}
|
|
54
|
+
* helper, which uses the same `crypto.randomUUID`-based generator the popup
|
|
55
|
+
* mixin's `generateState()` uses — one shared secure-random implementation.
|
|
56
|
+
*/
|
|
57
|
+
generateSsoState() {
|
|
58
|
+
return generateSsoState();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Exchange an opaque single-use SSO code for the real Oxy session.
|
|
62
|
+
*
|
|
63
|
+
* POSTs `{ code }` to `${getSessionBaseUrl()}/sso/exchange` as a CORS
|
|
64
|
+
* request with NO credentials/cookies. On success the returned access token
|
|
65
|
+
* is planted via `httpService.setTokens(...)` (matching
|
|
66
|
+
* `exchangeIdTokenForSession` / `verifyChallenge`), so callers do not need
|
|
67
|
+
* to plant tokens manually.
|
|
68
|
+
*
|
|
69
|
+
* @param code - The opaque single-use code delivered in the SSO return
|
|
70
|
+
* fragment (see {@link parseSsoReturnFragment}). The central store burns
|
|
71
|
+
* it atomically on exchange.
|
|
72
|
+
* @returns The resolved {@link SessionLoginResponse}.
|
|
73
|
+
*/
|
|
74
|
+
async exchangeSsoCode(code) {
|
|
75
|
+
if (typeof code !== 'string' || code.length === 0) {
|
|
76
|
+
throw this.handleError(new Error('exchangeSsoCode requires a non-empty code'));
|
|
77
|
+
}
|
|
78
|
+
const url = `${this.getSessionBaseUrl().replace(/\/$/, '')}/sso/exchange`;
|
|
79
|
+
debug.log('Exchanging SSO code for session...');
|
|
80
|
+
let response;
|
|
81
|
+
try {
|
|
82
|
+
response = await fetch(url, {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
// No cookies: the opaque code is the sole bearer of authority and the
|
|
85
|
+
// server validates by Origin against the code's bound clientOrigin.
|
|
86
|
+
credentials: 'omit',
|
|
87
|
+
headers: {
|
|
88
|
+
'Content-Type': 'application/json',
|
|
89
|
+
Accept: 'application/json',
|
|
90
|
+
},
|
|
91
|
+
body: JSON.stringify({ code }),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
debug.error('SSO exchange request failed:', error instanceof Error ? error.message : String(error));
|
|
96
|
+
throw this.handleError(error);
|
|
97
|
+
}
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
throw this.handleError(new Error(`SSO exchange failed with HTTP ${response.status}`));
|
|
100
|
+
}
|
|
101
|
+
let payload;
|
|
102
|
+
try {
|
|
103
|
+
payload = (await response.json());
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
throw this.handleError(error);
|
|
107
|
+
}
|
|
108
|
+
if (!payload || typeof payload.accessToken !== 'string' || payload.accessToken.length === 0) {
|
|
109
|
+
throw this.handleError(new Error('SSO exchange returned no access token'));
|
|
110
|
+
}
|
|
111
|
+
if (typeof payload.sessionId !== 'string' || payload.sessionId.length === 0) {
|
|
112
|
+
throw this.handleError(new Error('SSO exchange returned no sessionId'));
|
|
113
|
+
}
|
|
114
|
+
const userId = payload.user?.id ?? payload.user?._id;
|
|
115
|
+
if (!userId || typeof payload.user?.username !== 'string') {
|
|
116
|
+
throw this.handleError(new Error('SSO exchange returned an invalid user'));
|
|
117
|
+
}
|
|
118
|
+
const user = {
|
|
119
|
+
id: userId,
|
|
120
|
+
username: payload.user.username,
|
|
121
|
+
avatar: payload.user.avatar,
|
|
122
|
+
};
|
|
123
|
+
// Plant the access token exactly like exchangeIdTokenForSession does.
|
|
124
|
+
// The SSO exchange does not return a refresh token (the central store
|
|
125
|
+
// holds the refresh credential), so default it to an empty string.
|
|
126
|
+
this.httpService.setTokens(payload.accessToken, '');
|
|
127
|
+
debug.log('SSO exchange complete:', { hasSession: !!payload.sessionId });
|
|
128
|
+
const session = {
|
|
129
|
+
sessionId: payload.sessionId,
|
|
130
|
+
deviceId: '',
|
|
131
|
+
expiresAt: payload.expiresAt ?? '',
|
|
132
|
+
user,
|
|
133
|
+
accessToken: payload.accessToken,
|
|
134
|
+
};
|
|
135
|
+
return session;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
package/dist/esm/mixins/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { OxyServicesAuthMixin } from './OxyServices.auth.js';
|
|
|
9
9
|
import { OxyServicesFedCMMixin } from './OxyServices.fedcm.js';
|
|
10
10
|
import { OxyServicesPopupAuthMixin } from './OxyServices.popup.js';
|
|
11
11
|
import { OxyServicesRedirectAuthMixin } from './OxyServices.redirect.js';
|
|
12
|
+
import { OxyServicesSsoMixin } from './OxyServices.sso.js';
|
|
12
13
|
import { OxyServicesUserMixin } from './OxyServices.user.js';
|
|
13
14
|
import { OxyServicesPrivacyMixin } from './OxyServices.privacy.js';
|
|
14
15
|
import { OxyServicesLanguageMixin } from './OxyServices.language.js';
|
|
@@ -48,6 +49,9 @@ const MIXIN_PIPELINE = [
|
|
|
48
49
|
OxyServicesFedCMMixin,
|
|
49
50
|
OxyServicesPopupAuthMixin,
|
|
50
51
|
OxyServicesRedirectAuthMixin,
|
|
52
|
+
// Central cross-domain SSO (opaque-code exchange). After Popup so it can
|
|
53
|
+
// reuse the popup mixin's secure-random `generateState()`.
|
|
54
|
+
OxyServicesSsoMixin,
|
|
51
55
|
// User management (requires auth)
|
|
52
56
|
OxyServicesUserMixin,
|
|
53
57
|
OxyServicesPrivacyMixin,
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Central IdP (auth web) URL resolution for cross-domain SSO.
|
|
3
|
+
*
|
|
4
|
+
* The Oxy ecosystem runs a single, central Identity Provider at
|
|
5
|
+
* `auth.oxy.so`. For TRUE central cross-domain SSO (Google/Meta/Clerk style),
|
|
6
|
+
* FedCM and the opaque-code SSO bounce always target this one origin — it owns
|
|
7
|
+
* the host-only `fedcm_session` cookie and the central session store reachable
|
|
8
|
+
* via `api.oxy.so`. Relying Parties (mention.earth, homiio.com, alia.onl, …)
|
|
9
|
+
* delegate to it rather than standing up a per-apex IdP.
|
|
10
|
+
*
|
|
11
|
+
* This module is intentionally pure: it performs no DOM access, reads no
|
|
12
|
+
* `window`/`location`, and has no side effects. It is the single source of
|
|
13
|
+
* truth for the central IdP origin so call sites never hardcode the literal.
|
|
14
|
+
*
|
|
15
|
+
* Note: this is distinct from `autoDetectAuthWebUrl` (per-apex `auth.<rp-apex>`
|
|
16
|
+
* derivation). The central-SSO path deliberately does NOT auto-detect per-apex
|
|
17
|
+
* IdPs — it is central only. An explicitly-configured `authWebUrl` still wins.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* The canonical central Identity Provider origin for the Oxy ecosystem.
|
|
21
|
+
* No trailing slash.
|
|
22
|
+
*/
|
|
23
|
+
export const CENTRAL_AUTH_URL = 'https://auth.oxy.so';
|
|
24
|
+
/**
|
|
25
|
+
* Resolve the central IdP origin, honouring an explicit override.
|
|
26
|
+
*
|
|
27
|
+
* @param explicit - A caller-supplied auth web URL, or `undefined`/empty to use
|
|
28
|
+
* the central default. An explicit non-empty value always wins.
|
|
29
|
+
* @returns The explicit value when provided, otherwise {@link CENTRAL_AUTH_URL}.
|
|
30
|
+
*/
|
|
31
|
+
export function resolveCentralAuthUrl(explicit) {
|
|
32
|
+
return explicit ?? CENTRAL_AUTH_URL;
|
|
33
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse the SSO return fragment delivered by the central IdP.
|
|
3
|
+
*
|
|
4
|
+
* After a top-level redirect bounce to `auth.oxy.so/sso` (prompt=none), the
|
|
5
|
+
* central IdP returns the Relying Party to its `redirect_uri` with the result
|
|
6
|
+
* encoded in the URL fragment (the `#…` part). The fragment is used — not a
|
|
7
|
+
* query string — so the opaque single-use code never reaches a server access
|
|
8
|
+
* log, a `Referer` header, or browser history in a recoverable form.
|
|
9
|
+
*
|
|
10
|
+
* Three outcomes are possible:
|
|
11
|
+
* - `#oxy_sso=ok&code=<opaque>&state=<state>` — the IdP had a session; the RP
|
|
12
|
+
* exchanges `code` (via `oxy.exchangeSsoCode`) for the real session. NO
|
|
13
|
+
* token/JWT ever appears in the URL — only the opaque code.
|
|
14
|
+
* - `#oxy_sso=none&state=<state>` — the IdP had no session (prompt=none, user
|
|
15
|
+
* not signed in centrally). The RP shows its own signed-out UI.
|
|
16
|
+
* - `#oxy_sso=error&state=<state>` — the bounce failed. The RP recovers.
|
|
17
|
+
*
|
|
18
|
+
* This parser is pure and defensive: it never throws, and `kind` is strictly
|
|
19
|
+
* one of `'ok' | 'none' | 'error'`. It returns `null` when the fragment is not
|
|
20
|
+
* an oxy_sso fragment at all (i.e. `oxy_sso` is absent or an unrecognised
|
|
21
|
+
* value), so the caller can ignore unrelated fragments without special-casing.
|
|
22
|
+
*/
|
|
23
|
+
const VALID_KINDS = new Set(['ok', 'none', 'error']);
|
|
24
|
+
/**
|
|
25
|
+
* Parse an SSO return fragment.
|
|
26
|
+
*
|
|
27
|
+
* @param hash - The URL fragment, with or without the leading `#`
|
|
28
|
+
* (e.g. `location.hash`). May be `undefined`/empty.
|
|
29
|
+
* @returns The parsed result when `hash` is a recognised oxy_sso fragment,
|
|
30
|
+
* otherwise `null`. Never throws.
|
|
31
|
+
*/
|
|
32
|
+
export function parseSsoReturnFragment(hash) {
|
|
33
|
+
if (typeof hash !== 'string' || hash.length === 0) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
// Strip a single leading '#'. A bare '#' (empty fragment) yields no params.
|
|
37
|
+
const raw = hash.startsWith('#') ? hash.slice(1) : hash;
|
|
38
|
+
if (raw.length === 0) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
let params;
|
|
42
|
+
try {
|
|
43
|
+
params = new URLSearchParams(raw);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// URLSearchParams does not throw for malformed input in practice, but guard
|
|
47
|
+
// against any environment/polyfill that might so this stays total.
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const kind = params.get('oxy_sso');
|
|
51
|
+
if (kind === null || !VALID_KINDS.has(kind)) {
|
|
52
|
+
// Not an oxy_sso fragment (absent or unrecognised value) — ignore it.
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const result = { kind: kind };
|
|
56
|
+
const state = params.get('state');
|
|
57
|
+
if (state !== null && state.length > 0) {
|
|
58
|
+
result.state = state;
|
|
59
|
+
}
|
|
60
|
+
// The opaque code is only meaningful on success; ignore any stray `code` on
|
|
61
|
+
// none/error so callers never attempt an exchange for a non-ok outcome.
|
|
62
|
+
if (result.kind === 'ok') {
|
|
63
|
+
const code = params.get('code');
|
|
64
|
+
if (code !== null && code.length > 0) {
|
|
65
|
+
result.code = code;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|