@oxyhq/core 5.5.0 → 7.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 (196) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/HttpService.js +7 -4
  3. package/dist/cjs/OxyServices.base.js +7 -102
  4. package/dist/cjs/boot/coldBootV2.js +350 -0
  5. package/dist/cjs/boot/deviceBootReturn.js +152 -0
  6. package/dist/cjs/crypto/keyManager.js +95 -0
  7. package/dist/cjs/i18n/locales/en-US.json +13 -1
  8. package/dist/cjs/i18n/locales/es-ES.json +13 -1
  9. package/dist/cjs/i18n/locales/locales/en-US.json +13 -1
  10. package/dist/cjs/i18n/locales/locales/es-ES.json +13 -1
  11. package/dist/cjs/index.js +54 -45
  12. package/dist/cjs/mixins/OxyServices.accounts.js +13 -26
  13. package/dist/cjs/mixins/OxyServices.auth.js +66 -201
  14. package/dist/cjs/mixins/OxyServices.deviceBoot.js +119 -0
  15. package/dist/cjs/mixins/index.js +8 -17
  16. package/dist/cjs/server/index.js +2 -2
  17. package/dist/cjs/session/SessionClient.js +40 -1
  18. package/dist/cjs/session/authStateStore.js +284 -0
  19. package/dist/cjs/session/refresh.js +264 -0
  20. package/dist/cjs/shared/utils/debugUtils.js +3 -3
  21. package/dist/cjs/utils/accountUtils.js +1 -55
  22. package/dist/cjs/utils/authWebUrl.js +10 -39
  23. package/dist/cjs/utils/coldBoot.js +10 -8
  24. package/dist/cjs/utils/platform.js +19 -0
  25. package/dist/cjs/utils/registrableApex.js +49 -0
  26. package/dist/cjs/utils/ssoBounce.js +15 -362
  27. package/dist/cjs/utils/validationUtils.js +57 -0
  28. package/dist/esm/.tsbuildinfo +1 -1
  29. package/dist/esm/HttpService.js +7 -4
  30. package/dist/esm/OxyServices.base.js +7 -102
  31. package/dist/esm/boot/coldBootV2.js +344 -0
  32. package/dist/esm/boot/deviceBootReturn.js +146 -0
  33. package/dist/esm/crypto/keyManager.js +95 -0
  34. package/dist/esm/i18n/locales/en-US.json +13 -1
  35. package/dist/esm/i18n/locales/es-ES.json +13 -1
  36. package/dist/esm/i18n/locales/locales/en-US.json +13 -1
  37. package/dist/esm/i18n/locales/locales/es-ES.json +13 -1
  38. package/dist/esm/index.js +34 -17
  39. package/dist/esm/mixins/OxyServices.accounts.js +13 -26
  40. package/dist/esm/mixins/OxyServices.auth.js +66 -201
  41. package/dist/esm/mixins/OxyServices.deviceBoot.js +116 -0
  42. package/dist/esm/mixins/index.js +8 -17
  43. package/dist/esm/server/index.js +1 -1
  44. package/dist/esm/session/SessionClient.js +40 -1
  45. package/dist/esm/session/authStateStore.js +278 -0
  46. package/dist/esm/session/refresh.js +257 -0
  47. package/dist/esm/shared/utils/debugUtils.js +3 -3
  48. package/dist/esm/utils/accountUtils.js +0 -53
  49. package/dist/esm/utils/authWebUrl.js +9 -37
  50. package/dist/esm/utils/coldBoot.js +10 -8
  51. package/dist/esm/utils/platform.js +18 -0
  52. package/dist/esm/utils/registrableApex.js +46 -0
  53. package/dist/esm/utils/ssoBounce.js +14 -345
  54. package/dist/esm/utils/validationUtils.js +56 -0
  55. package/dist/types/.tsbuildinfo +1 -1
  56. package/dist/types/HttpService.d.ts +14 -1
  57. package/dist/types/OxyServices.base.d.ts +0 -52
  58. package/dist/types/OxyServices.d.ts +0 -25
  59. package/dist/types/boot/coldBootV2.d.ts +76 -0
  60. package/dist/types/boot/deviceBootReturn.d.ts +83 -0
  61. package/dist/types/crypto/keyManager.d.ts +21 -0
  62. package/dist/types/index.d.ts +14 -21
  63. package/dist/types/mixins/OxyServices.accounts.d.ts +24 -29
  64. package/dist/types/mixins/OxyServices.analytics.d.ts +0 -2
  65. package/dist/types/mixins/OxyServices.appData.d.ts +0 -2
  66. package/dist/types/mixins/OxyServices.assets.d.ts +0 -2
  67. package/dist/types/mixins/OxyServices.auth.d.ts +35 -77
  68. package/dist/types/mixins/OxyServices.civic.d.ts +0 -2
  69. package/dist/types/mixins/OxyServices.connectedApps.d.ts +0 -2
  70. package/dist/types/mixins/OxyServices.contacts.d.ts +0 -2
  71. package/dist/types/mixins/OxyServices.deviceBoot.d.ts +110 -0
  72. package/dist/types/mixins/OxyServices.devices.d.ts +0 -2
  73. package/dist/types/mixins/OxyServices.features.d.ts +0 -2
  74. package/dist/types/mixins/OxyServices.identity.d.ts +0 -2
  75. package/dist/types/mixins/OxyServices.language.d.ts +0 -2
  76. package/dist/types/mixins/OxyServices.links.d.ts +0 -2
  77. package/dist/types/mixins/OxyServices.location.d.ts +0 -2
  78. package/dist/types/mixins/OxyServices.nodes.d.ts +0 -2
  79. package/dist/types/mixins/OxyServices.payment.d.ts +0 -2
  80. package/dist/types/mixins/OxyServices.privacy.d.ts +0 -2
  81. package/dist/types/mixins/OxyServices.reputation.d.ts +0 -2
  82. package/dist/types/mixins/OxyServices.security.d.ts +0 -2
  83. package/dist/types/mixins/OxyServices.topics.d.ts +0 -2
  84. package/dist/types/mixins/OxyServices.user.d.ts +0 -2
  85. package/dist/types/mixins/OxyServices.utility.d.ts +0 -2
  86. package/dist/types/mixins/index.d.ts +5 -9
  87. package/dist/types/models/interfaces.d.ts +0 -67
  88. package/dist/types/models/session.d.ts +4 -5
  89. package/dist/types/server/index.d.ts +1 -1
  90. package/dist/types/session/SessionClient.d.ts +26 -1
  91. package/dist/types/session/authStateStore.d.ts +119 -0
  92. package/dist/types/session/refresh.d.ts +93 -0
  93. package/dist/types/shared/utils/debugUtils.d.ts +3 -3
  94. package/dist/types/utils/accountUtils.d.ts +2 -17
  95. package/dist/types/utils/authWebUrl.d.ts +9 -35
  96. package/dist/types/utils/coldBoot.d.ts +17 -14
  97. package/dist/types/utils/platform.d.ts +14 -0
  98. package/dist/types/utils/registrableApex.d.ts +31 -0
  99. package/dist/types/utils/ssoBounce.d.ts +14 -280
  100. package/dist/types/utils/validationUtils.d.ts +15 -0
  101. package/package.json +2 -2
  102. package/src/HttpService.ts +20 -4
  103. package/src/OxyServices.base.ts +7 -112
  104. package/src/OxyServices.ts +0 -38
  105. package/src/boot/__tests__/coldBootV2.test.ts +317 -0
  106. package/src/boot/__tests__/deviceBootReturn.test.ts +158 -0
  107. package/src/boot/coldBootV2.ts +426 -0
  108. package/src/boot/deviceBootReturn.ts +195 -0
  109. package/src/crypto/__tests__/sharedDeviceToken.test.ts +24 -0
  110. package/src/crypto/keyManager.ts +101 -0
  111. package/src/i18n/locales/en-US.json +13 -1
  112. package/src/i18n/locales/es-ES.json +13 -1
  113. package/src/index.ts +75 -65
  114. package/src/mixins/OxyServices.accounts.ts +27 -39
  115. package/src/mixins/OxyServices.auth.ts +78 -253
  116. package/src/mixins/OxyServices.deviceBoot.ts +146 -0
  117. package/src/mixins/__tests__/OxyServices.deviceBoot.test.ts +107 -0
  118. package/src/mixins/__tests__/accounts.test.ts +17 -44
  119. package/src/mixins/__tests__/passwordSignIn.test.ts +91 -0
  120. package/src/mixins/index.ts +10 -23
  121. package/src/models/interfaces.ts +0 -79
  122. package/src/models/session.ts +4 -5
  123. package/src/server/index.ts +1 -1
  124. package/src/session/SessionClient.ts +53 -2
  125. package/src/session/__tests__/SessionClient.additive.test.ts +92 -0
  126. package/src/session/__tests__/SessionClient.rest.test.ts +25 -0
  127. package/src/session/__tests__/SessionClient.state.test.ts +18 -5
  128. package/src/session/__tests__/authStateStore.test.ts +209 -0
  129. package/src/session/__tests__/refresh.test.ts +256 -0
  130. package/src/session/authStateStore.ts +335 -0
  131. package/src/session/refresh.ts +334 -0
  132. package/src/shared/utils/debugUtils.ts +3 -3
  133. package/src/utils/__tests__/authWebUrl.test.ts +5 -40
  134. package/src/utils/__tests__/registrableApex.test.ts +62 -0
  135. package/src/utils/__tests__/validationUtils.test.ts +30 -0
  136. package/src/utils/accountUtils.ts +2 -65
  137. package/src/utils/authWebUrl.ts +9 -39
  138. package/src/utils/coldBoot.ts +17 -14
  139. package/src/utils/platform.ts +21 -0
  140. package/src/utils/registrableApex.ts +45 -0
  141. package/src/utils/ssoBounce.ts +14 -393
  142. package/src/utils/validationUtils.ts +62 -0
  143. package/dist/cjs/AuthManager.js +0 -1110
  144. package/dist/cjs/AuthManagerTypes.js +0 -13
  145. package/dist/cjs/CrossDomainAuth.js +0 -206
  146. package/dist/cjs/mixins/OxyServices.fedcm.js +0 -823
  147. package/dist/cjs/mixins/OxyServices.redirect.js +0 -95
  148. package/dist/cjs/mixins/OxyServices.silent.js +0 -204
  149. package/dist/cjs/mixins/OxyServices.sso.js +0 -208
  150. package/dist/cjs/utils/fapiAutoDetect.js +0 -99
  151. package/dist/cjs/utils/ssoEstablish.js +0 -110
  152. package/dist/cjs/utils/ssoReturn.js +0 -275
  153. package/dist/esm/AuthManager.js +0 -1105
  154. package/dist/esm/AuthManagerTypes.js +0 -12
  155. package/dist/esm/CrossDomainAuth.js +0 -201
  156. package/dist/esm/mixins/OxyServices.fedcm.js +0 -821
  157. package/dist/esm/mixins/OxyServices.redirect.js +0 -92
  158. package/dist/esm/mixins/OxyServices.silent.js +0 -202
  159. package/dist/esm/mixins/OxyServices.sso.js +0 -204
  160. package/dist/esm/utils/fapiAutoDetect.js +0 -95
  161. package/dist/esm/utils/ssoEstablish.js +0 -107
  162. package/dist/esm/utils/ssoReturn.js +0 -271
  163. package/dist/types/AuthManager.d.ts +0 -380
  164. package/dist/types/AuthManagerTypes.d.ts +0 -81
  165. package/dist/types/CrossDomainAuth.d.ts +0 -164
  166. package/dist/types/mixins/OxyServices.fedcm.d.ts +0 -331
  167. package/dist/types/mixins/OxyServices.redirect.d.ts +0 -92
  168. package/dist/types/mixins/OxyServices.silent.d.ts +0 -132
  169. package/dist/types/mixins/OxyServices.sso.d.ts +0 -138
  170. package/dist/types/utils/fapiAutoDetect.d.ts +0 -56
  171. package/dist/types/utils/ssoEstablish.d.ts +0 -85
  172. package/dist/types/utils/ssoReturn.d.ts +0 -156
  173. package/src/AuthManager.ts +0 -1269
  174. package/src/AuthManagerTypes.ts +0 -86
  175. package/src/CrossDomainAuth.ts +0 -243
  176. package/src/__tests__/authManager.cookiePath.test.ts +0 -390
  177. package/src/__tests__/authManager.security.test.ts +0 -377
  178. package/src/__tests__/crossDomainAuth.test.ts +0 -116
  179. package/src/__tests__/establishDeviceRefreshSlot.test.ts +0 -221
  180. package/src/mixins/OxyServices.fedcm.ts +0 -1026
  181. package/src/mixins/OxyServices.redirect.ts +0 -122
  182. package/src/mixins/OxyServices.silent.ts +0 -272
  183. package/src/mixins/OxyServices.sso.ts +0 -261
  184. package/src/mixins/__tests__/constructorAuthWebUrl.test.ts +0 -85
  185. package/src/mixins/__tests__/fedcm.test.ts +0 -667
  186. package/src/mixins/__tests__/sessionBaseUrl.test.ts +0 -61
  187. package/src/mixins/__tests__/silent.test.ts +0 -102
  188. package/src/mixins/__tests__/sso.test.ts +0 -228
  189. package/src/utils/__tests__/consumeSsoReturn.test.ts +0 -816
  190. package/src/utils/__tests__/fapiAutoDetect.test.ts +0 -183
  191. package/src/utils/__tests__/ssoBounce.test.ts +0 -219
  192. package/src/utils/__tests__/ssoEstablish.test.ts +0 -204
  193. package/src/utils/__tests__/ssoReturn.test.ts +0 -276
  194. package/src/utils/fapiAutoDetect.ts +0 -91
  195. package/src/utils/ssoEstablish.ts +0 -174
  196. package/src/utils/ssoReturn.ts +0 -389
