@oxyhq/services 21.0.1 → 22.0.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.
Files changed (68) hide show
  1. package/lib/commonjs/ui/boot/runProviderColdBoot.js +12 -2
  2. package/lib/commonjs/ui/boot/runProviderColdBoot.js.map +1 -1
  3. package/lib/commonjs/ui/components/OxyAccountDialog.js +0 -17
  4. package/lib/commonjs/ui/components/OxyAccountDialog.js.map +1 -1
  5. package/lib/commonjs/ui/components/OxyProvider.js +2 -0
  6. package/lib/commonjs/ui/components/OxyProvider.js.map +1 -1
  7. package/lib/commonjs/ui/context/OxyContext.js +22 -69
  8. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  9. package/lib/commonjs/ui/context/passkeyFlow.js +9 -8
  10. package/lib/commonjs/ui/context/passkeyFlow.js.map +1 -1
  11. package/lib/commonjs/ui/utils/crossOriginRestore.js +26 -0
  12. package/lib/commonjs/ui/utils/crossOriginRestore.js.map +1 -1
  13. package/lib/module/ui/boot/runProviderColdBoot.js +13 -3
  14. package/lib/module/ui/boot/runProviderColdBoot.js.map +1 -1
  15. package/lib/module/ui/components/OxyAccountDialog.js +0 -17
  16. package/lib/module/ui/components/OxyAccountDialog.js.map +1 -1
  17. package/lib/module/ui/components/OxyProvider.js +2 -0
  18. package/lib/module/ui/components/OxyProvider.js.map +1 -1
  19. package/lib/module/ui/context/OxyContext.js +22 -69
  20. package/lib/module/ui/context/OxyContext.js.map +1 -1
  21. package/lib/module/ui/context/passkeyFlow.js +9 -8
  22. package/lib/module/ui/context/passkeyFlow.js.map +1 -1
  23. package/lib/module/ui/utils/crossOriginRestore.js +26 -1
  24. package/lib/module/ui/utils/crossOriginRestore.js.map +1 -1
  25. package/lib/typescript/commonjs/index.d.ts +1 -1
  26. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  27. package/lib/typescript/commonjs/ui/boot/runProviderColdBoot.d.ts +6 -0
  28. package/lib/typescript/commonjs/ui/boot/runProviderColdBoot.d.ts.map +1 -1
  29. package/lib/typescript/commonjs/ui/components/OxyAccountDialog.d.ts.map +1 -1
  30. package/lib/typescript/commonjs/ui/components/OxyProvider.d.ts.map +1 -1
  31. package/lib/typescript/commonjs/ui/context/OxyContext.d.ts +1 -1
  32. package/lib/typescript/commonjs/ui/context/OxyContext.d.ts.map +1 -1
  33. package/lib/typescript/commonjs/ui/context/oxyContextTypes.d.ts +22 -27
  34. package/lib/typescript/commonjs/ui/context/oxyContextTypes.d.ts.map +1 -1
  35. package/lib/typescript/commonjs/ui/context/passkeyFlow.d.ts +15 -4
  36. package/lib/typescript/commonjs/ui/context/passkeyFlow.d.ts.map +1 -1
  37. package/lib/typescript/commonjs/ui/types/navigation.d.ts +8 -0
  38. package/lib/typescript/commonjs/ui/types/navigation.d.ts.map +1 -1
  39. package/lib/typescript/commonjs/ui/utils/crossOriginRestore.d.ts +19 -0
  40. package/lib/typescript/commonjs/ui/utils/crossOriginRestore.d.ts.map +1 -1
  41. package/lib/typescript/module/index.d.ts +1 -1
  42. package/lib/typescript/module/index.d.ts.map +1 -1
  43. package/lib/typescript/module/ui/boot/runProviderColdBoot.d.ts +6 -0
  44. package/lib/typescript/module/ui/boot/runProviderColdBoot.d.ts.map +1 -1
  45. package/lib/typescript/module/ui/components/OxyAccountDialog.d.ts.map +1 -1
  46. package/lib/typescript/module/ui/components/OxyProvider.d.ts.map +1 -1
  47. package/lib/typescript/module/ui/context/OxyContext.d.ts +1 -1
  48. package/lib/typescript/module/ui/context/OxyContext.d.ts.map +1 -1
  49. package/lib/typescript/module/ui/context/oxyContextTypes.d.ts +22 -27
  50. package/lib/typescript/module/ui/context/oxyContextTypes.d.ts.map +1 -1
  51. package/lib/typescript/module/ui/context/passkeyFlow.d.ts +15 -4
  52. package/lib/typescript/module/ui/context/passkeyFlow.d.ts.map +1 -1
  53. package/lib/typescript/module/ui/types/navigation.d.ts +8 -0
  54. package/lib/typescript/module/ui/types/navigation.d.ts.map +1 -1
  55. package/lib/typescript/module/ui/utils/crossOriginRestore.d.ts +19 -0
  56. package/lib/typescript/module/ui/utils/crossOriginRestore.d.ts.map +1 -1
  57. package/package.json +5 -5
  58. package/src/index.ts +1 -1
  59. package/src/ui/boot/runProviderColdBoot.ts +24 -1
  60. package/src/ui/components/OxyAccountDialog.tsx +0 -14
  61. package/src/ui/components/OxyProvider.tsx +2 -0
  62. package/src/ui/context/OxyContext.tsx +25 -79
  63. package/src/ui/context/__tests__/passkeyFlow.test.ts +22 -15
  64. package/src/ui/context/oxyContextTypes.ts +27 -26
  65. package/src/ui/context/passkeyFlow.ts +17 -9
  66. package/src/ui/types/navigation.ts +8 -0
  67. package/src/ui/utils/__tests__/crossOriginRestore.test.ts +143 -0
  68. package/src/ui/utils/crossOriginRestore.ts +34 -0
