@oxyhq/services 8.2.0 → 8.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.
- package/lib/commonjs/ui/context/OxyContext.js +220 -55
- package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
- package/lib/commonjs/ui/utils/ssoBounce.js +158 -0
- package/lib/commonjs/ui/utils/ssoBounce.js.map +1 -0
- package/lib/module/ui/context/OxyContext.js +220 -56
- package/lib/module/ui/context/OxyContext.js.map +1 -1
- package/lib/module/ui/utils/ssoBounce.js +148 -0
- package/lib/module/ui/utils/ssoBounce.js.map +1 -0
- package/lib/typescript/commonjs/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/utils/ssoBounce.d.ts +89 -0
- package/lib/typescript/commonjs/ui/utils/ssoBounce.d.ts.map +1 -0
- package/lib/typescript/module/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/module/ui/utils/ssoBounce.d.ts +89 -0
- package/lib/typescript/module/ui/utils/ssoBounce.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/ui/context/OxyContext.tsx +238 -56
- package/src/ui/utils/ssoBounce.ts +146 -0
|
@@ -14,8 +14,18 @@ import type { User, ApiError, SessionLoginResponse } from '@oxyhq/core';
|
|
|
14
14
|
import type { ManagedAccount, CreateManagedAccountInput } from '@oxyhq/core';
|
|
15
15
|
import { KeyManager } from '@oxyhq/core';
|
|
16
16
|
import type { ClientSession } from '@oxyhq/core';
|
|
17
|
-
import {
|
|
17
|
+
import { runColdBoot, resolveCentralAuthUrl, parseSsoReturnFragment } from '@oxyhq/core';
|
|
18
18
|
import { toast } from '@oxyhq/bloom';
|
|
19
|
+
import {
|
|
20
|
+
SSO_CALLBACK_PATH,
|
|
21
|
+
ssoStateKey,
|
|
22
|
+
ssoGuardKey,
|
|
23
|
+
ssoDestKey,
|
|
24
|
+
ssoNoSessionKey,
|
|
25
|
+
isCentralIdPOrigin,
|
|
26
|
+
guardActive,
|
|
27
|
+
} from '../utils/ssoBounce';
|
|
28
|
+
import * as ssoBounce from '../utils/ssoBounce';
|
|
19
29
|
import { useAuthStore, type AuthState } from '../stores/authStore';
|
|
20
30
|
import { useShallow } from 'zustand/react/shallow';
|
|
21
31
|
import { useSessionSocket } from '../hooks/useSessionSocket';
|
|
@@ -115,19 +125,19 @@ export interface OxyContextProviderProps {
|
|
|
115
125
|
}
|
|
116
126
|
|
|
117
127
|
/**
|
|
118
|
-
* Module-level run-once guard for the cold-boot silent
|
|
119
|
-
* (`fedcm-silent` and `silent-iframe`).
|
|
128
|
+
* Module-level run-once guard for the cold-boot `fedcm-silent` step.
|
|
120
129
|
*
|
|
121
|
-
*
|
|
122
|
-
* fire AT MOST ONCE per page load — otherwise a provider
|
|
123
|
-
* churn, StrictMode double-invoke, error-boundary
|
|
124
|
-
* request storm. A per-instance ref resets on
|
|
125
|
-
* live at module scope. Keyed on
|
|
126
|
-
* the same API from the same
|
|
127
|
-
*
|
|
128
|
-
*
|
|
130
|
+
* The FedCM silent step triggers a one-shot `navigator.credentials.get`
|
|
131
|
+
* handshake that must fire AT MOST ONCE per page load — otherwise a provider
|
|
132
|
+
* remount storm (route churn, StrictMode double-invoke, error-boundary
|
|
133
|
+
* recovery) becomes a credential request storm. A per-instance ref resets on
|
|
134
|
+
* every remount, so the guard must live at module scope. Keyed on
|
|
135
|
+
* `origin|baseURL` so two providers pointed at the same API from the same
|
|
136
|
+
* origin share one attempt; never cleared because only a fresh page load can
|
|
137
|
+
* change the central IdP session state, and a fresh page load starts a fresh
|
|
138
|
+
* module scope.
|
|
129
139
|
*
|
|
130
|
-
* This is a
|
|
140
|
+
* This is a dedicated set — distinct from `useWebSSO`'s `silentSSOAttempted`
|
|
131
141
|
* (which guards the post-boot INTERACTIVE button path) and never a core
|
|
132
142
|
* module-level singleton (that re-evaluates under Metro web bundling and the
|
|
133
143
|
* guard would not hold).
|
|
@@ -226,19 +236,19 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
|
|
|
226
236
|
if (providedOxyServices) {
|
|
227
237
|
oxyServicesRef.current = providedOxyServices;
|
|
228
238
|
} else if (baseURL) {
|
|
229
|
-
//
|
|
230
|
-
//
|
|
231
|
-
// `auth
|
|
232
|
-
//
|
|
233
|
-
//
|
|
234
|
-
//
|
|
235
|
-
//
|
|
236
|
-
//
|
|
237
|
-
//
|
|
238
|
-
// never mutated.
|
|
239
|
+
// Target the CENTRAL IdP for TRUE cross-domain SSO. Every RP
|
|
240
|
+
// (mention.earth, homiio.com, alia.onl, …) delegates to the one central
|
|
241
|
+
// `auth.oxy.so` — it owns the host-only `fedcm_session` cookie and the
|
|
242
|
+
// central session store reached via `api.oxy.so`, so a single sign-in
|
|
243
|
+
// there is observed by all RPs through the opaque-code `/sso` bounce.
|
|
244
|
+
// `resolveCentralAuthUrl(authWebUrl)` returns the explicit `authWebUrl`
|
|
245
|
+
// prop when provided (explicit always wins) and the central default
|
|
246
|
+
// otherwise. This is NOT per-apex auto-detection — central SSO is
|
|
247
|
+
// deliberately central. A consumer-provided `OxyServices` instance is
|
|
248
|
+
// never mutated; only the baseURL-only construction path applies this.
|
|
239
249
|
oxyServicesRef.current = new OxyServices({
|
|
240
250
|
baseURL,
|
|
241
|
-
authWebUrl: authWebUrl
|
|
251
|
+
authWebUrl: resolveCentralAuthUrl(authWebUrl),
|
|
242
252
|
authRedirectUri,
|
|
243
253
|
});
|
|
244
254
|
} else {
|
|
@@ -735,14 +745,111 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
|
|
|
735
745
|
storageKeys.sessionIds,
|
|
736
746
|
]);
|
|
737
747
|
|
|
748
|
+
// Central cross-domain SSO return handler (web). Parses the IdP redirect
|
|
749
|
+
// fragment, validates the CSRF `state`, exchanges the opaque single-use code
|
|
750
|
+
// for the real session, commits it, and restores the user's pre-bounce
|
|
751
|
+
// destination. Shared by the `sso-return` cold-boot step AND the bfcache
|
|
752
|
+
// `pageshow` re-evaluation, so the same security-critical logic runs exactly
|
|
753
|
+
// once per delivered fragment regardless of how the page was (re)shown.
|
|
754
|
+
//
|
|
755
|
+
// Returns `true` when a session was committed (caller short-circuits), `false`
|
|
756
|
+
// otherwise. On ANY non-ok outcome — `none`/`error`, state mismatch, missing
|
|
757
|
+
// code, or a failed/forged exchange — it sets the per-origin NO_SESSION flag
|
|
758
|
+
// so `sso-bounce` is disabled and the page cannot loop. Off-browser it is a
|
|
759
|
+
// no-op returning `false` (native never reaches it).
|
|
760
|
+
const runSsoReturn = useCallback(async (): Promise<boolean> => {
|
|
761
|
+
if (!isWebBrowser()) {
|
|
762
|
+
return false;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
const ret = parseSsoReturnFragment(window.location.hash);
|
|
766
|
+
if (!ret) {
|
|
767
|
+
// Not an oxy_sso fragment — nothing to do (do NOT touch any flags).
|
|
768
|
+
return false;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
const origin = window.location.origin;
|
|
772
|
+
const expectedState = window.sessionStorage.getItem(ssoStateKey(origin));
|
|
773
|
+
const stateOk = !!ret.state && !!expectedState && ret.state === expectedState;
|
|
774
|
+
|
|
775
|
+
// Strip the fragment FIRST so the opaque code never lingers in the address
|
|
776
|
+
// bar, history, or a copy-paste — even if a later step throws.
|
|
777
|
+
window.history.replaceState(null, '', window.location.pathname + window.location.search);
|
|
778
|
+
window.sessionStorage.removeItem(ssoStateKey(origin));
|
|
779
|
+
|
|
780
|
+
const markNoSession = () => {
|
|
781
|
+
window.sessionStorage.setItem(ssoNoSessionKey(origin), '1');
|
|
782
|
+
};
|
|
783
|
+
|
|
784
|
+
if (ret.kind === 'none' || ret.kind === 'error') {
|
|
785
|
+
// The central IdP had no session (or the bounce failed). Record it so we
|
|
786
|
+
// do not bounce again this tab — the definitive loop breaker.
|
|
787
|
+
markNoSession();
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
if (!stateOk || !ret.code) {
|
|
792
|
+
// Forged / replayed / stale fragment, or a malformed ok with no code.
|
|
793
|
+
// Treat exactly like "no session": never exchange, never loop.
|
|
794
|
+
markNoSession();
|
|
795
|
+
return false;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
const commitWebSession = handleWebSSOSessionRef.current;
|
|
799
|
+
let session: SessionLoginResponse;
|
|
800
|
+
try {
|
|
801
|
+
session = await oxyServices.exchangeSsoCode(ret.code);
|
|
802
|
+
} catch (error) {
|
|
803
|
+
if (__DEV__) {
|
|
804
|
+
loggerUtil.debug(
|
|
805
|
+
'SSO code exchange failed (treating as no session)',
|
|
806
|
+
{ component: 'OxyContext', method: 'runSsoReturn' },
|
|
807
|
+
error,
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
markNoSession();
|
|
811
|
+
return false;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
if (!session?.sessionId || !commitWebSession) {
|
|
815
|
+
markNoSession();
|
|
816
|
+
return false;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
await commitWebSession(session);
|
|
820
|
+
|
|
821
|
+
// Restore the user's real destination captured before the bounce. We only
|
|
822
|
+
// rewrite the URL when we are sitting on the callback path — otherwise the
|
|
823
|
+
// current URL is already the destination.
|
|
824
|
+
if (window.location.pathname === SSO_CALLBACK_PATH) {
|
|
825
|
+
const dest = window.sessionStorage.getItem(ssoDestKey(origin));
|
|
826
|
+
if (dest) {
|
|
827
|
+
try {
|
|
828
|
+
const destUrl = new URL(dest);
|
|
829
|
+
// Same-origin only — never honour a cross-origin destination that
|
|
830
|
+
// could have been planted to redirect the freshly signed-in user.
|
|
831
|
+
if (destUrl.origin === origin) {
|
|
832
|
+
window.history.replaceState(null, '', destUrl.pathname + destUrl.search + destUrl.hash);
|
|
833
|
+
}
|
|
834
|
+
} catch {
|
|
835
|
+
// Malformed stored destination — leave the URL on the callback path.
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
window.sessionStorage.removeItem(ssoDestKey(origin));
|
|
840
|
+
|
|
841
|
+
return true;
|
|
842
|
+
}, [oxyServices]);
|
|
843
|
+
|
|
738
844
|
// Cold boot — the single, ordered, short-circuit session-recovery sequence,
|
|
739
845
|
// consuming the SAME `runColdBoot` core primitive as `WebOxyProvider`. The
|
|
740
846
|
// FIRST step that yields a session wins; every later step is skipped. Each
|
|
741
847
|
// web-only step is gated by `isWebBrowser()`, so on native ONLY
|
|
742
848
|
// `stored-session` runs.
|
|
743
849
|
//
|
|
744
|
-
// Order (web): redirect callback →
|
|
745
|
-
//
|
|
850
|
+
// Order (web): redirect callback → SSO return → FedCM silent → cookie restore
|
|
851
|
+
// → stored session → SSO bounce (terminal). Order (native): stored session
|
|
852
|
+
// only (every web-only step is disabled off-browser).
|
|
746
853
|
const restoreSessionsFromStorage = useCallback(async (): Promise<void> => {
|
|
747
854
|
if (!storage) {
|
|
748
855
|
return;
|
|
@@ -758,7 +865,7 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
|
|
|
758
865
|
const outcome = await runColdBoot<true>({
|
|
759
866
|
steps: [
|
|
760
867
|
{
|
|
761
|
-
//
|
|
868
|
+
// 0) Redirect callback wins: a popup/redirect sign-in just landed
|
|
762
869
|
// back on this page with `access_token`/`session_id` query params.
|
|
763
870
|
// `handleAuthCallback` plants the token but returns a PLACEHOLDER
|
|
764
871
|
// user (empty id), so we hydrate the REAL user via `getCurrentUser`
|
|
@@ -777,36 +884,30 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
|
|
|
777
884
|
},
|
|
778
885
|
},
|
|
779
886
|
{
|
|
780
|
-
//
|
|
781
|
-
// the
|
|
782
|
-
//
|
|
783
|
-
//
|
|
784
|
-
|
|
785
|
-
|
|
887
|
+
// 1) Central SSO return: we are landing back from an `auth.oxy.so/sso`
|
|
888
|
+
// bounce with the result in the URL fragment. Parse it, validate the
|
|
889
|
+
// CSRF state, exchange the opaque code, and commit. On any non-ok
|
|
890
|
+
// outcome `runSsoReturn` sets the per-origin NO_SESSION flag so the
|
|
891
|
+
// terminal `sso-bounce` step is disabled — the loop breaker.
|
|
892
|
+
id: 'sso-return',
|
|
893
|
+
enabled: () => isWebBrowser(),
|
|
786
894
|
run: async () => {
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
if (!session || !commitWebSession) {
|
|
790
|
-
return { kind: 'skip' };
|
|
791
|
-
}
|
|
792
|
-
await commitWebSession(session);
|
|
793
|
-
return { kind: 'session', session: true };
|
|
895
|
+
const committed = await runSsoReturn();
|
|
896
|
+
return committed ? { kind: 'session', session: true } : { kind: 'skip' };
|
|
794
897
|
},
|
|
795
898
|
},
|
|
796
899
|
{
|
|
797
|
-
//
|
|
798
|
-
//
|
|
799
|
-
//
|
|
800
|
-
// `
|
|
801
|
-
//
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
oxyServices.isFedCMSupported?.() !== true &&
|
|
806
|
-
!servicesSilentAttempted.has(silentKey),
|
|
900
|
+
// 2) FedCM silent reauthn (Chrome) against the CENTRAL IdP
|
|
901
|
+
// (auth.oxy.so). `silentSignInWithFedCM` plants the access token
|
|
902
|
+
// internally; we commit the returned session via
|
|
903
|
+
// `handleWebSSOSession`. Guarded so it fires at most once per page
|
|
904
|
+
// load across remounts. This is an enhancement layered above the
|
|
905
|
+
// opaque-code bounce: when it succeeds the bounce never fires.
|
|
906
|
+
id: 'fedcm-silent',
|
|
907
|
+
enabled: () => fedcmSupported && !servicesSilentAttempted.has(silentKey),
|
|
807
908
|
run: async () => {
|
|
808
909
|
servicesSilentAttempted.add(silentKey);
|
|
809
|
-
const session = await oxyServices.
|
|
910
|
+
const session = await oxyServices.silentSignInWithFedCM?.();
|
|
810
911
|
if (!session || !commitWebSession) {
|
|
811
912
|
return { kind: 'skip' };
|
|
812
913
|
}
|
|
@@ -815,12 +916,12 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
|
|
|
815
916
|
},
|
|
816
917
|
},
|
|
817
918
|
{
|
|
818
|
-
//
|
|
919
|
+
// 3) Refresh-cookie restore (first-party only). On `*.oxy.so` the
|
|
819
920
|
// httpOnly `oxy_rt_${n}` cookies ride along and resurrect every
|
|
820
921
|
// device-local slot. On a cross-domain RP (mention.earth, …) the
|
|
821
922
|
// cookie is `Domain=oxy.so` so it never reaches `api.<apex>` —
|
|
822
|
-
// `refreshAllSessions` returns `{accounts:[]}` and this skips.
|
|
823
|
-
//
|
|
923
|
+
// `refreshAllSessions` returns `{accounts:[]}` and this skips. That
|
|
924
|
+
// is correct; cross-domain restore is handled by the SSO bounce.
|
|
824
925
|
id: 'cookie-restore',
|
|
825
926
|
enabled: () => isWebBrowser(),
|
|
826
927
|
run: async () => {
|
|
@@ -829,15 +930,62 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
|
|
|
829
930
|
},
|
|
830
931
|
},
|
|
831
932
|
{
|
|
832
|
-
//
|
|
833
|
-
// platforms. This is native's ONLY restore path (every web-only
|
|
834
|
-
//
|
|
933
|
+
// 4) Stored-session bearer restore. NO `enabled` gate — runs on ALL
|
|
934
|
+
// platforms. This is native's ONLY restore path (every web-only step
|
|
935
|
+
// is disabled off-browser, so native reaches exactly this).
|
|
835
936
|
id: 'stored-session',
|
|
836
937
|
run: async () => {
|
|
837
938
|
const restored = await restoreStoredSession();
|
|
838
939
|
return restored ? { kind: 'session', session: true } : { kind: 'skip' };
|
|
839
940
|
},
|
|
840
941
|
},
|
|
942
|
+
{
|
|
943
|
+
// 5) SSO bounce (TERMINAL, web only, at most once). No local session
|
|
944
|
+
// was found by any step above. Top-level navigate to the central
|
|
945
|
+
// `auth.oxy.so/sso?prompt=none` so the IdP can either mint a session
|
|
946
|
+
// (returning an opaque code we exchange on the callback) or report
|
|
947
|
+
// `none`. This step tears the document down on success — its `skip`
|
|
948
|
+
// result is only observed if `assign` no-ops. Disabled on the IdP
|
|
949
|
+
// itself, once the NO_SESSION flag is set, or while a bounce guard is
|
|
950
|
+
// still active (loop + self-heal protection).
|
|
951
|
+
id: 'sso-bounce',
|
|
952
|
+
enabled: () => {
|
|
953
|
+
if (!isWebBrowser() || window.top !== window.self) {
|
|
954
|
+
return false;
|
|
955
|
+
}
|
|
956
|
+
const origin = window.location.origin;
|
|
957
|
+
if (isCentralIdPOrigin(origin)) {
|
|
958
|
+
return false;
|
|
959
|
+
}
|
|
960
|
+
if (window.sessionStorage.getItem(ssoNoSessionKey(origin)) === '1') {
|
|
961
|
+
return false;
|
|
962
|
+
}
|
|
963
|
+
if (guardActive(window.sessionStorage, origin, Date.now())) {
|
|
964
|
+
return false;
|
|
965
|
+
}
|
|
966
|
+
return true;
|
|
967
|
+
},
|
|
968
|
+
run: async () => {
|
|
969
|
+
const origin = window.location.origin;
|
|
970
|
+
const state = oxyServices.generateSsoState();
|
|
971
|
+
window.sessionStorage.setItem(ssoStateKey(origin), state);
|
|
972
|
+
window.sessionStorage.setItem(ssoGuardKey(origin), String(Date.now()));
|
|
973
|
+
window.sessionStorage.setItem(ssoDestKey(origin), window.location.href);
|
|
974
|
+
|
|
975
|
+
const url = new URL('/sso', resolveCentralAuthUrl(oxyServices.config?.authWebUrl));
|
|
976
|
+
url.searchParams.set('prompt', 'none');
|
|
977
|
+
url.searchParams.set('client_id', origin);
|
|
978
|
+
url.searchParams.set('return_to', origin + SSO_CALLBACK_PATH);
|
|
979
|
+
url.searchParams.set('state', state);
|
|
980
|
+
|
|
981
|
+
// TERMINAL: the document is torn down by this navigation. The
|
|
982
|
+
// `skip` below is only reached if `assign` is a no-op (e.g. the
|
|
983
|
+
// navigation is blocked); in that case we fall through
|
|
984
|
+
// unauthenticated, which is correct.
|
|
985
|
+
ssoBounce.ssoNavigate(url.toString());
|
|
986
|
+
return { kind: 'skip' };
|
|
987
|
+
},
|
|
988
|
+
},
|
|
841
989
|
],
|
|
842
990
|
onStepError: (id, error) => {
|
|
843
991
|
if (__DEV__) {
|
|
@@ -869,6 +1017,7 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
|
|
|
869
1017
|
storage,
|
|
870
1018
|
restoreViaRefreshCookie,
|
|
871
1019
|
restoreStoredSession,
|
|
1020
|
+
runSsoReturn,
|
|
872
1021
|
]);
|
|
873
1022
|
|
|
874
1023
|
useEffect(() => {
|
|
@@ -884,6 +1033,39 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
|
|
|
884
1033
|
});
|
|
885
1034
|
}, [restoreSessionsFromStorage, storage, initialized, logger]);
|
|
886
1035
|
|
|
1036
|
+
// bfcache re-evaluation (web only, registered once). When a page is restored
|
|
1037
|
+
// from the back/forward cache (`e.persisted`) NO cold boot re-runs — React
|
|
1038
|
+
// state is resurrected as-is — yet the page may have been frozen mid-bounce
|
|
1039
|
+
// and resurrected ON the SSO callback with a fresh fragment in the URL. Re-run
|
|
1040
|
+
// the `sso-return` parse so the opaque code is still exchanged (and the
|
|
1041
|
+
// fragment stripped + NO_SESSION flag maintained) on a bfcache restore. Routed
|
|
1042
|
+
// through a ref so the listener registers exactly once and never churns with
|
|
1043
|
+
// `runSsoReturn`'s identity.
|
|
1044
|
+
const runSsoReturnRef = useRef(runSsoReturn);
|
|
1045
|
+
runSsoReturnRef.current = runSsoReturn;
|
|
1046
|
+
|
|
1047
|
+
useEffect(() => {
|
|
1048
|
+
if (!isWebBrowser()) {
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
const onPageShow = (event: PageTransitionEvent) => {
|
|
1052
|
+
if (!event.persisted) {
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
runSsoReturnRef.current().catch((error) => {
|
|
1056
|
+
if (__DEV__) {
|
|
1057
|
+
loggerUtil.debug(
|
|
1058
|
+
'bfcache SSO return re-evaluation failed (non-fatal)',
|
|
1059
|
+
{ component: 'OxyContext', method: 'onPageShow' },
|
|
1060
|
+
error,
|
|
1061
|
+
);
|
|
1062
|
+
}
|
|
1063
|
+
});
|
|
1064
|
+
};
|
|
1065
|
+
window.addEventListener('pageshow', onPageShow);
|
|
1066
|
+
return () => window.removeEventListener('pageshow', onPageShow);
|
|
1067
|
+
}, []);
|
|
1068
|
+
|
|
887
1069
|
// Web SSO: Automatically check for cross-domain session on web platforms
|
|
888
1070
|
// Also used for popup auth - updates all state and persists session
|
|
889
1071
|
const handleWebSSOSession = useCallback(async (session: SessionLoginResponse) => {
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Central cross-domain SSO bounce — per-origin sessionStorage keys and small
|
|
3
|
+
* pure predicates shared by the cold-boot `sso-return` / `sso-bounce` steps and
|
|
4
|
+
* the bfcache `pageshow` re-evaluation.
|
|
5
|
+
*
|
|
6
|
+
* TRUE central SSO (Google/Meta/Clerk style) works like this for a Relying
|
|
7
|
+
* Party (mention.earth, homiio.com, alia.onl, …) with no local session:
|
|
8
|
+
*
|
|
9
|
+
* 1. `sso-bounce` (terminal, once): top-level navigate to
|
|
10
|
+
* `auth.oxy.so/sso?prompt=none&client_id=<origin>&return_to=<origin>/__oxy/sso-callback&state=<s>`.
|
|
11
|
+
* Before navigating it records, in this origin's `sessionStorage`, the CSRF
|
|
12
|
+
* `state`, a guard timestamp (loop breaker), and the real destination URL
|
|
13
|
+
* to restore after the callback.
|
|
14
|
+
* 2. The central IdP worker reads its first-party `fedcm_session`, mints a
|
|
15
|
+
* session, stores it under an opaque single-use `code`, and 303-redirects
|
|
16
|
+
* back to `<origin>/__oxy/sso-callback#oxy_sso=ok&code=<code>&state=<s>`
|
|
17
|
+
* (or `#oxy_sso=none` / `#oxy_sso=error`).
|
|
18
|
+
* 3. `sso-return` parses the fragment (`parseSsoReturnFragment` from core),
|
|
19
|
+
* validates `state`, exchanges the `code` via `oxyServices.exchangeSsoCode`,
|
|
20
|
+
* and commits the session — then restores the original destination.
|
|
21
|
+
*
|
|
22
|
+
* Loop proof (logged-out): first load all steps skip → `sso-bounce` sets
|
|
23
|
+
* guard/state/dest and navigates; the IdP (no central session) returns
|
|
24
|
+
* `#oxy_sso=none`; the callback load's `sso-return` sees `none`, sets the
|
|
25
|
+
* no-session flag, and `sso-bounce` is then disabled. Exactly ONE bounce, no
|
|
26
|
+
* loop. An interrupted bounce (user hit back mid-redirect) self-heals once the
|
|
27
|
+
* 30s guard TTL lapses.
|
|
28
|
+
*
|
|
29
|
+
* All state lives in `sessionStorage` (per tab, cleared on tab close) and is
|
|
30
|
+
* keyed per-origin so two RPs hosted in the same browser never collide. This
|
|
31
|
+
* module is pure with respect to navigation: it only reads/writes
|
|
32
|
+
* `sessionStorage` and parses URLs; it performs no redirects itself.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { CENTRAL_AUTH_URL } from '@oxyhq/core';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The RP callback path the central IdP redirects back to. The SSO result is
|
|
39
|
+
* delivered in the fragment of this URL; `sso-return` consumes it and then
|
|
40
|
+
* restores the user's real destination.
|
|
41
|
+
*/
|
|
42
|
+
export const SSO_CALLBACK_PATH = '/__oxy/sso-callback';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Self-healing TTL (ms) for the bounce guard. If a bounce is interrupted before
|
|
46
|
+
* the callback lands (e.g. the user navigates back mid-redirect), the guard
|
|
47
|
+
* would otherwise pin the RP signed-out forever. After this window the guard is
|
|
48
|
+
* treated as stale and a fresh single bounce is permitted.
|
|
49
|
+
*/
|
|
50
|
+
export const SSO_GUARD_TTL_MS = 30_000;
|
|
51
|
+
|
|
52
|
+
const STATE_KEY_PREFIX = 'oxy_sso_state:';
|
|
53
|
+
const GUARD_KEY_PREFIX = 'oxy_sso_guard:';
|
|
54
|
+
const DEST_KEY_PREFIX = 'oxy_sso_dest:';
|
|
55
|
+
const NO_SESSION_KEY_PREFIX = 'oxy_sso_no_session:';
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Perform the terminal top-level SSO bounce navigation.
|
|
59
|
+
*
|
|
60
|
+
* A thin wrapper over `window.location.assign(url)` so the single navigation
|
|
61
|
+
* seam lives in one place (and stays mockable in tests, where jsdom's
|
|
62
|
+
* `Location.assign` is a non-configurable native method). In production this is
|
|
63
|
+
* exactly `window.location.assign` — the document is torn down and replaced by
|
|
64
|
+
* the central IdP page. Off-browser it is a no-op (native never bounces).
|
|
65
|
+
*/
|
|
66
|
+
export function ssoNavigate(url: string): void {
|
|
67
|
+
if (typeof window === 'undefined' || typeof window.location === 'undefined') {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
window.location.assign(url);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Per-origin CSRF state key (matched on return to defeat fragment forgery). */
|
|
74
|
+
export function ssoStateKey(origin: string): string {
|
|
75
|
+
return `${STATE_KEY_PREFIX}${origin}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Per-origin bounce guard key (a timestamp; loop breaker + self-heal TTL). */
|
|
79
|
+
export function ssoGuardKey(origin: string): string {
|
|
80
|
+
return `${GUARD_KEY_PREFIX}${origin}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Per-origin destination key (the real URL to restore after the callback). */
|
|
84
|
+
export function ssoDestKey(origin: string): string {
|
|
85
|
+
return `${DEST_KEY_PREFIX}${origin}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Per-origin "the central IdP has no session for me" key. Set after a
|
|
90
|
+
* `none`/`error` return (or a failed/forged exchange) so `sso-bounce` does not
|
|
91
|
+
* fire again this tab — the definitive loop breaker.
|
|
92
|
+
*/
|
|
93
|
+
export function ssoNoSessionKey(origin: string): string {
|
|
94
|
+
return `${NO_SESSION_KEY_PREFIX}${origin}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Whether `origin` IS the central IdP origin. We must never bounce while on
|
|
99
|
+
* `auth.oxy.so` itself (it would bounce to itself). Compared by URL origin so a
|
|
100
|
+
* trailing-slash / path difference never defeats the guard.
|
|
101
|
+
*/
|
|
102
|
+
export function isCentralIdPOrigin(origin: string): boolean {
|
|
103
|
+
let centralOrigin: string;
|
|
104
|
+
try {
|
|
105
|
+
centralOrigin = new URL(CENTRAL_AUTH_URL).origin;
|
|
106
|
+
} catch {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
let candidateOrigin: string;
|
|
110
|
+
try {
|
|
111
|
+
candidateOrigin = new URL(origin).origin;
|
|
112
|
+
} catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return candidateOrigin === centralOrigin;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Read the bounce guard and decide whether it is still ACTIVE.
|
|
120
|
+
*
|
|
121
|
+
* Active means: a guard value is present AND it parses to a finite timestamp AND
|
|
122
|
+
* less than {@link SSO_GUARD_TTL_MS} has elapsed since it was set. An active
|
|
123
|
+
* guard disables `sso-bounce` (a bounce is already in flight this tab). A
|
|
124
|
+
* missing, malformed, or expired guard is NOT active, so a fresh bounce may
|
|
125
|
+
* proceed (this is the 30s self-heal for an interrupted bounce).
|
|
126
|
+
*
|
|
127
|
+
* @param storage - The session storage to read (injected for testability).
|
|
128
|
+
* @param origin - The page origin whose guard to evaluate.
|
|
129
|
+
* @param now - Current epoch ms (injected for deterministic tests).
|
|
130
|
+
*/
|
|
131
|
+
export function guardActive(storage: Storage, origin: string, now: number): boolean {
|
|
132
|
+
let raw: string | null;
|
|
133
|
+
try {
|
|
134
|
+
raw = storage.getItem(ssoGuardKey(origin));
|
|
135
|
+
} catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
if (raw === null || raw.length === 0) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
const stamp = Number(raw);
|
|
142
|
+
if (!Number.isFinite(stamp)) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
return now - stamp < SSO_GUARD_TTL_MS;
|
|
146
|
+
}
|