@shellui/core 0.5.0-beta.3 → 0.5.1
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/README.md +5 -5
- package/package.json +3 -2
- package/schemas/shellui.config.schema.json +98 -33
- package/src/app.tsx +10 -4
- package/src/components/ContentView.tsx +3 -5
- package/src/components/contentIframeSandbox.spec.ts +25 -0
- package/src/components/contentIframeSandbox.ts +43 -0
- package/src/components/ui/dialog.tsx +5 -1
- package/src/components/ui/dropdown-menu.tsx +39 -4
- package/src/components/ui/sheet.tsx +1 -1
- package/src/components/ui/sidebar.tsx +3 -2
- package/src/components/ui/tabs.tsx +52 -0
- package/src/constants/urls.ts +1 -0
- package/src/features/auth/AuthProvider.tsx +169 -35
- package/src/features/auth/backends/index.ts +2 -0
- package/src/features/auth/backends/shellui.ts +43 -35
- package/src/features/auth/backends/supabase.ts +1 -0
- package/src/features/auth/backends/types.ts +5 -0
- package/src/features/auth/components/LoginView.tsx +5 -1
- package/src/features/auth/components/OAuthCallbackView.tsx +115 -44
- package/src/features/auth/constants/oauth.ts +2 -0
- package/src/features/auth/hooks/useAuth.tsx +4 -0
- package/src/features/auth/utils/authSessionPersistence.spec.ts +93 -0
- package/src/features/auth/utils/authSessionPersistence.ts +138 -0
- package/src/features/auth/utils/authStorageKeys.ts +8 -0
- package/src/features/auth/utils/bffAuthClient.ts +107 -0
- package/src/features/auth/utils/buildOAuthSessionRedirectTo.spec.ts +22 -0
- package/src/features/auth/utils/buildOAuthSessionRedirectTo.ts +17 -0
- package/src/features/auth/utils/buildSessionFromTokenPayload.spec.ts +29 -0
- package/src/features/auth/utils/buildSessionFromTokenPayload.ts +24 -0
- package/src/features/auth/utils/index.ts +18 -3
- package/src/features/auth/utils/isBffAuthEnabled.ts +12 -0
- package/src/features/chromeActions/ChromeActionIcons.tsx +261 -0
- package/src/features/chromeActions/ChromeActionsDemoView.tsx +149 -0
- package/src/features/chromeActions/ChromeActionsHost.tsx +1511 -0
- package/src/features/chromeActions/ChromeActionsProvider.tsx +141 -0
- package/src/features/chromeActions/chromeActionsStore.spec.ts +61 -0
- package/src/features/chromeActions/chromeActionsStore.ts +77 -0
- package/src/features/chromeActions/computeActionInsets.spec.ts +360 -0
- package/src/features/chromeActions/computeActionInsets.ts +326 -0
- package/src/features/chromeActions/index.ts +26 -0
- package/src/features/chromeActions/resolveUuidForRemoveIframe.spec.ts +49 -0
- package/src/features/config/types.ts +49 -2
- package/src/features/layouts/AppLayout.tsx +4 -4
- package/src/features/layouts/OverlayShell.tsx +60 -12
- package/src/features/layouts/app-bar-inset/AppBarInsetLayout.tsx +1 -1
- package/src/features/layouts/appbar/AppBarLayout.tsx +194 -89
- package/src/features/layouts/chrome/DesktopChrome.tsx +17 -3
- package/src/features/layouts/chrome/InsetMobileRadiusOverlay.tsx +18 -0
- package/src/features/layouts/chrome/index.ts +8 -0
- package/src/features/layouts/chrome/runtime.ts +73 -0
- package/src/features/layouts/chrome/useScrollHideChrome.ts +210 -0
- package/src/features/layouts/floating/FloatingBottomDock.tsx +410 -0
- package/src/features/layouts/floating/FloatingLayout.tsx +31 -21
- package/src/features/layouts/floating/FloatingStatusScrim.tsx +36 -0
- package/src/features/layouts/floating/computeFloatingInsets.spec.ts +5 -5
- package/src/features/layouts/floating/computeFloatingInsets.ts +64 -12
- package/src/features/layouts/floating/isShellOwnedPath.spec.ts +2 -0
- package/src/features/layouts/floating/isShellOwnedPath.ts +1 -0
- package/src/features/layouts/floating/layoutChromeStore.spec.ts +138 -0
- package/src/features/layouts/floating/layoutChromeStore.ts +169 -6
- package/src/features/layouts/floating/useFloatingChrome.ts +20 -30
- package/src/features/layouts/sidebar/SidebarLayout.tsx +87 -31
- package/src/features/layouts/sidebar-inset/SidebarInsetLayout.tsx +1 -1
- package/src/features/layouts/windows/WindowsLayout.tsx +240 -110
- package/src/features/messaging/MessagingSecurityBootstrap.tsx +19 -0
- package/src/features/messaging/deriveAllowedMessageOrigins.spec.ts +86 -0
- package/src/features/messaging/deriveAllowedMessageOrigins.ts +74 -0
- package/src/features/modal/ModalContext.spec.ts +13 -2
- package/src/features/modal/validateAndNormalizeUrl.ts +9 -23
- package/src/features/overlays/ResponsiveModal.tsx +201 -21
- package/src/features/overlays/overlaySize.spec.ts +18 -0
- package/src/features/overlays/overlaySize.ts +89 -0
- package/src/features/security/trustedFrames.spec.ts +129 -0
- package/src/features/security/trustedFrames.ts +79 -0
- package/src/features/security/urlAllowlist.spec.ts +99 -0
- package/src/features/security/urlAllowlist.ts +122 -0
- package/src/features/settings/SettingsProvider.tsx +38 -54
- package/src/features/settings/components/Develop.tsx +2 -0
- package/src/features/settings/components/develop/ChromeActionsTestButtons.tsx +63 -0
- package/src/features/settings/utils/buildSettingsForPropagation.spec.ts +23 -1
- package/src/features/settings/utils/buildSettingsForPropagation.ts +7 -0
- package/src/features/storage/StorageBridge.spec.ts +82 -0
- package/src/features/storage/StorageBridge.tsx +25 -3
- package/src/features/storage/StoragePickerContext.tsx +3 -4
- package/src/features/storage/uploads/uploadQueue.spec.ts +56 -1
- package/src/features/storage/uploads/uploadQueue.ts +44 -6
- package/src/features/theme/applyTheme.ts +23 -5
- package/src/hooks/use-mobile.tsx +21 -7
- package/src/i18n/translations/en/settings.json +9 -0
- package/src/i18n/translations/fr/settings.json +9 -0
- package/src/index.css +264 -124
- package/src/index.html +6 -4
- package/src/index.ts +8 -0
- package/src/routes/components/NavigationItemRoute.tsx +30 -15
- package/src/routes/components/RouteFallback.tsx +15 -0
- package/src/routes/routes.tsx +14 -0
- package/src/routes/utils/navigationRouteAccess.spec.ts +85 -0
- package/src/routes/utils/navigationRouteAccess.ts +43 -0
- package/src/features/auth/utils/clearStoredAuthSession.spec.ts +0 -23
- package/src/features/auth/utils/clearStoredAuthSession.ts +0 -10
- package/src/features/auth/utils/persistAuthSession.spec.ts +0 -35
- package/src/features/auth/utils/persistAuthSession.ts +0 -12
- package/src/features/auth/utils/readStoredAuthSession.spec.ts +0 -34
- package/src/features/auth/utils/readStoredAuthSession.ts +0 -14
- package/src/features/layouts/floating/FloatingFadeMask.tsx +0 -22
- package/src/features/layouts/floating/FloatingTabBar.tsx +0 -437
- package/src/features/layouts/floating/FloatingTabIndicator.tsx +0 -216
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import { getLogger, shellui, type Settings } from '@shellui/sdk';
|
|
2
|
+
import { getLogger, postShellMessage, shellui, type Settings } from '@shellui/sdk';
|
|
3
3
|
import urls from '../../constants/urls';
|
|
4
4
|
import { useConfig } from '../config/useConfig';
|
|
5
5
|
import { createAuthBackend } from './backends';
|
|
@@ -8,14 +8,18 @@ import type { AuthEvent, AuthSession, AuthUser, UserPreferences } from './types'
|
|
|
8
8
|
import {
|
|
9
9
|
AuthRequestError,
|
|
10
10
|
clearStoredAuthSession,
|
|
11
|
+
establishBffAuthSession,
|
|
11
12
|
getAccessTokenFromSdkSettings,
|
|
12
13
|
getAuthRequestErrorCode,
|
|
13
14
|
getUserFromSdkSettings,
|
|
14
15
|
inferAccessPendingErrorCode,
|
|
16
|
+
isBffAuthEnabledFromConfig,
|
|
15
17
|
isSessionExpired,
|
|
16
18
|
isTokenAutoRefreshDisabled,
|
|
19
|
+
logoutBffAuthSession,
|
|
17
20
|
persistAuthSession,
|
|
18
21
|
readStoredAuthSession,
|
|
22
|
+
refreshBffAuthSession,
|
|
19
23
|
toAuthSessionFromSettingsUser,
|
|
20
24
|
} from './utils';
|
|
21
25
|
|
|
@@ -36,11 +40,37 @@ type LoginMessagePayload = {
|
|
|
36
40
|
|
|
37
41
|
export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
38
42
|
const { config } = useConfig();
|
|
43
|
+
const bffAuthEnabled = isBffAuthEnabledFromConfig(config.security);
|
|
39
44
|
const backend = useMemo(
|
|
40
45
|
() => createAuthBackend(config.backend),
|
|
41
46
|
[config.backend?.publishableKey, config.backend?.type, config.backend?.url],
|
|
42
47
|
);
|
|
43
48
|
|
|
49
|
+
const commitAuthSession = useCallback(
|
|
50
|
+
async (nextSession: AuthSession, nowSeconds: number): Promise<AuthSession> => {
|
|
51
|
+
if (bffAuthEnabled) {
|
|
52
|
+
const bffSession = await establishBffAuthSession(nextSession, nowSeconds);
|
|
53
|
+
if (bffSession) {
|
|
54
|
+
persistAuthSession(bffSession, { storeRefreshToken: false });
|
|
55
|
+
return bffSession;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
persistAuthSession(nextSession);
|
|
59
|
+
return nextSession;
|
|
60
|
+
},
|
|
61
|
+
[bffAuthEnabled],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const refreshAuthSession = useCallback(
|
|
65
|
+
async (current: AuthSession, nowSeconds: number): Promise<AuthSession | null> => {
|
|
66
|
+
if (bffAuthEnabled) {
|
|
67
|
+
return refreshBffAuthSession(current, nowSeconds);
|
|
68
|
+
}
|
|
69
|
+
return backend.refreshAuthSession(current, nowSeconds);
|
|
70
|
+
},
|
|
71
|
+
[backend, bffAuthEnabled],
|
|
72
|
+
);
|
|
73
|
+
|
|
44
74
|
const [session, setSession] = useState<AuthSession | null>(null);
|
|
45
75
|
const [isLoading, setIsLoading] = useState(true);
|
|
46
76
|
const [error, setError] = useState<string | null>(null);
|
|
@@ -49,6 +79,8 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
49
79
|
const sessionRef = useRef<AuthSession | null>(null);
|
|
50
80
|
sessionRef.current = session;
|
|
51
81
|
const refreshInFlightRef = useRef(false);
|
|
82
|
+
/** Bumps on every preferences sync so a slower older PUT can’t overwrite a newer theme. */
|
|
83
|
+
const preferencesSyncGenRef = useRef(0);
|
|
52
84
|
|
|
53
85
|
useEffect(() => {
|
|
54
86
|
let cancelled = false;
|
|
@@ -68,8 +100,13 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
68
100
|
const sessionFromHash = backend.readSessionFromCallback(window.location.hash, now);
|
|
69
101
|
if (sessionFromHash) {
|
|
70
102
|
if (!cancelled) {
|
|
71
|
-
|
|
72
|
-
|
|
103
|
+
try {
|
|
104
|
+
const committed = await commitAuthSession(sessionFromHash, now);
|
|
105
|
+
setSession(committed);
|
|
106
|
+
} catch {
|
|
107
|
+
persistAuthSession(sessionFromHash);
|
|
108
|
+
setSession(sessionFromHash);
|
|
109
|
+
}
|
|
73
110
|
setAuthEvent('oauth_callback');
|
|
74
111
|
if (window.location.hash) {
|
|
75
112
|
window.history.replaceState(
|
|
@@ -102,6 +139,40 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
102
139
|
return;
|
|
103
140
|
}
|
|
104
141
|
|
|
142
|
+
if (!stored.accessToken && (stored.refreshToken || bffAuthEnabled)) {
|
|
143
|
+
if (isTokenAutoRefreshDisabled()) {
|
|
144
|
+
if (!cancelled) {
|
|
145
|
+
setSession(stored);
|
|
146
|
+
setIsLoading(false);
|
|
147
|
+
}
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
const restored = await refreshAuthSession(stored, now);
|
|
152
|
+
if (!restored) {
|
|
153
|
+
clearStoredAuthSession();
|
|
154
|
+
if (!cancelled) {
|
|
155
|
+
setSession(null);
|
|
156
|
+
setIsLoading(false);
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
persistAuthSession(restored, { storeRefreshToken: !bffAuthEnabled });
|
|
161
|
+
if (!cancelled) {
|
|
162
|
+
setSession(restored);
|
|
163
|
+
setIsLoading(false);
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
} catch {
|
|
167
|
+
clearStoredAuthSession();
|
|
168
|
+
if (!cancelled) {
|
|
169
|
+
setSession(null);
|
|
170
|
+
setIsLoading(false);
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
105
176
|
if (!isSessionExpired(stored)) {
|
|
106
177
|
if (!cancelled) {
|
|
107
178
|
setSession(stored);
|
|
@@ -123,7 +194,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
123
194
|
}
|
|
124
195
|
|
|
125
196
|
try {
|
|
126
|
-
const restored = await
|
|
197
|
+
const restored = await refreshAuthSession(stored, now);
|
|
127
198
|
if (!restored) {
|
|
128
199
|
clearStoredAuthSession();
|
|
129
200
|
if (!cancelled) {
|
|
@@ -134,7 +205,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
134
205
|
}
|
|
135
206
|
|
|
136
207
|
if (!cancelled) {
|
|
137
|
-
persistAuthSession(restored);
|
|
208
|
+
persistAuthSession(restored, { storeRefreshToken: !bffAuthEnabled });
|
|
138
209
|
setSession(restored);
|
|
139
210
|
setIsLoading(false);
|
|
140
211
|
}
|
|
@@ -151,13 +222,13 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
151
222
|
return () => {
|
|
152
223
|
cancelled = true;
|
|
153
224
|
};
|
|
154
|
-
}, [backend]);
|
|
225
|
+
}, [backend, bffAuthEnabled, commitAuthSession, refreshAuthSession]);
|
|
155
226
|
|
|
156
227
|
useEffect(() => {
|
|
157
228
|
if (typeof window === 'undefined' || window.parent !== window || isLoading) {
|
|
158
229
|
return;
|
|
159
230
|
}
|
|
160
|
-
if (!session
|
|
231
|
+
if (!session || (!bffAuthEnabled && !session.refreshToken)) {
|
|
161
232
|
return;
|
|
162
233
|
}
|
|
163
234
|
|
|
@@ -167,7 +238,8 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
167
238
|
const runRefresh = async () => {
|
|
168
239
|
if (cancelled) return;
|
|
169
240
|
const current = sessionRef.current;
|
|
170
|
-
if (!current
|
|
241
|
+
if (!current || refreshInFlightRef.current) return;
|
|
242
|
+
if (!bffAuthEnabled && !current.refreshToken) return;
|
|
171
243
|
|
|
172
244
|
const now = Math.floor(Date.now() / 1000);
|
|
173
245
|
const secondsLeft = current.expiresAt - now;
|
|
@@ -189,9 +261,9 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
189
261
|
|
|
190
262
|
refreshInFlightRef.current = true;
|
|
191
263
|
try {
|
|
192
|
-
const next = await
|
|
264
|
+
const next = await refreshAuthSession(current, now);
|
|
193
265
|
if (cancelled || !next) return;
|
|
194
|
-
persistAuthSession(next);
|
|
266
|
+
persistAuthSession(next, { storeRefreshToken: !bffAuthEnabled });
|
|
195
267
|
sessionRef.current = next;
|
|
196
268
|
setSession(next);
|
|
197
269
|
} catch (err) {
|
|
@@ -228,7 +300,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
228
300
|
document.removeEventListener('visibilitychange', maybeRefreshOnResume);
|
|
229
301
|
window.removeEventListener('focus', maybeRefreshOnResume);
|
|
230
302
|
};
|
|
231
|
-
}, [
|
|
303
|
+
}, [bffAuthEnabled, isLoading, refreshAuthSession, session, session?.expiresAt]);
|
|
232
304
|
|
|
233
305
|
useEffect(() => {
|
|
234
306
|
if (typeof window === 'undefined' || window.parent === window) {
|
|
@@ -273,6 +345,64 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
273
345
|
[backend],
|
|
274
346
|
);
|
|
275
347
|
|
|
348
|
+
const persistOAuthSession = useCallback(
|
|
349
|
+
async (nextSession: AuthSession) => {
|
|
350
|
+
const now = Math.floor(Date.now() / 1000);
|
|
351
|
+
try {
|
|
352
|
+
const committed = await commitAuthSession(nextSession, now);
|
|
353
|
+
setSession(committed);
|
|
354
|
+
} catch {
|
|
355
|
+
persistAuthSession(nextSession, { storeRefreshToken: !bffAuthEnabled });
|
|
356
|
+
setSession(nextSession);
|
|
357
|
+
}
|
|
358
|
+
setAuthEvent('oauth_callback');
|
|
359
|
+
},
|
|
360
|
+
[bffAuthEnabled, commitAuthSession],
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
const mapOAuthCallbackError = useCallback((err: unknown): OAuthCallbackResult => {
|
|
364
|
+
const message = err instanceof Error ? err.message : 'Unable to complete OAuth login.';
|
|
365
|
+
const oauthErrorCode =
|
|
366
|
+
getAuthRequestErrorCode(err) ??
|
|
367
|
+
inferAccessPendingErrorCode(message) ??
|
|
368
|
+
(err instanceof AuthRequestError ? err.code : null);
|
|
369
|
+
setError(message);
|
|
370
|
+
setErrorCode(oauthErrorCode);
|
|
371
|
+
return { ok: false, error: message, errorCode: oauthErrorCode };
|
|
372
|
+
}, []);
|
|
373
|
+
|
|
374
|
+
const completeOAuthSessionCallback = useCallback(
|
|
375
|
+
async ({
|
|
376
|
+
authCode,
|
|
377
|
+
redirectTo,
|
|
378
|
+
}: {
|
|
379
|
+
authCode: string;
|
|
380
|
+
redirectTo: string;
|
|
381
|
+
}): Promise<OAuthCallbackResult> => {
|
|
382
|
+
try {
|
|
383
|
+
setError(null);
|
|
384
|
+
setErrorCode(null);
|
|
385
|
+
const now = Math.floor(Date.now() / 1000);
|
|
386
|
+
const nextSession = await backend.exchangeOAuthSessionCode({
|
|
387
|
+
authCode,
|
|
388
|
+
redirectTo,
|
|
389
|
+
nowSeconds: now,
|
|
390
|
+
});
|
|
391
|
+
if (!nextSession) {
|
|
392
|
+
const message = 'Unable to complete OAuth login.';
|
|
393
|
+
setError(message);
|
|
394
|
+
setErrorCode(null);
|
|
395
|
+
return { ok: false, error: message, errorCode: null };
|
|
396
|
+
}
|
|
397
|
+
await persistOAuthSession(nextSession);
|
|
398
|
+
return { ok: true };
|
|
399
|
+
} catch (err) {
|
|
400
|
+
return mapOAuthCallbackError(err);
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
[backend, mapOAuthCallbackError, persistOAuthSession],
|
|
404
|
+
);
|
|
405
|
+
|
|
276
406
|
const completeOAuthCallback = useCallback(
|
|
277
407
|
async ({
|
|
278
408
|
provider,
|
|
@@ -302,22 +432,13 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
302
432
|
setErrorCode(null);
|
|
303
433
|
return { ok: false, error: message, errorCode: null };
|
|
304
434
|
}
|
|
305
|
-
|
|
306
|
-
setSession(nextSession);
|
|
307
|
-
setAuthEvent('oauth_callback');
|
|
435
|
+
await persistOAuthSession(nextSession);
|
|
308
436
|
return { ok: true };
|
|
309
437
|
} catch (err) {
|
|
310
|
-
|
|
311
|
-
const oauthErrorCode =
|
|
312
|
-
getAuthRequestErrorCode(err) ??
|
|
313
|
-
inferAccessPendingErrorCode(message) ??
|
|
314
|
-
(err instanceof AuthRequestError ? err.code : null);
|
|
315
|
-
setError(message);
|
|
316
|
-
setErrorCode(oauthErrorCode);
|
|
317
|
-
return { ok: false, error: message, errorCode: oauthErrorCode };
|
|
438
|
+
return mapOAuthCallbackError(err);
|
|
318
439
|
}
|
|
319
440
|
},
|
|
320
|
-
[backend],
|
|
441
|
+
[backend, mapOAuthCallbackError, persistOAuthSession],
|
|
321
442
|
);
|
|
322
443
|
|
|
323
444
|
const startWeb3Ethereum = useCallback(async () => {
|
|
@@ -330,15 +451,16 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
330
451
|
setErrorCode(null);
|
|
331
452
|
return false;
|
|
332
453
|
}
|
|
333
|
-
|
|
334
|
-
|
|
454
|
+
const now = Math.floor(Date.now() / 1000);
|
|
455
|
+
const committed = await commitAuthSession(nextSession, now);
|
|
456
|
+
setSession(committed);
|
|
335
457
|
return true;
|
|
336
458
|
} catch (err) {
|
|
337
459
|
setError(err instanceof Error ? err.message : 'Unable to start Ethereum wallet login.');
|
|
338
460
|
setErrorCode(err instanceof AuthRequestError ? err.code : null);
|
|
339
461
|
return false;
|
|
340
462
|
}
|
|
341
|
-
}, [backend]);
|
|
463
|
+
}, [backend, commitAuthSession]);
|
|
342
464
|
|
|
343
465
|
const getAuthSettings = useCallback(() => backend.getAuthSettings(), [backend]);
|
|
344
466
|
|
|
@@ -356,30 +478,37 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
356
478
|
|
|
357
479
|
const syncUserPreferences = useCallback(
|
|
358
480
|
async (preferences: UserPreferences) => {
|
|
481
|
+
const gen = ++preferencesSyncGenRef.current;
|
|
482
|
+
const sessionAtStart = sessionRef.current;
|
|
359
483
|
try {
|
|
360
|
-
await backend.syncUserPreferences(
|
|
484
|
+
await backend.syncUserPreferences(sessionAtStart, preferences);
|
|
485
|
+
// A newer appearance change started another sync — drop this result.
|
|
486
|
+
if (gen !== preferencesSyncGenRef.current) return;
|
|
487
|
+
|
|
361
488
|
const now = Math.floor(Date.now() / 1000);
|
|
362
489
|
let nextSession: AuthSession | null = null;
|
|
363
490
|
try {
|
|
364
|
-
if (
|
|
491
|
+
if (sessionAtStart && isTokenAutoRefreshDisabled()) {
|
|
365
492
|
logger.info(
|
|
366
493
|
'Skipped token refresh after preferences sync because Develop → Disable token auto-refresh is on',
|
|
367
494
|
);
|
|
368
495
|
nextSession = null;
|
|
369
496
|
} else {
|
|
370
|
-
nextSession =
|
|
497
|
+
nextSession = sessionAtStart ? await refreshAuthSession(sessionAtStart, now) : null;
|
|
371
498
|
}
|
|
372
499
|
} catch (refreshErr) {
|
|
373
500
|
logger.error('Failed to refresh auth session after syncing preferences', { refreshErr });
|
|
374
501
|
}
|
|
375
|
-
if (
|
|
376
|
-
|
|
502
|
+
if (gen !== preferencesSyncGenRef.current) return;
|
|
503
|
+
|
|
504
|
+
if (!nextSession && sessionAtStart) {
|
|
505
|
+
nextSession = { ...sessionAtStart, userPreferences: preferences };
|
|
377
506
|
}
|
|
378
507
|
if (nextSession) {
|
|
379
508
|
// Keep stored snapshot aligned with what we synced (JWT claims can lag the PUT).
|
|
380
509
|
nextSession = { ...nextSession, userPreferences: preferences };
|
|
381
510
|
if (typeof window !== 'undefined' && window.parent === window) {
|
|
382
|
-
persistAuthSession(nextSession);
|
|
511
|
+
persistAuthSession(nextSession, { storeRefreshToken: !bffAuthEnabled });
|
|
383
512
|
}
|
|
384
513
|
setSession(nextSession);
|
|
385
514
|
}
|
|
@@ -387,7 +516,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
387
516
|
logger.error('Failed to sync user preferences to auth provider metadata', { err });
|
|
388
517
|
}
|
|
389
518
|
},
|
|
390
|
-
[backend,
|
|
519
|
+
[backend, bffAuthEnabled, refreshAuthSession],
|
|
391
520
|
);
|
|
392
521
|
|
|
393
522
|
const loadUserPreferences = useCallback(async () => {
|
|
@@ -401,6 +530,9 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
401
530
|
|
|
402
531
|
const logout = useCallback(async () => {
|
|
403
532
|
try {
|
|
533
|
+
if (bffAuthEnabled) {
|
|
534
|
+
await logoutBffAuthSession(session?.accessToken);
|
|
535
|
+
}
|
|
404
536
|
await backend.logout(session);
|
|
405
537
|
} catch {
|
|
406
538
|
// Even if API sign-out fails, still clear local session.
|
|
@@ -411,7 +543,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
411
543
|
setAuthEvent(null);
|
|
412
544
|
setError(null);
|
|
413
545
|
setErrorCode(null);
|
|
414
|
-
}, [backend, session]);
|
|
546
|
+
}, [backend, bffAuthEnabled, session]);
|
|
415
547
|
|
|
416
548
|
useEffect(() => {
|
|
417
549
|
if (typeof window === 'undefined' || window.parent !== window) {
|
|
@@ -439,7 +571,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
439
571
|
void (async () => {
|
|
440
572
|
const started = await startWeb3Ethereum();
|
|
441
573
|
if (started && typeof window !== 'undefined') {
|
|
442
|
-
|
|
574
|
+
postShellMessage({ type: 'SHELLUI_CLOSE_MODAL', payload: {} });
|
|
443
575
|
}
|
|
444
576
|
})();
|
|
445
577
|
return;
|
|
@@ -487,6 +619,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
487
619
|
errorCode,
|
|
488
620
|
authEvent,
|
|
489
621
|
clearAuthEvent,
|
|
622
|
+
completeOAuthSessionCallback,
|
|
490
623
|
completeOAuthCallback,
|
|
491
624
|
startOAuth,
|
|
492
625
|
startWeb3Ethereum,
|
|
@@ -504,6 +637,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
|
504
637
|
errorCode,
|
|
505
638
|
authEvent,
|
|
506
639
|
clearAuthEvent,
|
|
640
|
+
completeOAuthSessionCallback,
|
|
507
641
|
completeOAuthCallback,
|
|
508
642
|
startOAuth,
|
|
509
643
|
startWeb3Ethereum,
|
|
@@ -6,6 +6,8 @@ import type { AuthBackend } from './types';
|
|
|
6
6
|
const createNoopBackend = (): AuthBackend => ({
|
|
7
7
|
type: 'none',
|
|
8
8
|
readSessionFromCallback: () => null,
|
|
9
|
+
exchangeOAuthSessionCode: async () => null,
|
|
10
|
+
exchangeOAuthCode: async () => null,
|
|
9
11
|
restoreSession: async () => null,
|
|
10
12
|
refreshAuthSession: async () => null,
|
|
11
13
|
startOAuth: () => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
buildSessionFromTokenPayload,
|
|
2
3
|
buildSessionFromParams,
|
|
3
4
|
getShellUILoginClientTimezone,
|
|
4
5
|
getShellUILoginDeviceId,
|
|
@@ -13,6 +14,20 @@ import type { AuthBackend } from './types';
|
|
|
13
14
|
|
|
14
15
|
const USER_PREFERENCES_ENDPOINT = '/api/v1/preferences';
|
|
15
16
|
const OAUTH_EXCHANGE_ENDPOINT = '/api/v1/oauth/exchange';
|
|
17
|
+
const OAUTH_SESSION_ENDPOINT = '/api/v1/oauth/session';
|
|
18
|
+
|
|
19
|
+
const parseAuthErrorPayload = (
|
|
20
|
+
payload: Record<string, unknown> | null,
|
|
21
|
+
response: Response,
|
|
22
|
+
): never => {
|
|
23
|
+
const err = payload?.error ?? payload?.detail;
|
|
24
|
+
const message = typeof err === 'string' && err.trim() ? err : `HTTP ${response.status}`;
|
|
25
|
+
let errorCode = typeof payload?.error_code === 'string' ? payload.error_code.trim() : null;
|
|
26
|
+
if (!errorCode && response.status === 403) {
|
|
27
|
+
errorCode = 'access_pending';
|
|
28
|
+
}
|
|
29
|
+
throw new AuthRequestError(message, errorCode);
|
|
30
|
+
};
|
|
16
31
|
|
|
17
32
|
export const createShellUIAuthBackend = ({
|
|
18
33
|
backendUrl,
|
|
@@ -46,19 +61,7 @@ export const createShellUIAuthBackend = ({
|
|
|
46
61
|
}
|
|
47
62
|
|
|
48
63
|
const payload = (await response.json()) as Record<string, unknown>;
|
|
49
|
-
|
|
50
|
-
if (typeof payload.access_token === 'string')
|
|
51
|
-
refreshParams.set('access_token', payload.access_token);
|
|
52
|
-
if (typeof payload.refresh_token === 'string')
|
|
53
|
-
refreshParams.set('refresh_token', payload.refresh_token);
|
|
54
|
-
if (typeof payload.expires_at === 'number' || typeof payload.expires_at === 'string') {
|
|
55
|
-
refreshParams.set('expires_at', String(payload.expires_at));
|
|
56
|
-
}
|
|
57
|
-
if (typeof payload.expires_in === 'number' || typeof payload.expires_in === 'string') {
|
|
58
|
-
refreshParams.set('expires_in', String(payload.expires_in));
|
|
59
|
-
}
|
|
60
|
-
if (typeof payload.token_type === 'string') refreshParams.set('token_type', payload.token_type);
|
|
61
|
-
return buildSessionFromParams(refreshParams, nowSeconds);
|
|
64
|
+
return buildSessionFromTokenPayload(payload, nowSeconds);
|
|
62
65
|
};
|
|
63
66
|
|
|
64
67
|
return {
|
|
@@ -67,6 +70,27 @@ export const createShellUIAuthBackend = ({
|
|
|
67
70
|
const hashParams = new URLSearchParams(locationHash.replace(/^#/, ''));
|
|
68
71
|
return buildSessionFromParams(hashParams, nowSeconds);
|
|
69
72
|
},
|
|
73
|
+
exchangeOAuthSessionCode: async ({ authCode, redirectTo, nowSeconds }) => {
|
|
74
|
+
if (!backendUrl) {
|
|
75
|
+
throw new Error('Missing Shellui backend URL.');
|
|
76
|
+
}
|
|
77
|
+
const response = await fetch(`${backendUrl}${OAUTH_SESSION_ENDPOINT}`, {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
Accept: 'application/json',
|
|
81
|
+
'Content-Type': 'application/json',
|
|
82
|
+
},
|
|
83
|
+
body: JSON.stringify({
|
|
84
|
+
auth_code: authCode,
|
|
85
|
+
redirect_to: redirectTo,
|
|
86
|
+
}),
|
|
87
|
+
});
|
|
88
|
+
const payload = (await response.json().catch(() => null)) as Record<string, unknown> | null;
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
parseAuthErrorPayload(payload, response);
|
|
91
|
+
}
|
|
92
|
+
return buildSessionFromTokenPayload(payload, nowSeconds);
|
|
93
|
+
},
|
|
70
94
|
exchangeOAuthCode: async ({ provider, code, redirectUri, oauthClientId, nowSeconds }) => {
|
|
71
95
|
if (!backendUrl) {
|
|
72
96
|
throw new Error('Missing Shellui backend URL.');
|
|
@@ -100,29 +124,9 @@ export const createShellUIAuthBackend = ({
|
|
|
100
124
|
});
|
|
101
125
|
const payload = (await response.json().catch(() => null)) as Record<string, unknown> | null;
|
|
102
126
|
if (!response.ok) {
|
|
103
|
-
|
|
104
|
-
const message = typeof err === 'string' && err.trim() ? err : `HTTP ${response.status}`;
|
|
105
|
-
let errorCode = typeof payload?.error_code === 'string' ? payload.error_code.trim() : null;
|
|
106
|
-
// Exchange uses 403 only for company join denial; default the code if omitted.
|
|
107
|
-
if (!errorCode && response.status === 403) {
|
|
108
|
-
errorCode = 'access_pending';
|
|
109
|
-
}
|
|
110
|
-
throw new AuthRequestError(message, errorCode);
|
|
111
|
-
}
|
|
112
|
-
const refreshParams = new URLSearchParams();
|
|
113
|
-
if (typeof payload?.access_token === 'string')
|
|
114
|
-
refreshParams.set('access_token', payload.access_token);
|
|
115
|
-
if (typeof payload?.refresh_token === 'string')
|
|
116
|
-
refreshParams.set('refresh_token', payload.refresh_token);
|
|
117
|
-
if (typeof payload?.expires_at === 'number' || typeof payload?.expires_at === 'string') {
|
|
118
|
-
refreshParams.set('expires_at', String(payload.expires_at));
|
|
119
|
-
}
|
|
120
|
-
if (typeof payload?.expires_in === 'number' || typeof payload?.expires_in === 'string') {
|
|
121
|
-
refreshParams.set('expires_in', String(payload.expires_in));
|
|
127
|
+
parseAuthErrorPayload(payload, response);
|
|
122
128
|
}
|
|
123
|
-
|
|
124
|
-
refreshParams.set('token_type', payload.token_type);
|
|
125
|
-
return buildSessionFromParams(refreshParams, nowSeconds);
|
|
129
|
+
return buildSessionFromTokenPayload(payload, nowSeconds);
|
|
126
130
|
},
|
|
127
131
|
restoreSession: async (storedSession, nowSeconds) => {
|
|
128
132
|
if (!storedSession) return null;
|
|
@@ -188,8 +192,12 @@ export const createShellUIAuthBackend = ({
|
|
|
188
192
|
method: 'POST',
|
|
189
193
|
headers: {
|
|
190
194
|
Accept: 'application/json',
|
|
195
|
+
'Content-Type': 'application/json',
|
|
191
196
|
Authorization: `Bearer ${session.accessToken}`,
|
|
192
197
|
},
|
|
198
|
+
body: JSON.stringify({
|
|
199
|
+
...(session.refreshToken ? { refresh_token: session.refreshToken } : {}),
|
|
200
|
+
}),
|
|
193
201
|
});
|
|
194
202
|
},
|
|
195
203
|
getAuthSettings: async () => {
|
|
@@ -107,6 +107,7 @@ export const createSupabaseAuthBackend = ({
|
|
|
107
107
|
const hashParams = new URLSearchParams(locationHash.replace(/^#/, ''));
|
|
108
108
|
return buildSessionFromParams(hashParams, nowSeconds);
|
|
109
109
|
},
|
|
110
|
+
exchangeOAuthSessionCode: async () => null,
|
|
110
111
|
exchangeOAuthCode: async () => null,
|
|
111
112
|
restoreSession: async (storedSession, nowSeconds) => {
|
|
112
113
|
if (!storedSession) return null;
|
|
@@ -4,6 +4,11 @@ import type { AuthSession, AuthSettings, UserPreferences } from '../types';
|
|
|
4
4
|
export interface AuthBackend {
|
|
5
5
|
type: BackendConfig['type'] | 'none';
|
|
6
6
|
readSessionFromCallback: (locationHash: string, nowSeconds: number) => AuthSession | null;
|
|
7
|
+
exchangeOAuthSessionCode: (params: {
|
|
8
|
+
authCode: string;
|
|
9
|
+
redirectTo: string;
|
|
10
|
+
nowSeconds: number;
|
|
11
|
+
}) => Promise<AuthSession | null>;
|
|
7
12
|
exchangeOAuthCode: (params: {
|
|
8
13
|
provider: string;
|
|
9
14
|
code: string;
|
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
import { AppBrandIcon } from '../../layouts/branding/AppBrandIcon';
|
|
26
26
|
import { DESKTOP_TITLEBAR_HEIGHT_PX } from '../../layouts/chrome/constants';
|
|
27
27
|
import { useMacTrafficLights } from '../../layouts/chrome/runtime';
|
|
28
|
+
import { validateLoginPanelUrl } from '../../security/urlAllowlist';
|
|
28
29
|
import { AccessPendingView } from './AccessPendingView';
|
|
29
30
|
import { LoginPreferencesControls } from './LoginPreferencesControls';
|
|
30
31
|
|
|
@@ -416,7 +417,10 @@ export const LoginView = () => {
|
|
|
416
417
|
}
|
|
417
418
|
};
|
|
418
419
|
|
|
419
|
-
const panelUrl =
|
|
420
|
+
const panelUrl = useMemo(
|
|
421
|
+
() => validateLoginPanelUrl(config.backend?.login?.panelUrl, config),
|
|
422
|
+
[config],
|
|
423
|
+
);
|
|
420
424
|
const panelImage = config.backend?.login?.panelImage?.trim() || null;
|
|
421
425
|
const hasCustomPanel = Boolean(panelUrl || panelImage);
|
|
422
426
|
const legalDocuments = useMemo(() => getLegalDocuments(config), [config]);
|