@@ -0,0 +1,143 @@
1
+ import type { OxyServices } from '@oxyhq/core';
2
+
3
+ // Spy on the platform redirect so no real navigation happens and we can assert
4
+ // the authorize URL the silent-restore flow hands to the browser.
5
+ jest.mock('../../components/oauthNavigation', () => ({
6
+ redirectToAuthorize: jest.fn(),
7
+ }));
8
+
9
+ // jsdom's `location` is non-configurable, so the origin the code reads cannot be
10
+ // reassigned per-test. Instead, drive the ORIGIN-CLASSIFICATION predicates
11
+ // (whose real behaviour is covered by core's officialOrigins.test.ts) and keep
12
+ // every other core helper real (PKCE, authorize-URL builder, handshake store).
13
+ jest.mock('@oxyhq/core', () => {
14
+ const actual = jest.requireActual('@oxyhq/core');
15
+ return {
16
+ __esModule: true,
17
+ ...actual,
18
+ isLoopbackOrigin: jest.fn(),
19
+ isOfficialWebOrigin: jest.fn(),
20
+ isIdpHubOrigin: jest.fn(),
21
+ };
22
+ });
23
+
24
+ import {
25
+ isLoopbackOrigin,
26
+ isOfficialWebOrigin,
27
+ isIdpHubOrigin,
28
+ } from '@oxyhq/core';
29
+ import { redirectToAuthorize } from '../../components/oauthNavigation';
30
+ import {
31
+ isSilentRestoreEligibleOrigin,
32
+ maybeStartSilentOAuthRestore,
33
+ clearCrossOriginRestoreGuards,
34
+ } from '../crossOriginRestore';
35
+
36
+ const mockRedirect = redirectToAuthorize as jest.Mock;
37
+ const mockIsLoopback = isLoopbackOrigin as jest.Mock;
38
+ const mockIsOfficial = isOfficialWebOrigin as jest.Mock;
39
+ const mockIsIdpHub = isIdpHubOrigin as jest.Mock;
40
+
41
+ const OXY_SERVICES_STUB = {} as OxyServices;
42
+
43
+ /** Default to an eligible official origin; individual tests narrow this. */
44
+ function makeOriginEligible(): void {
45
+ mockIsLoopback.mockReturnValue(false);
46
+ mockIsIdpHub.mockReturnValue(false);
47
+ mockIsOfficial.mockReturnValue(true);
48
+ }
49
+
50
+ describe('crossOriginRestore', () => {
51
+ beforeEach(() => {
52
+ mockRedirect.mockClear();
53
+ mockIsLoopback.mockReset();
54
+ mockIsOfficial.mockReset();
55
+ mockIsIdpHub.mockReset();
56
+ globalThis.sessionStorage?.clear();
57
+ clearCrossOriginRestoreGuards();
58
+ });
59
+
60
+ describe('isSilentRestoreEligibleOrigin', () => {
61
+ it('rejects loopback / local-dev origins', () => {
62
+ mockIsLoopback.mockReturnValue(true);
63
+ mockIsIdpHub.mockReturnValue(false);
64
+ mockIsOfficial.mockReturnValue(true);
65
+ expect(isSilentRestoreEligibleOrigin('http://localhost:3000')).toBe(false);
66
+ });
67
+
68
+ it('rejects the central IdP hub origin (no self-hop)', () => {
69
+ mockIsLoopback.mockReturnValue(false);
70
+ mockIsIdpHub.mockReturnValue(true);
71
+ mockIsOfficial.mockReturnValue(true);
72
+ expect(isSilentRestoreEligibleOrigin('https://auth.oxy.so')).toBe(false);
73
+ });
74
+
75
+ it('rejects non-official origins', () => {
76
+ mockIsLoopback.mockReturnValue(false);
77
+ mockIsIdpHub.mockReturnValue(false);
78
+ mockIsOfficial.mockReturnValue(false);
79
+ expect(isSilentRestoreEligibleOrigin('https://evil.example')).toBe(false);
80
+ });
81
+
82
+ it('accepts official, non-loopback, non-IdP origins', () => {
83
+ makeOriginEligible();
84
+ expect(isSilentRestoreEligibleOrigin('https://accounts.oxy.so')).toBe(true);
85
+ });
86
+ });
87
+
88
+ describe('maybeStartSilentOAuthRestore', () => {
89
+ it('is SKIPPED on a loopback origin (no redirect)', async () => {
90
+ mockIsLoopback.mockReturnValue(true);
91
+ mockIsIdpHub.mockReturnValue(false);
92
+ mockIsOfficial.mockReturnValue(true);
93
+ const redirected = await maybeStartSilentOAuthRestore({
94
+ oxyServices: OXY_SERVICES_STUB,
95
+ clientId: 'oxy_dk_test',
96
+ });
97
+ expect(redirected).toBe(false);
98
+ expect(mockRedirect).not.toHaveBeenCalled();
99
+ });
100
+
101
+ it('is SKIPPED on a non-official origin (no redirect)', async () => {
102
+ mockIsLoopback.mockReturnValue(false);
103
+ mockIsIdpHub.mockReturnValue(false);
104
+ mockIsOfficial.mockReturnValue(false);
105
+ const redirected = await maybeStartSilentOAuthRestore({
106
+ oxyServices: OXY_SERVICES_STUB,
107
+ clientId: 'oxy_dk_test',
108
+ });
109
+ expect(redirected).toBe(false);
110
+ expect(mockRedirect).not.toHaveBeenCalled();
111
+ });
112
+
113
+ it('redirects to the PROD IdP by default on an eligible origin', async () => {
114
+ makeOriginEligible();
115
+ const redirected = await maybeStartSilentOAuthRestore({
116
+ oxyServices: OXY_SERVICES_STUB,
117
+ clientId: 'oxy_dk_test',
118
+ });
119
+ expect(redirected).toBe(true);
120
+ expect(mockRedirect).toHaveBeenCalledTimes(1);
121
+ const url = mockRedirect.mock.calls[0][0] as string;
122
+ expect(url.startsWith('https://auth.oxy.so/authorize?')).toBe(true);
123
+ const params = new URL(url).searchParams;
124
+ expect(params.get('client_id')).toBe('oxy_dk_test');
125
+ expect(params.get('prompt')).toBe('none');
126
+ });
127
+
128
+ it('honors an authorizeBaseUrl override (self-hosted / staging IdP)', async () => {
129
+ makeOriginEligible();
130
+ const redirected = await maybeStartSilentOAuthRestore({
131
+ oxyServices: OXY_SERVICES_STUB,
132
+ clientId: 'oxy_dk_test',
133
+ authorizeBaseUrl: 'https://auth.staging.oxy.so/authorize',
134
+ });
135
+ expect(redirected).toBe(true);
136
+ const url = mockRedirect.mock.calls[0][0] as string;
137
+ expect(url.startsWith('https://auth.staging.oxy.so/authorize?')).toBe(true);
138
+ const params = new URL(url).searchParams;
139
+ expect(params.get('client_id')).toBe('oxy_dk_test');
140
+ expect(params.get('prompt')).toBe('none');
141
+ });
142
+ });
143
+ });
@@ -3,6 +3,9 @@ import {
3
3
  buildOAuthAuthorizeUrl,
4
4
  generateOAuthState,
5
5
  generatePkcePair,
6
+ isIdpHubOrigin,
7
+ isLoopbackOrigin,
8
+ isOfficialWebOrigin,
6
9
  OXY_CROSS_ORIGIN_RESTORE_ATTEMPTED_KEY,
7
10
  OXY_SILENT_OAUTH_ATTEMPTED_KEY,
8
11
  persistOAuthHandshake,
@@ -43,11 +46,35 @@ export function clearCrossOriginRestoreGuards(): void {
43
46
  }
44
47
  }
