@oxyhq/core 5.2.1 → 5.3.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 (59) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/CrossDomainAuth.js +32 -42
  3. package/dist/cjs/index.js +24 -2
  4. package/dist/cjs/mixins/OxyServices.sso.js +36 -0
  5. package/dist/cjs/mixins/OxyServices.user.js +24 -0
  6. package/dist/cjs/server/index.js +13 -1
  7. package/dist/cjs/session/SessionClient.js +142 -0
  8. package/dist/cjs/session/createSessionClient.js +26 -0
  9. package/dist/cjs/session/projectSessionState.js +75 -0
  10. package/dist/cjs/session/sessionClientHost.js +30 -0
  11. package/dist/cjs/session/socketLoader.js +55 -0
  12. package/dist/cjs/utils/ssoBounce.js +9 -9
  13. package/dist/cjs/utils/ssoEstablish.js +110 -0
  14. package/dist/esm/.tsbuildinfo +1 -1
  15. package/dist/esm/CrossDomainAuth.js +32 -42
  16. package/dist/esm/index.js +14 -0
  17. package/dist/esm/mixins/OxyServices.sso.js +36 -0
  18. package/dist/esm/mixins/OxyServices.user.js +24 -0
  19. package/dist/esm/server/index.js +10 -0
  20. package/dist/esm/session/SessionClient.js +138 -0
  21. package/dist/esm/session/createSessionClient.js +23 -0
  22. package/dist/esm/session/projectSessionState.js +69 -0
  23. package/dist/esm/session/sessionClientHost.js +27 -0
  24. package/dist/esm/session/socketLoader.js +19 -0
  25. package/dist/esm/utils/ssoBounce.js +9 -9
  26. package/dist/esm/utils/ssoEstablish.js +107 -0
  27. package/dist/types/.tsbuildinfo +1 -1
  28. package/dist/types/CrossDomainAuth.d.ts +33 -13
  29. package/dist/types/index.d.ts +7 -0
  30. package/dist/types/mixins/OxyServices.sso.d.ts +24 -0
  31. package/dist/types/mixins/OxyServices.user.d.ts +14 -0
  32. package/dist/types/server/index.d.ts +2 -0
  33. package/dist/types/session/SessionClient.d.ts +55 -0
  34. package/dist/types/session/createSessionClient.d.ts +23 -0
  35. package/dist/types/session/projectSessionState.d.ts +43 -0
  36. package/dist/types/session/sessionClientHost.d.ts +18 -0
  37. package/dist/types/session/socketLoader.d.ts +9 -0
  38. package/dist/types/utils/ssoBounce.d.ts +9 -9
  39. package/dist/types/utils/ssoEstablish.d.ts +85 -0
  40. package/package.json +1 -1
  41. package/src/CrossDomainAuth.ts +33 -44
  42. package/src/__tests__/crossDomainAuth.test.ts +33 -16
  43. package/src/index.ts +24 -0
  44. package/src/mixins/OxyServices.sso.ts +55 -0
  45. package/src/mixins/OxyServices.user.ts +26 -0
  46. package/src/mixins/__tests__/sso.test.ts +41 -0
  47. package/src/server/index.ts +12 -0
  48. package/src/session/SessionClient.ts +175 -0
  49. package/src/session/__tests__/SessionClient.rest.test.ts +79 -0
  50. package/src/session/__tests__/SessionClient.socket.test.ts +132 -0
  51. package/src/session/__tests__/SessionClient.state.test.ts +64 -0
  52. package/src/session/__tests__/sessionIntegration.test.ts +202 -0
  53. package/src/session/createSessionClient.ts +31 -0
  54. package/src/session/projectSessionState.ts +83 -0
  55. package/src/session/sessionClientHost.ts +32 -0
  56. package/src/session/socketLoader.ts +28 -0
  57. package/src/utils/__tests__/ssoEstablish.test.ts +204 -0
  58. package/src/utils/ssoBounce.ts +9 -9
  59. package/src/utils/ssoEstablish.ts +174 -0
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ /**
3
+ * Post-claim durable-session establish hop (web device-flow / "Sign in with
4
+ * Oxy" QR).
5
+ *
6
+ * A WEB device-flow claim (`claimSessionByToken`) plants only IN-MEMORY tokens:
7
+ * unlike a redirect/FedCM/silent sign-in, it never causes the IdP to plant a
8
+ * `fedcm_session` cookie. So a reload has nothing to restore from — the
9
+ * silent-iframe and `/sso` paths find no IdP session and the session is lost.
10
+ *
11
+ * This primitive closes that gap. AFTER the claim has committed and the session
12
+ * has been durably persisted, it performs ONE top-level establish hop through
13
+ * the RP's own per-apex IdP host (`auth.<rp-apex>`), reusing the EXISTING
14
+ * `/sso/establish` endpoint: the server mints a short-lived, host+audience-bound
15
+ * establish-token and returns a fully-formed establish URL; navigating to it
16
+ * plants the durable first-party `fedcm_session` cookie and bounces back to the
17
+ * RP callback with an opaque code the standard `sso-return` cold-boot step
18
+ * exchanges.
19
+ *
20
+ * It reuses the SAME per-origin `sessionStorage` bounce contract
21
+ * (`ssoStateKey` / `ssoGuardKey` / `ssoDestKey`) that {@link buildSsoBounceUrl}
22
+ * primes for the terminal `/sso` bounce, so the post-bounce `sso-return` step
23
+ * (`consumeSsoReturn`) validates the CSRF `state`, exchanges the code, and
24
+ * restores the user's real destination with no extra wiring.
25
+ *
26
+ * Contract:
27
+ * - WEB only — off-web / native it is a no-op returning `false`.
28
+ * - NEVER fires while sitting on the central IdP origin (that would loop the
29
+ * IdP against itself).
30
+ * - Bounce state is persisted ONLY after the establish-URL request succeeds, so
31
+ * a failed request leaves no stale state behind.
32
+ * - SINGLE attempt: the caller invokes this exactly once per successful claim.
33
+ * On ANY failure it does NOT navigate and returns `false`, leaving the
34
+ * committed in-memory session exactly as-is (the user is no worse off than
35
+ * before this hop existed).
36
+ * - Total: never throws. Failures are reported via {@link deps.onError} only.
37
+ */
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.establishIdpSessionAfterClaim = establishIdpSessionAfterClaim;
40
+ const ssoBounce_1 = require("./ssoBounce");
41
+ const OxyServices_sso_1 = require("../mixins/OxyServices.sso");
42
+ /**
43
+ * Perform the post-claim establish hop. Returns `true` when a navigation to the
44
+ * establish URL was initiated (the document is being torn down and replaced by
45
+ * the IdP), `false` on every no-op / failure path.
46
+ *
47
+ * @param client - The exchange surface (`oxyServices.requestSsoEstablishUrl`).
48
+ * @param deps - Injectable web seams; see {@link EstablishAfterClaimDeps}.
49
+ */
50
+ async function establishIdpSessionAfterClaim(client, deps = {}) {
51
+ const isWeb = deps.isWeb ??
52
+ (() => typeof window !== 'undefined' &&
53
+ typeof window.sessionStorage !== 'undefined');
54
+ if (!isWeb()) {
55
+ return false;
56
+ }
57
+ const storage = deps.storage ?? window.sessionStorage;
58
+ const location = deps.location ?? window.location;
59
+ const navigate = deps.navigate ?? ssoBounce_1.ssoNavigate;
60
+ const generateState = deps.generateState ?? OxyServices_sso_1.generateSsoState;
61
+ const now = deps.now ?? (() => Date.now());
62
+ const origin = location.origin;
63
+ // Never establish while sitting on the central IdP itself — that would bounce
64
+ // the IdP against itself. (The device-flow claim can only happen on an RP.)
65
+ if ((0, ssoBounce_1.isCentralIdPOrigin)(origin)) {
66
+ return false;
67
+ }
68
+ const state = generateState();
69
+ let establishUrl;
70
+ try {
71
+ const result = await client.requestSsoEstablishUrl(origin, state);
72
+ if (!result ||
73
+ typeof result.establishUrl !== 'string' ||
74
+ result.establishUrl.length === 0) {
75
+ return false;
76
+ }
77
+ establishUrl = result.establishUrl;
78
+ // Defense-in-depth before a TOP-LEVEL navigation: the establish URL is
79
+ // server-formed, but never follow anything that is not a well-formed https
80
+ // `/sso/establish` on an `auth.` host. A malformed or unexpected URL aborts
81
+ // silently (no navigation, no state persisted) — same total/no-throw
82
+ // contract — rather than navigating the document somewhere unintended.
83
+ let parsedEstablishUrl;
84
+ try {
85
+ parsedEstablishUrl = new URL(establishUrl);
86
+ }
87
+ catch {
88
+ return false;
89
+ }
90
+ if (parsedEstablishUrl.protocol !== 'https:' ||
91
+ parsedEstablishUrl.pathname !== '/sso/establish' ||
92
+ !parsedEstablishUrl.hostname.toLowerCase().startsWith('auth.')) {
93
+ return false;
94
+ }
95
+ // Persist the bounce state ONLY now that the request has succeeded and the
96
+ // URL is validated — so a failed/rejected request never leaves stale state
97
+ // behind. This is the exact contract the terminal `/sso` bounce primes via
98
+ // `buildSsoBounceUrl`, so the post-bounce `sso-return` step (`consumeSsoReturn`)
99
+ // validates `state`, exchanges the returned code, and restores the dest.
100
+ storage.setItem((0, ssoBounce_1.ssoStateKey)(origin), state);
101
+ storage.setItem((0, ssoBounce_1.ssoGuardKey)(origin), String(now()));
102
+ storage.setItem((0, ssoBounce_1.ssoDestKey)(origin), location.href);
103
+ }
104
+ catch (error) {
105
+ deps.onError?.(error);
106
+ return false;
107
+ }
108
+ navigate(establishUrl);
109
+ return true;
110
+ }