@@ -1,401 +1,22 @@
1
1
  /**
2
- * Central cross-domain SSO bounce per-origin sessionStorage keys, the bounce
3
- * URL builder, and the small pure predicates shared by every consumer's
4
- * cold-boot `sso-return` / `sso-bounce` steps and bfcache `pageshow`
5
- * re-evaluation.
2
+ * SSO callback path constant.
6
3
  *
7
- * This is the single source of truth for the SSO bounce wire/storage contract.
8
- * `@oxyhq/auth` (`WebOxyProvider`) and `@oxyhq/services` (`OxyContext`) both
9
- * consume these helpers so the two providers behave identically.
4
+ * The client SSO-bounce machinery (per-origin sessionStorage keys, the bounce
5
+ * URL builder, the `guardActive` / `allowSsoBounce` / `getSsoCallbackBootstrapScript`
6
+ * predicates, …) was removed in the device-first cutover RP apps no longer
7
+ * bounce through `auth.oxy.so`. The one survivor is this path constant, still
8
+ * referenced by the api SSO controller and the IdP (both lista B, gated on the
9
+ * ecosystem bump). `@oxyhq/core/server` re-exports it for the api.
10
10
  *
11
- * TRUE central SSO (Google/Meta/Clerk style) works like this for a Relying
12
- * Party (mention.earth, homiio.com, alia.onl, …) with no local session:
13
- *
14
- * 1. `sso-bounce` (terminal, once): a TOP-LEVEL navigation to
15
- * `auth.oxy.so/sso?prompt=none&client_id=<origin>&return_to=<origin>{@link SSO_CALLBACK_PATH}&state=<s>`.
16
- * Before navigating it records, in this origin's `sessionStorage`, the
17
- * CSRF `state` ({@link ssoStateKey}), a guard timestamp ({@link ssoGuardKey},
18
- * the loop breaker), and the real destination URL ({@link ssoDestKey}) to
19
- * restore after the callback.
20
- * 2. The central IdP worker reads its first-party `fedcm_session`, mints a
21
- * session, stores it under an opaque single-use `code`, and 303-redirects
22
- * back to `<origin>{@link SSO_CALLBACK_PATH}#oxy_sso=ok&code=<code>&state=<s>`
23
- * (or `#oxy_sso=none` / `#oxy_sso=error`).
24
- * 3. `sso-return` parses the fragment (`parseSsoReturnFragment`), validates
25
- * `state`, exchanges the `code` via `oxyServices.exchangeSsoCode`, commits
26
- * the session, then restores the original destination.
27
- *
28
- * Loop proof (logged-out): first load all steps skip → `sso-bounce` sets
29
- * guard/state/dest + the outcome-independent attempted-flag
30
- * ({@link ssoAttemptedKey}) and navigates; the IdP (no central session) returns
31
- * `#oxy_sso=none`; the callback load's `sso-return` sees `none`, sets the
32
- * NO_SESSION flag ({@link ssoNoSessionKey}), and `sso-bounce` is then disabled.
33
- * Exactly ONE bounce, no loop. An interrupted bounce (user hit back
34
- * mid-redirect) self-heals once the {@link SSO_GUARD_TTL_MS} guard TTL lapses.
35
- * The attempted-flag is the definitive, outcome-INDEPENDENT loop breaker: it is
36
- * set pre-bounce so even if the return-side NO_SESSION write never lands, the
37
- * bounce can never re-fire this tab after the self-heal TTL lapses.
38
- *
39
- * All state lives in `sessionStorage` (per tab, cleared on tab close) and is
40
- * keyed per-origin so two RPs hosted in the same browser never collide. The
41
- * key strings and the 30s TTL are a wire/storage contract — they MUST match
42
- * the values the IdP and every consumer expect and must not change lightly.
11
+ * LEGACY(old-sdk): `SSO_CALLBACK_PATH` survives ONLY for the lista-B api/IdP SSO
12
+ * surface. Deletable once Homiio/Allo/Alia/Syra are bumped off the old SDK AND
13
+ * CloudWatch `/oxy/ecs` shows the `/sso*` + `/fedcm/*` routes quiet — the
14
+ * F-final sweep should remove this file then.
43
15
  */