45
48
 
49
+ /**
50
+ * Whether cold boot should attempt silent cross-origin OAuth restore for the
51
+ * given web origin.
52
+ *
53
+ * Mirrors the hub-sync WRITE gate (`syncHubAfterSignIn`): only OFFICIAL Oxy web
54
+ * origins participate in cross-app SSO — a random third-party / preview origin
55
+ * must never have its tab bounced to the IdP. Additionally, loopback / local-dev
56
+ * origins are EXCLUDED here even though {@link isOfficialWebOrigin} treats them
57
+ * as official: a developer's local app must never be redirected to a hosted IdP
58
+ * on cold boot (it signs in through the in-app dialog instead). The central IdP
59
+ * hub's own origin is excluded to avoid a wasteful self-hop.
60
+ */
61
+ export function isSilentRestoreEligibleOrigin(origin: string): boolean {
62
+ if (isLoopbackOrigin(origin)) return false;
63
+ if (isIdpHubOrigin()) return false;
64
+ return isOfficialWebOrigin(origin);
65
+ }
66
+
46
67
  export interface SilentOAuthRestoreOptions {
47
68
  oxyServices: OxyServices;
48
69
  clientId: string;
49
70
  redirectUri?: string;
50
71
  scope?: string;
72
+ /**
73
+ * Authorize endpoint override (env-configurable per deployment). Defaults to
74
+ * the production Oxy IdP when unset, so a local/staging app points silent
75
+ * restore at its OWN IdP instead of production `auth.oxy.so`.
76
+ */
77
+ authorizeBaseUrl?: string;
51
78
  }
52
79
 
53
80
  /**
@@ -62,6 +89,12 @@ export async function maybeStartSilentOAuthRestore(
62
89
  const location = (globalThis as { location?: Location }).location;
63
90
  if (!location) return false;
64
91
 
92
+ // Defense-in-depth: never bounce a loopback / non-official / IdP-hub origin to
93
+ // a hosted authorize endpoint (the caller gates on this too).
94
+ if (!isSilentRestoreEligibleOrigin(location.origin)) {
95
+ return false;
96
+ }
97
+
65
98
  if (isCrossOriginRestoreBlocked()) {
66
99
  return false;
67
100
  }
@@ -83,6 +116,7 @@ export async function maybeStartSilentOAuthRestore(
83
116
  );
84
117
 
85
118
  const authorizeUrl = buildOAuthAuthorizeUrl({
119
+ authorizeBaseUrl: opts.authorizeBaseUrl,
86
120
  clientId: opts.clientId,
87
121
  redirectUri,
88
122
  state,