@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.
@@ -126,6 +126,8 @@ export interface OxyServices extends InstanceType<ReturnType<typeof composeOxySe
126
126
  openBlankPopup(width?: number, height?: number): Window | null;
127
127
  signInWithRedirect(options?: RedirectAuthOptions): void;
128
128
  signUpWithRedirect(options?: RedirectAuthOptions): void;
129
+ exchangeSsoCode(code: string): Promise<SessionLoginResponse>;
130
+ generateSsoState(): string;
129
131
  auth(options?: {
130
132
  debug?: boolean;
131
133
  onError?: (error: unknown) => unknown;
@@ -73,6 +73,10 @@ export { updateAvatarVisibility } from './utils/avatarUtils';
73
73
  export { buildAccountsArray, createQuickAccount, getAccountDisplayName, getAccountFallbackHandle, formatPublicKeyHandle, mergeAccountsFromRefreshAll, getAccountColor, } from './utils/accountUtils';
74
74
  export type { QuickAccount, DisplayNameUserShape } from './utils/accountUtils';
75
75
  export { autoDetectAuthWebUrl } from './utils/fapiAutoDetect';
76
+ export { CENTRAL_AUTH_URL, resolveCentralAuthUrl } from './utils/authWebUrl';
77
+ export { parseSsoReturnFragment } from './utils/ssoReturn';
78
+ export type { SsoReturnKind, SsoReturnResult } from './utils/ssoReturn';
79
+ export { generateSsoState } from './mixins/OxyServices.sso';
76
80
  export { runColdBoot } from './utils/coldBoot';
77
81
  export type { ColdBootStep, ColdBootStepResult, ColdBootSession, ColdBootSkip, ColdBootOutcome, RunColdBootOptions, } from './utils/coldBoot';
78
82
  export { packageInfo } from './constants/version';
@@ -132,9 +132,7 @@ export declare function OxyServicesAssetsMixin<T extends typeof OxyServicesBase>
132
132
  withAuthRetry<T_1>(operation: () => Promise<T_1>, operationName: string, options?: {
133
133
  maxRetries?: number;
134
134
  retryDelay?: number;
135
- authTimeoutMs? /**
136
- * Get asset metadata
137
- */: number;
135
+ authTimeoutMs?: number;
138
136
  }): Promise<T_1>;
139
137
  validate(): Promise<boolean>;
140
138
  handleError(error: unknown): Error;
@@ -0,0 +1,111 @@
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 type { OxyServicesBase } from '../OxyServices.base';
26
+ import type { SessionLoginResponse } from '../models/session';
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 declare function generateSsoState(): string;
36
+ export declare function OxyServicesSsoMixin<T extends typeof OxyServicesBase>(Base: T): {
37
+ new (...args: any[]): {
38
+ /**
39
+ * Generate cryptographically secure state for the SSO bounce (CSRF
40
+ * protection). Delegates to the module-level {@link generateSsoState}
41
+ * helper, which uses the same `crypto.randomUUID`-based generator the popup
42
+ * mixin's `generateState()` uses — one shared secure-random implementation.
43
+ */
44
+ generateSsoState(): string;
45
+ /**
46
+ * Exchange an opaque single-use SSO code for the real Oxy session.
47
+ *
48
+ * POSTs `{ code }` to `${getSessionBaseUrl()}/sso/exchange` as a CORS
49
+ * request with NO credentials/cookies. On success the returned access token
50
+ * is planted via `httpService.setTokens(...)` (matching
51
+ * `exchangeIdTokenForSession` / `verifyChallenge`), so callers do not need
52
+ * to plant tokens manually.
53
+ *
54
+ * @param code - The opaque single-use code delivered in the SSO return
55
+ * fragment (see {@link parseSsoReturnFragment}). The central store burns
56
+ * it atomically on exchange.
57
+ * @returns The resolved {@link SessionLoginResponse}.
58
+ */
59
+ exchangeSsoCode(code: string): Promise<SessionLoginResponse>;
60
+ httpService: import("../HttpService").HttpService;
61
+ cloudURL: string;
62
+ config: import("../OxyServices.base").OxyConfig;
63
+ __resetTokensForTests(): void;
64
+ makeRequest<T_1>(method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", url: string, data?: any, options?: import("../HttpService").RequestOptions): Promise<T_1>;
65
+ getBaseURL(): string;
66
+ getSessionBaseUrl(): string;
67
+ getClient(): import("../HttpService").HttpService;
68
+ getMetrics(): {
69
+ totalRequests: number;
70
+ successfulRequests: number;
71
+ failedRequests: number;
72
+ cacheHits: number;
73
+ cacheMisses: number;
74
+ averageResponseTime: number;
75
+ };
76
+ clearCache(): void;
77
+ clearCacheEntry(key: string): void;
78
+ clearCacheByPrefix(prefix: string): number;
79
+ getCacheStats(): {
80
+ size: number;
81
+ hits: number;
82
+ misses: number;
83
+ hitRate: number;
84
+ };
85
+ getCloudURL(): string;
86
+ setTokens(accessToken: string, refreshToken?: string): void;
87
+ clearTokens(): void;
88
+ onTokensChanged(listener: (accessToken: string | null) => void): () => void;
89
+ _cachedUserId: string | null | undefined;
90
+ _cachedAccessToken: string | null;
91
+ getCurrentUserId(): string | null;
92
+ hasValidToken(): boolean;
93
+ getAccessToken(): string | null;
94
+ setActingAs(userId: string | null): void;
95
+ getActingAs(): string | null;
96
+ waitForAuth(timeoutMs?: number): Promise<boolean>;
97
+ withAuthRetry<T_1>(operation: () => Promise<T_1>, operationName: string, options?: {
98
+ maxRetries?: number;
99
+ retryDelay?: number;
100
+ authTimeoutMs?: number;
101
+ }): Promise<T_1>;
102
+ validate(): Promise<boolean>;
103
+ handleError(error: unknown): Error;
104
+ healthCheck(): Promise<{
105
+ status: string;
106
+ users?: number;
107
+ timestamp?: string;
108
+ [key: string]: any;
109
+ }>;
110
+ };
111
+ } & T;
@@ -9,6 +9,7 @@ import { OxyServicesAuthMixin } from './OxyServices.auth';
9
9
  import { OxyServicesFedCMMixin } from './OxyServices.fedcm';
10
10
  import { OxyServicesPopupAuthMixin } from './OxyServices.popup';
11
11
  import { OxyServicesRedirectAuthMixin } from './OxyServices.redirect';
12
+ import { OxyServicesSsoMixin } from './OxyServices.sso';
12
13
  import { OxyServicesUserMixin } from './OxyServices.user';
13
14
  import { OxyServicesPrivacyMixin } from './OxyServices.privacy';
14
15
  import { OxyServicesLanguageMixin } from './OxyServices.language';
@@ -35,7 +36,7 @@ import { OxyServicesAppDataMixin } from './OxyServices.appData';
35
36
  * If you add a new mixin to `MIXIN_PIPELINE`, add it here too so its methods
36
37
  * are visible without a cast.
37
38
  */
38
- type AllMixinInstances = InstanceType<ReturnType<typeof OxyServicesAuthMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesFedCMMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesPopupAuthMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesRedirectAuthMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesUserMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesPrivacyMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesLanguageMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesPaymentMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesKarmaMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesAssetsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesDeveloperMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesLocationMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesAnalyticsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesDevicesMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesSecurityMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesFeaturesMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesTopicsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesManagedAccountsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesContactsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesAppDataMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesUtilityMixin<typeof OxyServicesBase>>>;
39
+ type AllMixinInstances = InstanceType<ReturnType<typeof OxyServicesAuthMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesFedCMMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesPopupAuthMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesRedirectAuthMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesSsoMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesUserMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesPrivacyMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesLanguageMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesPaymentMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesKarmaMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesAssetsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesDeveloperMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesLocationMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesAnalyticsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesDevicesMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesSecurityMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesFeaturesMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesTopicsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesManagedAccountsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesContactsMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesAppDataMixin<typeof OxyServicesBase>>> & InstanceType<ReturnType<typeof OxyServicesUtilityMixin<typeof OxyServicesBase>>>;
39
40
  /**
40
41
  * Constructor type for the fully composed mixin pipeline. Each mixin returns
41
42
  * a new constructor that augments its input; reducing across the pipeline
@@ -0,0 +1,31 @@
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 declare 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 declare function resolveCentralAuthUrl(explicit?: string): string;
@@ -0,0 +1,46 @@
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
+ /**
24
+ * The recognised outcomes of an SSO bounce.
25
+ */
26
+ export type SsoReturnKind = 'ok' | 'none' | 'error';
27
+ /**
28
+ * The parsed result of an SSO return fragment.
29
+ *
30
+ * `code` is present only for `kind: 'ok'`. `state` echoes the CSRF state the RP
31
+ * generated for the bounce (when the IdP round-tripped it).
32
+ */
33
+ export interface SsoReturnResult {
34
+ kind: SsoReturnKind;
35
+ code?: string;
36
+ state?: string;
37
+ }
38
+ /**
39
+ * Parse an SSO return fragment.
40
+ *
41
+ * @param hash - The URL fragment, with or without the leading `#`
42
+ * (e.g. `location.hash`). May be `undefined`/empty.
43
+ * @returns The parsed result when `hash` is a recognised oxy_sso fragment,
44
+ * otherwise `null`. Never throws.
45
+ */
46
+ export declare function parseSsoReturnFragment(hash: string | undefined | null): SsoReturnResult | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxyhq/core",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "OxyHQ SDK Foundation — API client, authentication, cryptographic identity, and shared utilities",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -8,6 +8,7 @@ import type { OxyConfig as OxyConfigBase, ApiError, User } from './models/interf
8
8
  import { handleHttpError } from './utils/errorUtils';
9
9
  import { HttpService, type RequestOptions } from './HttpService';
10
10
  import { OxyAuthenticationError, OxyAuthenticationTimeoutError } from './OxyServices.errors';
11
+ import { resolveCentralAuthUrl } from './utils/authWebUrl';
11
12
 
12
13
  export interface OxyConfig extends OxyConfigBase {
13
14
  cloudURL?: string;
@@ -34,11 +35,24 @@ export class OxyServicesBase {
34
35
  if (!config || typeof config !== 'object') {
35
36
  throw new Error('OxyConfig is required');
36
37
  }
37
- this.config = config;
38
- this.cloudURL = config.cloudURL || 'https://cloud.oxy.so';
38
+
39
+ // Default `authWebUrl` to the CENTRAL IdP (`auth.oxy.so`) when the caller
40
+ // did not pin it explicitly. TRUE central cross-domain SSO (Google/Meta/
41
+ // Clerk style) routes every RP through the one central IdP — it owns the
42
+ // host-only `fedcm_session` cookie and the central session store — so the
43
+ // SDK no longer derives a per-apex `auth.<rp-apex>` IdP by default.
44
+ // `autoDetectAuthWebUrl` is still exported for any call site that opts into
45
+ // per-apex resolution, but it is NOT the constructor default anymore.
46
+ // An explicit `authWebUrl` always wins (we only fill it when absent).
47
+ const resolvedConfig: OxyConfig = config.authWebUrl
48
+ ? config
49
+ : { ...config, authWebUrl: resolveCentralAuthUrl(config.authWebUrl) };
50
+
51
+ this.config = resolvedConfig;
52
+ this.cloudURL = resolvedConfig.cloudURL || 'https://cloud.oxy.so';
39
53
 
40
54
  // Initialize unified HTTP service (handles auth, caching, deduplication, queuing, retry)
41
- this.httpService = new HttpService(config);
55
+ this.httpService = new HttpService(resolvedConfig);
42
56
  }
43
57
 
44
58
  // Test-only utility to reset tokens on this instance between jest tests
@@ -151,6 +151,10 @@ export interface OxyServices extends InstanceType<ReturnType<typeof composeOxySe
151
151
  signInWithRedirect(options?: RedirectAuthOptions): void;
152
152
  signUpWithRedirect(options?: RedirectAuthOptions): void;
153
153
 
154
+ // Central cross-domain SSO (opaque single-use code exchange)
155
+ exchangeSsoCode(code: string): Promise<SessionLoginResponse>;
156
+ generateSsoState(): string;
157
+
154
158
  // Express.js middleware
155
159
  auth(options?: {
156
160
  debug?: boolean;
package/src/index.ts CHANGED
@@ -369,6 +369,13 @@ export type { QuickAccount, DisplayNameUserShape } from './utils/accountUtils';
369
369
  // Cross-domain SSO infrastructure
370
370
  // ---------------------------------------------------------------------------
371
371
  export { autoDetectAuthWebUrl } from './utils/fapiAutoDetect';
372
+
373
+ // Central cross-domain SSO (opaque single-use code bounce via auth.oxy.so)
374
+ export { CENTRAL_AUTH_URL, resolveCentralAuthUrl } from './utils/authWebUrl';
375
+ export { parseSsoReturnFragment } from './utils/ssoReturn';
376
+ export type { SsoReturnKind, SsoReturnResult } from './utils/ssoReturn';
377
+ export { generateSsoState } from './mixins/OxyServices.sso';
378
+
372
379
  export { runColdBoot } from './utils/coldBoot';
373
380
  export type {
374
381
  ColdBootStep,
@@ -0,0 +1,172 @@
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
+
26
+ import type { OxyServicesBase } from '../OxyServices.base';
27
+ import type { SessionLoginResponse, MinimalUserData } from '../models/session';
28
+ import { createDebugLogger } from '../shared/utils/debugUtils';
29
+
30
+ const debug = createDebugLogger('SSO');
31
+
32
+ /**
33
+ * Wire shape of `POST /sso/exchange`. `expiresAt` and `authuser` are optional
34
+ * (the central store may omit them for legacy single-slot sessions).
35
+ */
36
+ interface SsoExchangeWireResponse {
37
+ accessToken: string;
38
+ sessionId: string;
39
+ user: {
40
+ id?: string;
41
+ _id?: string;
42
+ username?: string;
43
+ avatar?: string;
44
+ };
45
+ expiresAt?: string;
46
+ authuser?: number;
47
+ }
48
+
49
+ /**
50
+ * Generate a cryptographically secure state value for the SSO bounce.
51
+ *
52
+ * Exposed as a module-level helper (in addition to the instance method below)
53
+ * so consumers that do not yet hold an `OxyServices` instance can still mint a
54
+ * bounce state. Reuses the same `crypto.randomUUID`-based generator the popup
55
+ * mixin uses for its CSRF state, with a `getRandomValues` fallback.
56
+ */
57
+ export function generateSsoState(): string {
58
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
59
+ return crypto.randomUUID();
60
+ }
61
+ if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
62
+ const bytes = new Uint8Array(16);
63
+ crypto.getRandomValues(bytes);
64
+ return Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
65
+ }
66
+ throw new Error('No secure random source available for SSO state generation');
67
+ }
68
+
69
+ export function OxyServicesSsoMixin<T extends typeof OxyServicesBase>(Base: T) {
70
+ return class extends Base {
71
+ constructor(...args: any[]) {
72
+ super(...(args as [any]));
73
+ }
74
+
75
+ /**
76
+ * Generate cryptographically secure state for the SSO bounce (CSRF
77
+ * protection). Delegates to the module-level {@link generateSsoState}
78
+ * helper, which uses the same `crypto.randomUUID`-based generator the popup
79
+ * mixin's `generateState()` uses — one shared secure-random implementation.
80
+ */
81
+ public generateSsoState(): string {
82
+ return generateSsoState();
83
+ }
84
+
85
+ /**
86
+ * Exchange an opaque single-use SSO code for the real Oxy session.
87
+ *
88
+ * POSTs `{ code }` to `${getSessionBaseUrl()}/sso/exchange` as a CORS
89
+ * request with NO credentials/cookies. On success the returned access token
90
+ * is planted via `httpService.setTokens(...)` (matching
91
+ * `exchangeIdTokenForSession` / `verifyChallenge`), so callers do not need
92
+ * to plant tokens manually.
93
+ *
94
+ * @param code - The opaque single-use code delivered in the SSO return
95
+ * fragment (see {@link parseSsoReturnFragment}). The central store burns
96
+ * it atomically on exchange.
97
+ * @returns The resolved {@link SessionLoginResponse}.
98
+ */
99
+ public async exchangeSsoCode(code: string): Promise<SessionLoginResponse> {
100
+ if (typeof code !== 'string' || code.length === 0) {
101
+ throw this.handleError(new Error('exchangeSsoCode requires a non-empty code'));
102
+ }
103
+
104
+ const url = `${this.getSessionBaseUrl().replace(/\/$/, '')}/sso/exchange`;
105
+ debug.log('Exchanging SSO code for session...');
106
+
107
+ let response: Response;
108
+ try {
109
+ response = await fetch(url, {
110
+ method: 'POST',
111
+ // No cookies: the opaque code is the sole bearer of authority and the
112
+ // server validates by Origin against the code's bound clientOrigin.
113
+ credentials: 'omit',
114
+ headers: {
115
+ 'Content-Type': 'application/json',
116
+ Accept: 'application/json',
117
+ },
118
+ body: JSON.stringify({ code }),
119
+ });
120
+ } catch (error) {
121
+ debug.error('SSO exchange request failed:', error instanceof Error ? error.message : String(error));
122
+ throw this.handleError(error);
123
+ }
124
+
125
+ if (!response.ok) {
126
+ throw this.handleError(new Error(`SSO exchange failed with HTTP ${response.status}`));
127
+ }
128
+
129
+ let payload: SsoExchangeWireResponse;
130
+ try {
131
+ payload = (await response.json()) as SsoExchangeWireResponse;
132
+ } catch (error) {
133
+ throw this.handleError(error);
134
+ }
135
+
136
+ if (!payload || typeof payload.accessToken !== 'string' || payload.accessToken.length === 0) {
137
+ throw this.handleError(new Error('SSO exchange returned no access token'));
138
+ }
139
+ if (typeof payload.sessionId !== 'string' || payload.sessionId.length === 0) {
140
+ throw this.handleError(new Error('SSO exchange returned no sessionId'));
141
+ }
142
+
143
+ const userId = payload.user?.id ?? payload.user?._id;
144
+ if (!userId || typeof payload.user?.username !== 'string') {
145
+ throw this.handleError(new Error('SSO exchange returned an invalid user'));
146
+ }
147
+
148
+ const user: MinimalUserData = {
149
+ id: userId,
150
+ username: payload.user.username,
151
+ avatar: payload.user.avatar,
152
+ };
153
+
154
+ // Plant the access token exactly like exchangeIdTokenForSession does.
155
+ // The SSO exchange does not return a refresh token (the central store
156
+ // holds the refresh credential), so default it to an empty string.
157
+ this.httpService.setTokens(payload.accessToken, '');
158
+
159
+ debug.log('SSO exchange complete:', { hasSession: !!payload.sessionId });
160
+
161
+ const session: SessionLoginResponse = {
162
+ sessionId: payload.sessionId,
163
+ deviceId: '',
164
+ expiresAt: payload.expiresAt ?? '',
165
+ user,
166
+ accessToken: payload.accessToken,
167
+ };
168
+
169
+ return session;
170
+ }
171
+ };
172
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * `OxyServices` constructor central-IdP defaulting.
3
+ *
4
+ * TRUE central cross-domain SSO (Google/Meta/Clerk style, 2026-06-13) routes
5
+ * every Relying Party through ONE central IdP at `auth.oxy.so` — it owns the
6
+ * host-only `fedcm_session` cookie and the central session store. The SDK
7
+ * therefore defaults `authWebUrl` to the central IdP when the caller omits it,
8
+ * via `resolveCentralAuthUrl(config.authWebUrl)`.
9
+ *
10
+ * This replaces the previous behaviour, where the constructor auto-detected a
11
+ * per-apex IdP (`auth.<rp-apex>`) from `window.location`. `autoDetectAuthWebUrl`
12
+ * is still exported for call sites that opt into per-apex resolution, but it is
13
+ * NO LONGER the constructor default.
14
+ *
15
+ * Contract:
16
+ * - authWebUrl omitted, no window (native/SSR) -> 'https://auth.oxy.so'
17
+ * - authWebUrl omitted, window present -> 'https://auth.oxy.so'
18
+ * (the page host is irrelevant — central only)
19
+ * - authWebUrl explicit -> respected verbatim (wins)
20
+ */
21
+
22
+ import { OxyServices } from '../../OxyServices';
23
+ import { CENTRAL_AUTH_URL } from '../../utils/authWebUrl';
24
+
25
+ function installWindowLocation(hostname: string, protocol = 'https:'): void {
26
+ (globalThis as unknown as { window: unknown }).window = {
27
+ location: { hostname, protocol },
28
+ };
29
+ }
30
+
31
+ function clearWindow(): void {
32
+ delete (globalThis as Record<string, unknown>).window;
33
+ }
34
+
35
+ describe('OxyServices constructor — central IdP defaulting', () => {
36
+ afterEach(() => {
37
+ clearWindow();
38
+ });
39
+
40
+ it('defaults authWebUrl to the central IdP on native/SSR (no window)', () => {
41
+ const oxy = new OxyServices({ baseURL: 'https://api.oxy.so' });
42
+
43
+ expect(oxy.config.authWebUrl).toBe(CENTRAL_AUTH_URL);
44
+ expect(oxy.config.authWebUrl).toBe('https://auth.oxy.so');
45
+ });
46
+
47
+ it('defaults to the central IdP on web regardless of page host', () => {
48
+ // The page is mention.earth, but central SSO never derives a per-apex IdP.
49
+ installWindowLocation('mention.earth');
50
+
51
+ const oxy = new OxyServices({ baseURL: 'https://api.mention.earth' });
52
+
53
+ expect(oxy.config.authWebUrl).toBe('https://auth.oxy.so');
54
+ });
55
+
56
+ it('defaults to the central IdP even on a subdomain page host', () => {
57
+ installWindowLocation('www.homiio.com');
58
+
59
+ const oxy = new OxyServices({ baseURL: 'https://api.homiio.com' });
60
+
61
+ expect(oxy.config.authWebUrl).toBe('https://auth.oxy.so');
62
+ });
63
+
64
+ it('respects an explicit authWebUrl (explicit wins)', () => {
65
+ installWindowLocation('mention.earth');
66
+
67
+ const oxy = new OxyServices({
68
+ baseURL: 'https://api.mention.earth',
69
+ authWebUrl: 'https://auth.mention.earth',
70
+ });
71
+
72
+ // The caller pinned a per-apex IdP explicitly — honour it verbatim.
73
+ expect(oxy.config.authWebUrl).toBe('https://auth.mention.earth');
74
+ });
75
+
76
+ it('does not mutate the caller-supplied config object', () => {
77
+ const input = { baseURL: 'https://api.mention.earth' };
78
+ const oxy = new OxyServices(input);
79
+
80
+ // The stored config carries the central IdP default...
81
+ expect(oxy.config.authWebUrl).toBe('https://auth.oxy.so');
82
+ // ...but the caller's own object reference is untouched.
83
+ expect((input as { authWebUrl?: string }).authWebUrl).toBeUndefined();
84
+ });
85
+ });