44
16
 
45
- import { CENTRAL_AUTH_URL, resolveCentralAuthUrl } from './authWebUrl';
46
-
47
17
  /**
48
- * The RP callback path the central IdP redirects back to after a bounce. The
49
- * SSO result is delivered in the fragment of this URL; the `sso-return` step
50
- * consumes it and then restores the user's real destination (stored under
51
- * {@link ssoDestKey}), so the user never lingers on this internal path.
18
+ * The RP callback path the central IdP redirects back to after a legacy SSO
19
+ * bounce. Kept as the single source of truth so the api/IdP never hardcode the
20
+ * literal.
52
21
  */
53
22
  export const SSO_CALLBACK_PATH = '/__oxy/sso-callback';
54
-
55
- /**
56
- * Self-healing TTL (ms) for the bounce guard. An in-flight bounce sets a
57
- * timestamp guard; if the bounce is interrupted before the callback lands
58
- * (e.g. the user navigates back mid-redirect), the guard would otherwise pin
59
- * the RP signed-out forever. After this window the guard is treated as stale
60
- * and a fresh single bounce is permitted. 30s comfortably exceeds a real
61
- * redirect round-trip while keeping a crash short-lived.
62
- */
63
- export const SSO_GUARD_TTL_MS = 30_000;
64
-
65
- const STATE_KEY_PREFIX = 'oxy_sso_state:';
66
- const GUARD_KEY_PREFIX = 'oxy_sso_guard:';
67
- const DEST_KEY_PREFIX = 'oxy_sso_dest:';
68
- const NO_SESSION_KEY_PREFIX = 'oxy_sso_no_session:';
69
- const ATTEMPTED_KEY_PREFIX = 'oxy_sso_attempted:';
70
- const CALLBACK_BOOTSTRAP_KEY_PREFIX = 'oxy_sso_callback_bootstrap:';
71
- const PRIOR_SESSION_KEY_PREFIX = 'oxy_sso_prior_session:';
72
- const SIGNED_OUT_KEY_PREFIX = 'oxy_signed_out:';
73
- const OUTCOME_KEY_PREFIX = 'oxy_sso_outcome:';
74
-
75
- /** Per-origin CSRF state key (matched on return to defeat fragment forgery). */
76
- export function ssoStateKey(origin: string): string {
77
- return `${STATE_KEY_PREFIX}${origin}`;
78
- }
79
-
80
- /** Per-origin bounce guard key (a timestamp; loop breaker + self-heal TTL). */
81
- export function ssoGuardKey(origin: string): string {
82
- return `${GUARD_KEY_PREFIX}${origin}`;
83
- }
84
-
85
- /** Per-origin destination key (the real URL to restore after the callback). */
86
- export function ssoDestKey(origin: string): string {
87
- return `${DEST_KEY_PREFIX}${origin}`;
88
- }
89
-
90
- /**
91
- * Per-origin "the central IdP has no session for me" key. Set after a
92
- * `none`/`error` return (or a failed/forged exchange) so `sso-bounce` does not
93
- * fire again this tab — the definitive loop breaker.
94
- */
95
- export function ssoNoSessionKey(origin: string): string {
96
- return `${NO_SESSION_KEY_PREFIX}${origin}`;
97
- }
98
-
99
- /**
100
- * Per-origin, OUTCOME-INDEPENDENT once-guard. Set in `sessionStorage` BEFORE
101
- * the terminal SSO bounce navigates. Gates the bounce so the silent
102
- * cross-domain probe fires AT MOST ONCE per tab session — independent of
103
- * whether the return-side NO_SESSION flag ever lands. The definitive loop
104
- * breaker; survives the 30s self-heal `ssoGuardKey` TTL. Cleared only on an
105
- * explicit sign-out/clear so a later cold boot (after the user signs in
106
- * centrally) can probe again.
107
- */
108
- export function ssoAttemptedKey(origin: string): string {
109
- return `${ATTEMPTED_KEY_PREFIX}${origin}`;
110
- }
111
-
112
- /**
113
- * Per-origin DURABLE "this device/origin has had a signed-in Oxy session
114
- * before" hint.
115
- *
116
- * Unlike every other key in this module — which lives in per-tab
117
- * `sessionStorage` — this hint is written to DURABLE storage (web
118
- * `localStorage`; the services provider uses its own `storageKeyPrefix`-scoped
119
- * key in `@oxyhq/services`). It is set whenever a session is established or
120
- * restored and survives a session expiring; it is cleared ONLY on an explicit
121
- * full sign-out. It exists purely to drive {@link allowSsoBounce}: a returning
122
- * visitor (hint present) whose local session has lapsed still gets ONE terminal
123
- * `/sso` establish bounce to recover a session that lives only at the central
124
- * IdP, while a truly first-time anonymous visitor is never force-bounced.
125
- */
126
- export function ssoPriorSessionKey(origin: string): string {
127
- return `${PRIOR_SESSION_KEY_PREFIX}${origin}`;
128
- }
129
-
130
- /**
131
- * Per-origin DURABLE "the user DELIBERATELY signed out on this device/origin"
132
- * flag.
133
- *
134
- * Like {@link ssoPriorSessionKey} this lives in DURABLE storage (web
135
- * `localStorage`; services uses its own `storageKeyPrefix`-scoped key), NOT the
136
- * per-tab `sessionStorage` the loop-breaker keys use — it must survive a reload.
137
- *
138
- * It exists purely to suppress AUTOMATIC silent restore after a deliberate
139
- * sign-out: a still-live IdP session (the central `fedcm_session`) would
140
- * otherwise let the per-apex `/auth/silent` iframe re-mint a session on the
141
- * very next cold boot, so a user who pressed "Sign out" gets silently signed
142
- * back in on reload. With this flag set, that silent cold-boot step is
143
- * skipped while the Gmail-style returning-account fast-path is otherwise
144
- * preserved.
145
- *
146
- * Lifecycle (mirrors the existing gate machinery — set on a definitive event,
147
- * cleared on its inverse):
148
- * - SET on EXPLICIT full sign-out (alongside clearing the prior-session hint
149
- * and the SSO bounce state).
150
- * - CLEARED on ANY deliberate sign-in (password, account switch, device
151
- * claim) so a real sign-in fully re-enables silent restore — there is no
152
- * "stuck signed out" state.
153
- *
154
- * NOTE: this gates only AUTOMATIC/silent restore. An INTERACTIVE sign-in always
155
- * clears it first, so the user can always sign back in.
156
- */
157
- export function ssoSignedOutKey(origin: string): string {
158
- return `${SIGNED_OUT_KEY_PREFIX}${origin}`;
159
- }
160
-
161
- /**
162
- * Per-origin key holding the LAST consumed SSO-return outcome (`ok` | `none` |
163
- * `error`, plus an optional machine-readable `reason` on the non-`ok` outcomes).
164
- *
165
- * Lives in per-tab `sessionStorage` like the other loop-breaker keys, and for
166
- * the same reason: a `none`/`error` return HARD-navigates the RP off the
167
- * internal callback path back to its real destination (a fresh document load),
168
- * so the outcome an RP wants to render ("the central IdP had no session — show a
169
- * branded sign-in screen instead of bouncing again") must survive that
170
- * round-trip. The RP reads it on the destination load to decide whether an
171
- * AUTOMATIC (guard-driven) sign-in should re-bounce or defer to a user gesture.
172
- *
173
- * Written as a small JSON blob (`{kind, reason?}`). Set whenever a return is
174
- * consumed; cleared on a successful session commit and on an explicit
175
- * user-gesture sign-in / full sign-out (so a deliberate retry is never
176
- * suppressed by a prior automatic none/error).
177
- */
178
- export function ssoOutcomeKey(origin: string): string {
179
- return `${OUTCOME_KEY_PREFIX}${origin}`;
180
- }
181
-
182
- /**
183
- * Per-origin marker written by the pre-hydration callback bootstrap.
184
- *
185
- * Static Expo exports render unknown paths as `+not-found`; on
186
- * `/__oxy/sso-callback` that can fail hydration before the React provider has a
187
- * chance to run `consumeSsoReturn`. The bootstrap runs in the HTML head, moves
188
- * the URL to a hydratable route while preserving the SSO fragment, and writes
189
- * this marker so `consumeSsoReturn` still restores the original destination as
190
- * if the page were physically on the callback path.
191
- */
192
- export function ssoCallbackBootstrapKey(origin: string): string {
193
- return `${CALLBACK_BOOTSTRAP_KEY_PREFIX}${origin}`;
194
- }
195
-
196
- /**
197
- * Inline script for Expo/static web apps.
198
- *
199
- * Must run before the app bundle hydrates. It is intentionally tiny and
200
- * dependency-free: if the browser lands on the internal callback route with an
201
- * Oxy SSO fragment, it marks the handoff and rewrites the path to `/` while
202
- * preserving `#oxy_sso=...`. The normal SDK cold-boot `sso-return` step then
203
- * consumes the fragment from a route that can hydrate. If the internal route is
204
- * reached without a valid SSO fragment, it leaves the route via a hard root
205
- * navigation because there is no session material to preserve.
206
- */
207
- export function getSsoCallbackBootstrapScript(): string {
208
- const callbackPath = JSON.stringify(SSO_CALLBACK_PATH);
209
- const bootstrapPrefix = JSON.stringify(CALLBACK_BOOTSTRAP_KEY_PREFIX);
210
-
211
- return `(function(){var p=${callbackPath};if(window.location.pathname!==p)return;var h=window.location.hash||"";if(!/(?:^#|&)oxy_sso=(?:ok|none|error)(?:&|$)/.test(h)){window.location.replace("/");return;}try{window.sessionStorage.setItem(${bootstrapPrefix}+window.location.origin,"1");}catch(e){window.__oxySsoCallbackBootstrapError=e instanceof Error?e.message:String(e);}try{window.history.replaceState(null,"","/"+h);}catch(e){window.__oxySsoCallbackBootstrapError=e instanceof Error?e.message:String(e);window.location.replace("/"+h);}})();`;
212
- }
213
-
214
- /**
215
- * Perform the terminal top-level SSO bounce navigation.
216
- *
217
- * A thin wrapper over `window.location.assign(url)` so the single navigation
218
- * seam lives in one place (and stays mockable in tests, where jsdom's
219
- * `Location.assign` is a non-configurable native method). In production this is
220
- * exactly `window.location.assign` — the document is torn down and replaced by
221
- * the central IdP page. Off-browser (SSR / native) it is a no-op: native never
222
- * bounces.
223
- */
224
- export function ssoNavigate(url: string): void {
225
- if (typeof window === 'undefined' || typeof window.location === 'undefined') {
226
- return;
227
- }
228
- window.location.assign(url);
229
- }
230
-
231
- /**
232
- * Build the central IdP `/sso` bounce URL for an RP.
233
- *
234
- * Pure (no DOM access) so it is unit-testable and shared by every consumer's
235
- * terminal `sso-bounce` step. The IdP reads `client_id` (the RP origin) and
236
- * `return_to` to mint an origin-bound opaque code and 303-redirect back.
237
- *
238
- * The IdP base is resolved via {@link resolveCentralAuthUrl} so an explicit
239
- * `authWebUrl` override (e.g. a staging IdP) drives the SSO bounce exactly the
240
- * way it drives FedCM. When omitted, the central default {@link CENTRAL_AUTH_URL}
241
- * is used.
242
- *
243
- * @param origin - The RP origin (`window.location.origin`).
244
- * @param state - The CSRF state minted for this bounce.
245
- * @param authWebUrl - Optional explicit IdP base URL override. Falls back to
246
- * the central default when `undefined`/empty.
247
- * @returns The absolute `<idp-origin>/sso?...` URL string.
248
- */
249
- export function buildSsoBounceUrl(
250
- origin: string,
251
- state: string,
252
- authWebUrl?: string,
253
- ): string {
254
- const url = new URL('/sso', resolveCentralAuthUrl(authWebUrl));
255
- url.searchParams.set('prompt', 'none');
256
- url.searchParams.set('client_id', origin);
257
- url.searchParams.set('return_to', origin + SSO_CALLBACK_PATH);
258
- url.searchParams.set('state', state);
259
- return url.toString();
260
- }
261
-
262
- /**
263
- * Whether `origin` IS the central IdP origin. The RP must NEVER bounce while
264
- * sitting on `auth.oxy.so` itself — doing so would loop the IdP against itself.
265
- *
266
- * Both sides are normalised via `new URL(...).origin` so a trailing-slash or
267
- * path difference never defeats the guard. Returns `false` on any parse
268
- * failure (an unparseable candidate is, by definition, not the central IdP).
269
- */
270
- export function isCentralIdPOrigin(origin: string): boolean {
271
- let centralOrigin: string;
272
- try {
273
- centralOrigin = new URL(CENTRAL_AUTH_URL).origin;
274
- } catch {
275
- return false;
276
- }
277
- let candidateOrigin: string;
278
- try {
279
- candidateOrigin = new URL(origin).origin;
280
- } catch {
281
- return false;
282
- }
283
- return candidateOrigin === centralOrigin;
284
- }
285
-
286
- /**
287
- * Read the bounce guard and decide whether it is still ACTIVE.
288
- *
289
- * Active means: a guard value is present AND it parses to a finite timestamp
290
- * AND less than {@link SSO_GUARD_TTL_MS} has elapsed since it was set. An active
291
- * guard disables `sso-bounce` (a bounce is already in flight this tab). A
292
- * missing, malformed, or expired guard is NOT active, so a fresh bounce may
293
- * proceed (this is the 30s self-heal for an interrupted bounce).
294
- *
295
- * Defensive: a `getItem` that throws (e.g. a locked/disabled storage) is
296
- * treated as "not active" so the guard never wedges the flow.
297
- *
298
- * @param storage - The session storage to read (injected for testability).
299
- * @param origin - The page origin whose guard to evaluate.
300
- * @param now - Current epoch ms (injected for deterministic tests). Defaults to
301
- * `Date.now()`.
302
- */
303
- export function guardActive(
304
- storage: Pick<Storage, 'getItem'>,
305
- origin: string,
306
- now: number = Date.now(),
307
- ): boolean {
308
- let raw: string | null;
309
- try {
310
- raw = storage.getItem(ssoGuardKey(origin));
311
- } catch {
312
- return false;
313
- }
314
- if (raw === null || raw.length === 0) {
315
- return false;
316
- }
317
- const ts = Number(raw);
318
- if (!Number.isFinite(ts)) {
319
- return false;
320
- }
321
- return now - ts < SSO_GUARD_TTL_MS;
322
- }
323
-
324
- /**
325
- * Whether AUTOMATIC silent restore is SUPPRESSED for this origin because the
326
- * user deliberately signed out (the durable {@link ssoSignedOutKey} flag).
327
- *
328
- * When `true`, the silent cold-boot step that can re-mint a session from a
329
- * still-live IdP session WITHOUT user intent — the per-apex
330
- * `/auth/silent` iframe — MUST be skipped, so a user who pressed "Sign out" is
331
- * not silently signed back in on the next reload. Interactive sign-in clears the
332
- * flag, so this never blocks a deliberate re-sign-in.
333
- *
334
- * Defensive: a `getItem` that throws (locked/disabled storage) is treated as NOT
335
- * suppressed, so the gate fails toward the normal restore behaviour rather than
336
- * wedging the user out.
337
- *
338
- * @param storage - The DURABLE storage to read (web `localStorage`; injected for
339
- * testability).
340
- * @param origin - The page origin whose signed-out flag to evaluate.
341
- */
342
- export function silentRestoreSuppressed(
343
- storage: Pick<Storage, 'getItem'>,
344
- origin: string,
345
- ): boolean {
346
- try {
347
- return storage.getItem(ssoSignedOutKey(origin)) === '1';
348
- } catch {
349
- return false;
350
- }
351
- }
352
-
353
- /**
354
- * Inputs to the smart {@link allowSsoBounce} gate.
355
- */
356
- export interface SsoBounceGate {
357
- /**
358
- * Whether this device/origin has had a signed-in Oxy session before (the
359
- * durable {@link ssoPriorSessionKey} hint). Set whenever a session is
360
- * established or restored; survives session expiry; cleared only on explicit
361
- * full sign-out. `true` ⇒ a returning visitor.
362
- */
363
- readonly hasPriorSession: boolean;
364
- /**
365
- * Whether a local/stored session was recovered earlier this cold boot. At the
366
- * terminal bounce gate this is effectively always `false` (an earlier step
367
- * would have won and short-circuited), but it is part of the contract — "no
368
- * prior hint AND no local session" — so it is passed explicitly for fidelity
369
- * and robustness.
370
- */
371
- readonly hasLocalSession: boolean;
372
- }
373
-
374
- /**
375
- * Decide whether the terminal `/sso` establish-bounce is ALLOWED for this
376
- * visitor (the smart `enabled` gate for the `sso-bounce` cold-boot step).
377
- *
378
- * The terminal bounce is the ONLY cold-boot step that can recover a session
379
- * that lives SOLELY at the central IdP — the cross-apex Relying-Party case
380
- * (e.g. `mention.earth`, a different apex from `oxy.so`) whose device-local
381
- * session has expired and whose `Domain=oxy.so` refresh cookie never reaches
382
- * `api.<apex>`. It is also what plants the first-party per-apex `fedcm_session`
383
- * cookie that the EARLIER `silent-iframe` step later relies on. So it must fire
384
- * for a RETURNING user, yet it must NOT force a truly first-time anonymous
385
- * visitor off to the IdP.
386
- *
387
- * - ALLOW when there is a prior-signed-in hint OR a local session was
388
- * recovered this boot (a returning user) — so a central-only cross-domain
389
- * session recovers via ONE bounce, after which the per-apex cookie is
390
- * planted and subsequent loads restore silently with no bounce.
391
- * - else (no hint, no local session) SUPPRESS — a first-time anonymous
392
- * visitor browses without a forced redirect.
393
- *
394
- * This is the smart DEFAULT and the ONLY behaviour: apps never configure it.
395
- * It is also the GATE DECISION ONLY — callers still apply the per-tab loop
396
- * guards (`ssoAttemptedKey`, `ssoNoSessionKey`, {@link guardActive}) so an
397
- * allowed bounce still fires at most once per cold boot.
398
- */
399
- export function allowSsoBounce(gate: SsoBounceGate): boolean {
400
- return gate.hasPriorSession || gate.hasLocalSession;
401
- }
@@ -39,6 +39,68 @@ export function isValidPassword(password: string): boolean {
39
39
  return PASSWORD_REGEX.test(password);
40
40
  }
