@shellui/core 0.7.0-alpha.1 → 0.7.0-alpha.11
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/package.json +2 -2
- package/schemas/shellui.config.schema.json +37 -4
- package/src/components/ContentView.tsx +19 -8
- package/src/components/ui/drawer.tsx +47 -30
- package/src/components/ui/sheet.tsx +18 -12
- package/src/components/ui/sidebar.tsx +25 -11
- package/src/components/ui/tooltip.tsx +4 -2
- package/src/features/admin/AdminView.tsx +9 -6
- package/src/features/auth/AuthProvider.tsx +25 -4
- package/src/features/auth/appBackend/appBackendSessions.spec.ts +134 -0
- package/src/features/auth/appBackend/appBackendSessions.ts +168 -17
- package/src/features/auth/appBackend/childFrames.spec.ts +328 -0
- package/src/features/auth/appBackend/childFrames.ts +180 -0
- package/src/features/auth/appBackend/frameAuth.ts +47 -10
- package/src/features/auth/appBackend/silentSignIn.spec.ts +18 -0
- package/src/features/auth/appBackend/silentSignIn.ts +15 -1
- package/src/features/auth/appBackend/tokenExchange.spec.ts +98 -0
- package/src/features/auth/appBackend/tokenExchange.ts +76 -0
- package/src/features/auth/components/LoginButton.spec.tsx +155 -0
- package/src/features/auth/components/LoginButton.tsx +52 -27
- package/src/features/auth/components/LoginButtonIcons.tsx +4 -8
- package/src/features/auth/components/OAuthCallbackView.tsx +7 -5
- package/src/features/auth/utils/buildOAuthSessionRedirectTo.spec.ts +42 -1
- package/src/features/auth/utils/buildOAuthSessionRedirectTo.ts +23 -0
- package/src/features/auth/utils/canRestoreStoredSession.spec.ts +29 -0
- package/src/features/auth/utils/canRestoreStoredSession.ts +12 -0
- package/src/features/auth/utils/index.ts +7 -1
- package/src/features/auth/utils/resolveSdkLoginRedirectPath.spec.ts +41 -0
- package/src/features/auth/utils/resolveSdkLoginRedirectPath.ts +31 -0
- package/src/features/auth/utils/webPkce.spec.ts +23 -0
- package/src/features/auth/utils/webPkce.ts +23 -6
- package/src/features/chromeActions/ChromeActionsHost.tsx +5 -1
- package/src/features/chromeActions/computeActionInsets.ts +7 -4
- package/src/features/config/types.ts +30 -4
- package/src/features/layouts/OverlayShell.tsx +58 -40
- package/src/features/layouts/appbar/AppBarItemIcon.spec.tsx +45 -0
- package/src/features/layouts/appbar/AppBarItemIcon.tsx +19 -0
- package/src/features/layouts/appbar/AppBarLayout.tsx +115 -110
- package/src/features/layouts/appbar/appBarOverflow.spec.ts +27 -0
- package/src/features/layouts/appbar/appBarOverflow.ts +42 -0
- package/src/features/layouts/chrome/CollapsedDesktopTitlebar.tsx +13 -24
- package/src/features/layouts/chrome/ContentDragOverlay.tsx +5 -6
- package/src/features/layouts/chrome/DesktopChrome.tsx +37 -0
- package/src/features/layouts/chrome/DesktopHistoryButtons.tsx +15 -4
- package/src/features/layouts/chrome/MacStrip.tsx +33 -0
- package/src/features/layouts/chrome/constants.ts +36 -8
- package/src/features/layouts/chrome/index.ts +8 -0
- package/src/features/layouts/chrome/stripDragMap.ts +35 -0
- package/src/features/layouts/chrome/stripNoDrag.spec.tsx +103 -0
- package/src/features/layouts/chrome/useScrollHideChrome.ts +3 -8
- package/src/features/layouts/chrome/viewport.ts +11 -4
- package/src/features/layouts/filterNavigationForAuthState.spec.ts +5 -3
- package/src/features/layouts/floating/FloatingBottomDock.tsx +263 -43
- package/src/features/layouts/floating/FloatingLayout.tsx +14 -8
- package/src/features/layouts/floating/FloatingSidebar.tsx +165 -73
- package/src/features/layouts/floating/computeFloatingInsets.ts +22 -4
- package/src/features/layouts/floating/moreSections.spec.ts +28 -0
- package/src/features/layouts/floating/moreSections.ts +31 -0
- package/src/features/layouts/floating/useFloatingChrome.ts +3 -1
- package/src/features/layouts/floating/useSoftKeyboard.spec.ts +39 -0
- package/src/features/layouts/floating/useSoftKeyboard.ts +82 -0
- package/src/features/layouts/fullscreen/FullscreenLayout.spec.tsx +68 -0
- package/src/features/layouts/fullscreen/FullscreenLayout.tsx +47 -10
- package/src/features/layouts/sidebar/NavigationContent.tsx +17 -9
- package/src/features/layouts/sidebar/SidebarIcons.tsx +70 -9
- package/src/features/layouts/sidebar/SidebarInner.spec.tsx +109 -0
- package/src/features/layouts/sidebar/SidebarInner.tsx +94 -43
- package/src/features/layouts/sidebar/SidebarLayout.tsx +24 -17
- package/src/features/layouts/utils.ts +4 -3
- package/src/features/layouts/windows/DesktopDots.tsx +76 -0
- package/src/features/layouts/windows/WindowsLayout.tsx +251 -161
- package/src/features/layouts/windows/deskDots.spec.ts +54 -0
- package/src/features/layouts/windows/deskDots.ts +314 -0
- package/src/features/overlays/ResponsiveModal.tsx +5 -3
- package/src/features/overlays/overlaySize.spec.ts +56 -16
- package/src/features/overlays/overlaySize.ts +114 -31
- package/src/features/overlays/phoneDrawerAnchoring.spec.ts +187 -0
- package/src/features/overlays/useOverlayReportedSize.ts +8 -3
- package/src/features/settings/SettingsProvider.tsx +158 -2
- package/src/features/settings/utils/buildSettingsForPropagation.spec.ts +8 -7
- package/src/features/settings/utils/buildSettingsForPropagation.ts +4 -6
- package/src/features/theme/applyTheme.ts +8 -0
- package/src/features/theme/navContrast.spec.ts +49 -0
- package/src/features/theme/navContrast.ts +108 -0
- package/src/features/transfers/transferQueue.spec.ts +6 -2
- package/src/i18n/translations/en/common.json +26 -0
- package/src/i18n/translations/fr/common.json +26 -0
- package/src/index.css +1145 -77
- package/src/index.ts +1 -0
- package/src/routes/hooks/useNavigationItems.ts +14 -3
- package/src/routes/hooks/withShellSearch.spec.ts +13 -0
- package/src/service-worker/devCaches.spec.ts +32 -0
- package/src/service-worker/devCaches.ts +27 -0
- package/src/service-worker/sw-dev.ts +23 -23
|
@@ -11,6 +11,8 @@ import {
|
|
|
11
11
|
getAppBackendSession,
|
|
12
12
|
setAppBackendFactory,
|
|
13
13
|
setAppBackendSignIn,
|
|
14
|
+
setAppBackendTokenExchange,
|
|
15
|
+
resetAppBackendTokenExchange,
|
|
14
16
|
} from './appBackendSessions';
|
|
15
17
|
import { resolveAppBackendConfig } from './identity';
|
|
16
18
|
import { runSilentPkceSignIn } from './silentSignIn';
|
|
@@ -192,4 +194,136 @@ describe('app backend sessions', () => {
|
|
|
192
194
|
);
|
|
193
195
|
expect(dumped).not.toContain(APP_BACKEND_REFRESH_PREFIX);
|
|
194
196
|
});
|
|
197
|
+
|
|
198
|
+
describe('token exchange for upstream identity servers', () => {
|
|
199
|
+
const exchange = vi.fn();
|
|
200
|
+
const jwt = (sub: string, jti: string) =>
|
|
201
|
+
`h.${btoa(JSON.stringify({ sub, jti })).replace(/=+$/, '')}.s`;
|
|
202
|
+
let shellToken: string | null = jwt('1', 'g1');
|
|
203
|
+
|
|
204
|
+
function upstream(companyId?: number) {
|
|
205
|
+
const identity = resolveAppBackendConfig(
|
|
206
|
+
{ url: 'https://id.eu.example', companyId, provider: 'upstream', type: 'shellui' },
|
|
207
|
+
{ backend: { type: 'shellui', url: 'https://id.example.com', companyId: 1 } },
|
|
208
|
+
);
|
|
209
|
+
if (!identity) throw new Error('expected identity');
|
|
210
|
+
return identity;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function due(accessToken: string, refreshToken: string): AuthSession {
|
|
214
|
+
return {
|
|
215
|
+
...session(accessToken, refreshToken),
|
|
216
|
+
expiresAt: Math.floor(Date.now() / 1000) + 10,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function stored(): string {
|
|
221
|
+
return JSON.stringify(
|
|
222
|
+
[...Array(sessionStorage.length).keys()].map((index) => {
|
|
223
|
+
const key = sessionStorage.key(index) ?? '';
|
|
224
|
+
return [key, sessionStorage.getItem(key)];
|
|
225
|
+
}),
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
beforeEach(() => {
|
|
230
|
+
shellToken = jwt('1', 'g1');
|
|
231
|
+
exchange.mockReset();
|
|
232
|
+
setAppBackendTokenExchange(async () => shellToken, exchange);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
afterEach(() => {
|
|
236
|
+
resetAppBackendTokenExchange();
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('exchanges the shell token at the regional server and never uses silent sign-in', async () => {
|
|
240
|
+
exchange.mockResolvedValue({ ok: true, session: session('regional-1', 'regional-refresh') });
|
|
241
|
+
const identity = upstream(5);
|
|
242
|
+
|
|
243
|
+
const token = await ensureAppBackendSession(identity, { allowSilent: false });
|
|
244
|
+
|
|
245
|
+
expect(token).toBe('regional-1');
|
|
246
|
+
expect(exchange).toHaveBeenCalledWith(
|
|
247
|
+
expect.objectContaining({
|
|
248
|
+
backendUrl: 'https://id.eu.example',
|
|
249
|
+
companyId: 5,
|
|
250
|
+
shellAccessToken: shellToken,
|
|
251
|
+
}),
|
|
252
|
+
);
|
|
253
|
+
expect(signIn).not.toHaveBeenCalled();
|
|
254
|
+
expect(getAppBackendSession(identity.key)?.accessToken).toBe('regional-1');
|
|
255
|
+
expect(stored()).toContain('regional-refresh');
|
|
256
|
+
expect(stored()).not.toContain('regional-1"');
|
|
257
|
+
expect(stored()).not.toContain(shellToken);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('renews with the regional refresh token, not a new exchange', async () => {
|
|
261
|
+
exchange.mockResolvedValueOnce({ ok: true, session: due('regional-old', 'regional-r1') });
|
|
262
|
+
const identity = upstream(5);
|
|
263
|
+
await ensureAppBackendSession(identity);
|
|
264
|
+
refreshAuthSession.mockResolvedValueOnce(session('regional-new', 'regional-r2'));
|
|
265
|
+
|
|
266
|
+
shellToken = jwt('1', 'g2');
|
|
267
|
+
expect(await ensureAppBackendSession(identity)).toBe('regional-new');
|
|
268
|
+
expect(refreshAuthSession).toHaveBeenCalledWith(
|
|
269
|
+
expect.objectContaining({ refreshToken: 'regional-r1' }),
|
|
270
|
+
expect.any(Number),
|
|
271
|
+
);
|
|
272
|
+
expect(factory).toHaveBeenCalledWith(
|
|
273
|
+
expect.objectContaining({ url: 'https://id.eu.example' }),
|
|
274
|
+
);
|
|
275
|
+
expect(exchange).toHaveBeenCalledTimes(1);
|
|
276
|
+
expect(stored()).toContain('regional-r2');
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('exchanges again when the regional refresh fails', async () => {
|
|
280
|
+
exchange.mockResolvedValueOnce({ ok: true, session: due('regional-old', 'regional-r1') });
|
|
281
|
+
const identity = upstream(5);
|
|
282
|
+
await ensureAppBackendSession(identity);
|
|
283
|
+
refreshAuthSession.mockRejectedValueOnce(new Error('HTTP 401'));
|
|
284
|
+
exchange.mockResolvedValueOnce({ ok: true, session: session('regional-new', 'regional-r3') });
|
|
285
|
+
|
|
286
|
+
expect(await ensureAppBackendSession(identity)).toBe('regional-new');
|
|
287
|
+
expect(exchange).toHaveBeenCalledTimes(2);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('drops the regional session and exchanges when the shell user changes', async () => {
|
|
291
|
+
exchange.mockResolvedValueOnce({ ok: true, session: session('regional-ada', 'r-ada') });
|
|
292
|
+
const identity = upstream(5);
|
|
293
|
+
await ensureAppBackendSession(identity);
|
|
294
|
+
|
|
295
|
+
shellToken = jwt('2', 'g9');
|
|
296
|
+
exchange.mockResolvedValueOnce({ ok: true, session: session('regional-bob', 'r-bob') });
|
|
297
|
+
expect(await ensureAppBackendSession(identity)).toBe('regional-bob');
|
|
298
|
+
expect(refreshAuthSession).not.toHaveBeenCalled();
|
|
299
|
+
expect(stored()).not.toContain('r-ada');
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('does not retry a failed exchange until the shell token changes', async () => {
|
|
303
|
+
exchange.mockResolvedValue({ ok: false, reason: 'invalid_grant' });
|
|
304
|
+
const identity = upstream(5);
|
|
305
|
+
|
|
306
|
+
expect(await ensureAppBackendSession(identity)).toBeNull();
|
|
307
|
+
expect(await ensureAppBackendSession(identity)).toBeNull();
|
|
308
|
+
expect(exchange).toHaveBeenCalledTimes(1);
|
|
309
|
+
|
|
310
|
+
shellToken = jwt('1', 'g2');
|
|
311
|
+
exchange.mockResolvedValue({ ok: true, session: session('regional-2') });
|
|
312
|
+
expect(await ensureAppBackendSession(identity)).toBe('regional-2');
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('clears the regional session on shell sign-out', async () => {
|
|
316
|
+
exchange.mockResolvedValueOnce({ ok: true, session: session('regional-1', 'r1') });
|
|
317
|
+
const identity = upstream(5);
|
|
318
|
+
await ensureAppBackendSession(identity);
|
|
319
|
+
|
|
320
|
+
await endAppBackendSessions();
|
|
321
|
+
expect(getAppBackendSession(identity.key)).toBeNull();
|
|
322
|
+
expect(sessionStorage.length).toBe(0);
|
|
323
|
+
|
|
324
|
+
shellToken = null;
|
|
325
|
+
expect(await ensureAppBackendSession(identity)).toBeNull();
|
|
326
|
+
expect(exchange).toHaveBeenCalledTimes(1);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
195
329
|
});
|
|
@@ -10,9 +10,45 @@ import {
|
|
|
10
10
|
type ResolvedAppBackend,
|
|
11
11
|
} from './identity';
|
|
12
12
|
import { runSilentPkceSignIn, type SilentSignInOutcome } from './silentSignIn';
|
|
13
|
+
import {
|
|
14
|
+
describeAppSessionFailure,
|
|
15
|
+
exchangeShellTokenForAppSession,
|
|
16
|
+
type TokenExchangeOutcome,
|
|
17
|
+
} from './tokenExchange';
|
|
18
|
+
import { refreshFrameAccessTokenIfNeeded } from '../utils/frameSessionRefresher';
|
|
19
|
+
import { decodeJwtPayload } from '../utils/decodeJwtPayload';
|
|
13
20
|
|
|
14
21
|
const logger = getLogger('shellcore');
|
|
15
22
|
|
|
23
|
+
/** Why the last session attempt for an app identity server failed (no secrets). */
|
|
24
|
+
const lastFailure = new Map<string, string>();
|
|
25
|
+
|
|
26
|
+
export function getAppBackendSessionFailure(key: string): string | null {
|
|
27
|
+
return lastFailure.get(key) ?? null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Let the next ensure call try silent sign-in again (user asked to retry). */
|
|
31
|
+
export function allowSilentRetry(key: string): void {
|
|
32
|
+
silentAttempted.delete(key);
|
|
33
|
+
failedShellToken.delete(key);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Identity servers that trust the shell's global identity get a session by token exchange. */
|
|
37
|
+
export function usesTokenExchange(identity: ResolvedAppBackend): boolean {
|
|
38
|
+
return identity.type === 'shellui' && identity.provider === 'upstream';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function fail(identity: ResolvedAppBackend, reason: string): null {
|
|
42
|
+
lastFailure.set(identity.key, reason);
|
|
43
|
+
logger.warn('App session: no session for an app identity server', {
|
|
44
|
+
url: identity.url,
|
|
45
|
+
companyId: identity.companyId ?? null,
|
|
46
|
+
reason,
|
|
47
|
+
hint: describeAppSessionFailure(reason),
|
|
48
|
+
});
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
16
52
|
/** Tab-scoped refresh tokens for app identity servers. Not the shell refresh key. */
|
|
17
53
|
export const APP_BACKEND_REFRESH_PREFIX = 'shellui.auth.app.refresh.';
|
|
18
54
|
|
|
@@ -30,6 +66,8 @@ const memory = new Map<string, StoredEntry>();
|
|
|
30
66
|
const refreshTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
31
67
|
const inFlight = new Map<string, Promise<string | null>>();
|
|
32
68
|
const silentAttempted = new Set<string>();
|
|
69
|
+
/** Shell token that last failed an exchange, so the same token is not retried in a loop. Memory only. */
|
|
70
|
+
const failedShellToken = new Map<string, string>();
|
|
33
71
|
const listeners = new Set<() => void>();
|
|
34
72
|
|
|
35
73
|
let backendFactory: (config: BackendConfig) => AppBackendClient = createAuthBackend;
|
|
@@ -39,6 +77,24 @@ let signInImpl: (input: {
|
|
|
39
77
|
companyId?: string | number;
|
|
40
78
|
}) => Promise<SilentSignInOutcome> = runSilentPkceSignIn;
|
|
41
79
|
|
|
80
|
+
let shellTokenImpl: () => Promise<string | null | undefined> = refreshFrameAccessTokenIfNeeded;
|
|
81
|
+
let exchangeImpl: typeof exchangeShellTokenForAppSession = exchangeShellTokenForAppSession;
|
|
82
|
+
|
|
83
|
+
export function setAppBackendTokenExchange(
|
|
84
|
+
shellToken: () => Promise<string | null | undefined>,
|
|
85
|
+
exchange: (
|
|
86
|
+
input: Parameters<typeof exchangeShellTokenForAppSession>[0],
|
|
87
|
+
) => Promise<TokenExchangeOutcome> = exchangeShellTokenForAppSession,
|
|
88
|
+
): void {
|
|
89
|
+
shellTokenImpl = shellToken;
|
|
90
|
+
exchangeImpl = exchange;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function resetAppBackendTokenExchange(): void {
|
|
94
|
+
shellTokenImpl = refreshFrameAccessTokenIfNeeded;
|
|
95
|
+
exchangeImpl = exchangeShellTokenForAppSession;
|
|
96
|
+
}
|
|
97
|
+
|
|
42
98
|
export function setAppBackendFactory(factory: (config: BackendConfig) => AppBackendClient): void {
|
|
43
99
|
backendFactory = factory;
|
|
44
100
|
}
|
|
@@ -55,6 +111,7 @@ export function setAppBackendSignIn(
|
|
|
55
111
|
|
|
56
112
|
export function resetAppBackendSilentAttempts(): void {
|
|
57
113
|
silentAttempted.clear();
|
|
114
|
+
failedShellToken.clear();
|
|
58
115
|
}
|
|
59
116
|
|
|
60
117
|
export function subscribeAppBackendSessions(listener: () => void): () => void {
|
|
@@ -170,6 +227,7 @@ async function doEnsure(
|
|
|
170
227
|
identity: ResolvedAppBackend,
|
|
171
228
|
allowSilent: boolean,
|
|
172
229
|
): Promise<string | null> {
|
|
230
|
+
if (usesTokenExchange(identity)) return ensureByTokenExchange(identity);
|
|
173
231
|
const now = Math.floor(Date.now() / 1000);
|
|
174
232
|
const current = memory.get(identity.key)?.session ?? null;
|
|
175
233
|
if (current?.accessToken && !accessTokenRefreshDue(current.expiresAt, now)) {
|
|
@@ -194,8 +252,13 @@ async function doEnsure(
|
|
|
194
252
|
}
|
|
195
253
|
}
|
|
196
254
|
|
|
197
|
-
if (!allowSilent
|
|
198
|
-
if (
|
|
255
|
+
if (!allowSilent) return null;
|
|
256
|
+
if (identity.type !== 'shellui' || !identity.provider) {
|
|
257
|
+
return fail(identity, 'silent_sign_in_unsupported');
|
|
258
|
+
}
|
|
259
|
+
if (silentAttempted.has(identity.key)) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
199
262
|
silentAttempted.add(identity.key);
|
|
200
263
|
|
|
201
264
|
let outcome: SilentSignInOutcome;
|
|
@@ -206,18 +269,10 @@ async function doEnsure(
|
|
|
206
269
|
companyId: identity.companyId,
|
|
207
270
|
});
|
|
208
271
|
} catch {
|
|
209
|
-
|
|
210
|
-
url: identity.url,
|
|
211
|
-
});
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
if (!outcome.ok) {
|
|
215
|
-
logger.info('Silent sign-in did not complete for an app identity server', {
|
|
216
|
-
url: identity.url,
|
|
217
|
-
reason: outcome.reason,
|
|
218
|
-
});
|
|
219
|
-
return null;
|
|
272
|
+
return fail(identity, 'silent_sign_in_error');
|
|
220
273
|
}
|
|
274
|
+
if (!outcome.ok) return fail(identity, outcome.reason);
|
|
275
|
+
logger.info('App session: silent sign-in returned a code, exchanging it', { url: identity.url });
|
|
221
276
|
|
|
222
277
|
try {
|
|
223
278
|
const session = await backendFactory(toBackendConfig(identity)).exchangeOAuthSessionCode({
|
|
@@ -226,17 +281,112 @@ async function doEnsure(
|
|
|
226
281
|
codeVerifier: outcome.codeVerifier,
|
|
227
282
|
nowSeconds: Math.floor(Date.now() / 1000),
|
|
228
283
|
});
|
|
229
|
-
if (!session?.accessToken) return
|
|
284
|
+
if (!session?.accessToken) return fail(identity, 'token_exchange_empty');
|
|
230
285
|
remember(identity, session);
|
|
286
|
+
lastFailure.delete(identity.key);
|
|
287
|
+
logger.info('App session: signed in to app identity server', { url: identity.url });
|
|
231
288
|
return session.accessToken;
|
|
289
|
+
} catch (err) {
|
|
290
|
+
const message = err instanceof Error ? err.message.slice(0, 200) : '';
|
|
291
|
+
return fail(identity, `token_exchange_failed${message ? `: ${message}` : ''}`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function shellSubject(token: string): string | null {
|
|
296
|
+
const sub = decodeJwtPayload(token)?.sub;
|
|
297
|
+
return typeof sub === 'string' || typeof sub === 'number' ? `${sub}` : null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function subjectKey(key: string): string {
|
|
301
|
+
return `${storageKey(key)}#sub`;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function readSubject(key: string): string | null {
|
|
305
|
+
try {
|
|
306
|
+
return sessionStorage.getItem(subjectKey(key));
|
|
232
307
|
} catch {
|
|
233
|
-
logger.info('Silent sign-in did not complete for an app identity server', {
|
|
234
|
-
url: identity.url,
|
|
235
|
-
});
|
|
236
308
|
return null;
|
|
237
309
|
}
|
|
238
310
|
}
|
|
239
311
|
|
|
312
|
+
function writeSubject(key: string, sub: string | null): void {
|
|
313
|
+
try {
|
|
314
|
+
if (sub) sessionStorage.setItem(subjectKey(key), sub);
|
|
315
|
+
else sessionStorage.removeItem(subjectKey(key));
|
|
316
|
+
} catch {
|
|
317
|
+
// Ignore.
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Regional session for an upstream identity server:
|
|
323
|
+
* keep it while valid, renew it with its own refresh token, and exchange the shell token
|
|
324
|
+
* only when there is none, the refresh fails, or the shell user changed.
|
|
325
|
+
*/
|
|
326
|
+
async function ensureByTokenExchange(identity: ResolvedAppBackend): Promise<string | null> {
|
|
327
|
+
let shellToken: string | null | undefined;
|
|
328
|
+
try {
|
|
329
|
+
shellToken = await shellTokenImpl();
|
|
330
|
+
} catch {
|
|
331
|
+
shellToken = null;
|
|
332
|
+
}
|
|
333
|
+
if (!shellToken) {
|
|
334
|
+
forget(identity.key);
|
|
335
|
+
writeSubject(identity.key, null);
|
|
336
|
+
return fail(identity, 'no_shell_session');
|
|
337
|
+
}
|
|
338
|
+
const subject = shellSubject(shellToken);
|
|
339
|
+
if (readSubject(identity.key) !== subject) {
|
|
340
|
+
// Another shell user, or a session from before subjects were kept: never reuse it.
|
|
341
|
+
forget(identity.key);
|
|
342
|
+
writeSubject(identity.key, null);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const now = Math.floor(Date.now() / 1000);
|
|
346
|
+
const current = memory.get(identity.key)?.session ?? null;
|
|
347
|
+
if (current?.accessToken && !accessTokenRefreshDue(current.expiresAt, now)) {
|
|
348
|
+
return current.accessToken;
|
|
349
|
+
}
|
|
350
|
+
const refreshToken = current?.refreshToken || readRefresh(identity.key);
|
|
351
|
+
if (refreshToken) {
|
|
352
|
+
try {
|
|
353
|
+
const next = await backendFactory(toBackendConfig(identity)).refreshAuthSession(
|
|
354
|
+
current ? { ...current, refreshToken } : sessionForRefresh(refreshToken),
|
|
355
|
+
now,
|
|
356
|
+
);
|
|
357
|
+
if (next?.accessToken) {
|
|
358
|
+
remember(identity, next);
|
|
359
|
+
return next.accessToken;
|
|
360
|
+
}
|
|
361
|
+
} catch {
|
|
362
|
+
// Fall through to a new exchange.
|
|
363
|
+
}
|
|
364
|
+
forget(identity.key);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (failedShellToken.get(identity.key) === shellToken) return null;
|
|
368
|
+
const outcome = await exchangeImpl({
|
|
369
|
+
backendUrl: identity.url,
|
|
370
|
+
companyId: identity.companyId,
|
|
371
|
+
shellAccessToken: shellToken,
|
|
372
|
+
nowSeconds: now,
|
|
373
|
+
});
|
|
374
|
+
if (!outcome.ok) {
|
|
375
|
+
failedShellToken.set(identity.key, shellToken);
|
|
376
|
+
forget(identity.key);
|
|
377
|
+
return fail(identity, outcome.reason);
|
|
378
|
+
}
|
|
379
|
+
failedShellToken.delete(identity.key);
|
|
380
|
+
writeSubject(identity.key, subject);
|
|
381
|
+
remember(identity, outcome.session);
|
|
382
|
+
lastFailure.delete(identity.key);
|
|
383
|
+
logger.info('App session: token exchange succeeded', {
|
|
384
|
+
url: identity.url,
|
|
385
|
+
companyId: identity.companyId ?? null,
|
|
386
|
+
});
|
|
387
|
+
return outcome.session.accessToken;
|
|
388
|
+
}
|
|
389
|
+
|
|
240
390
|
export function ensureAppBackendSession(
|
|
241
391
|
identity: ResolvedAppBackend,
|
|
242
392
|
options?: { allowSilent?: boolean },
|
|
@@ -278,6 +428,7 @@ export async function endAppBackendSessions(): Promise<void> {
|
|
|
278
428
|
refreshTimers.clear();
|
|
279
429
|
inFlight.clear();
|
|
280
430
|
silentAttempted.clear();
|
|
431
|
+
failedShellToken.clear();
|
|
281
432
|
clearAllRefresh();
|
|
282
433
|
notify();
|
|
283
434
|
await Promise.all(
|