41
41
 
42
+ /**
43
+ * Display-name character policy.
44
+ *
45
+ * A clean display name is composed ONLY of:
46
+ * - letters of any script (`\p{L}`),
47
+ * - combining marks / accents (`\p{M}`, e.g. the acute accent in a decomposed
48
+ * "é"),
49
+ * - Unicode space separators (`\p{Zs}`: the ASCII space, NBSP, ideographic
50
+ * space, …) — but NOT control whitespace such as tab, newline, or carriage
51
+ * return, which would break layout or enable multi-line spoofing,
52
+ * - the straight apostrophe (`'`, e.g. "O'Brien").
53
+ *
54
+ * Everything else is rejected: emoji (🐧), symbols (⁂ ⏚), `:emoji:` shortcodes,
55
+ * digits, hyphens, dots, control whitespace (tab/newline/CR), and any other
56
+ * punctuation. The allowed set `\p{L}\p{M}\p{Zs}'` explicitly EXCLUDES `<`, `>`,
57
+ * `&`, and `"`, so a value that passes this predicate can never contain an
58
+ * HTML/XSS vector.
59
+ *
60
+ * This is the SINGLE definition of the policy, shared between the API 400-gate
61
+ * (`@oxyhq/api` `displayNameSanitize.ts`) and client-side inline validation
62
+ * (the RN profile editor) so the two can never drift. It is platform-agnostic
63
+ * (no react/react-native/expo).
64
+ */
65
+
66
+ /**
67
+ * Single test for the presence of a disallowed character (non-global). The
68
+ * whitespace class is `\p{Zs}` (space separators only), NOT `\s` — the latter
69
+ * would admit tab/newline/carriage return, which break layout and enable
70
+ * multi-line spoofing.
71
+ */
72
+ const DISALLOWED_PROBE = /[^\p{L}\p{M}\p{Zs}']/u;
73
+
74
+ /**
75
+ * Single test for the presence of an orphaned combining mark (non-global) — a
76
+ * `\p{M}` not attached to a base letter (string start, whitespace, the
77
+ * apostrophe, or a position vacated by a stripped character). A mark preceded by
78
+ * `\p{L}` (a base letter, e.g. the decomposed accent in "Renée") or by another
79
+ * `\p{M}` (a multi-mark cluster) is NOT matched because the negative lookbehind
80
+ * fails at its position.
81
+ */
82
+ const ORPHANED_MARK_PROBE = /(?<![\p{L}\p{M}])\p{M}/u;
83
+
84
+ /**
85
+ * Whether `raw` already satisfies the display-name policy, i.e. it contains no
86
+ * disallowed characters AND no orphaned combining marks. Used to REJECT native
87
+ * (signup / profile edit) names with a 400 rather than silently stripping them,
88
+ * and to validate inline in the client editor.
89
+ *
90
+ * The orphaned-mark probe runs on the NFC-normalized form so a legitimate
91
+ * decomposed accent (`e`+◌́) — which normalization recomposes into `é` — is NOT
92
+ * rejected, while a lone, base-less mark (e.g. `"༘"`) IS.
93
+ *
94
+ * The function only checks the character set; an empty or whitespace-only string
95
+ * is considered valid (`true`). Call sites that require a non-empty name enforce
96
+ * that separately.
97
+ */
98
+ export function isValidDisplayName(raw: string): boolean {
99
+ return (
100
+ !DISALLOWED_PROBE.test(raw) && !ORPHANED_MARK_PROBE.test(raw.normalize('NFC'))
101
+ );
102
+ }
103
+
42
104
  /**
43
105
  * Validate required string
44
106
  */