@shellui/core 0.5.0 → 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.
Files changed (51) hide show
  1. package/package.json +2 -2
  2. package/schemas/shellui.config.schema.json +98 -33
  3. package/src/app.tsx +2 -0
  4. package/src/components/ContentView.tsx +3 -5
  5. package/src/components/contentIframeSandbox.spec.ts +25 -0
  6. package/src/components/contentIframeSandbox.ts +43 -0
  7. package/src/features/auth/AuthProvider.tsx +156 -33
  8. package/src/features/auth/backends/index.ts +2 -0
  9. package/src/features/auth/backends/shellui.ts +43 -35
  10. package/src/features/auth/backends/supabase.ts +1 -0
  11. package/src/features/auth/backends/types.ts +5 -0
  12. package/src/features/auth/components/LoginView.tsx +5 -1
  13. package/src/features/auth/components/OAuthCallbackView.tsx +115 -44
  14. package/src/features/auth/constants/oauth.ts +2 -0
  15. package/src/features/auth/hooks/useAuth.tsx +4 -0
  16. package/src/features/auth/utils/authSessionPersistence.spec.ts +93 -0
  17. package/src/features/auth/utils/authSessionPersistence.ts +138 -0
  18. package/src/features/auth/utils/authStorageKeys.ts +8 -0
  19. package/src/features/auth/utils/bffAuthClient.ts +107 -0
  20. package/src/features/auth/utils/buildOAuthSessionRedirectTo.spec.ts +22 -0
  21. package/src/features/auth/utils/buildOAuthSessionRedirectTo.ts +17 -0
  22. package/src/features/auth/utils/buildSessionFromTokenPayload.spec.ts +29 -0
  23. package/src/features/auth/utils/buildSessionFromTokenPayload.ts +24 -0
  24. package/src/features/auth/utils/index.ts +18 -3
  25. package/src/features/auth/utils/isBffAuthEnabled.ts +12 -0
  26. package/src/features/config/types.ts +49 -2
  27. package/src/features/messaging/MessagingSecurityBootstrap.tsx +19 -0
  28. package/src/features/messaging/deriveAllowedMessageOrigins.spec.ts +86 -0
  29. package/src/features/messaging/deriveAllowedMessageOrigins.ts +74 -0
  30. package/src/features/modal/ModalContext.spec.ts +13 -2
  31. package/src/features/modal/validateAndNormalizeUrl.ts +9 -23
  32. package/src/features/security/trustedFrames.spec.ts +129 -0
  33. package/src/features/security/trustedFrames.ts +79 -0
  34. package/src/features/security/urlAllowlist.spec.ts +99 -0
  35. package/src/features/security/urlAllowlist.ts +122 -0
  36. package/src/features/settings/SettingsProvider.tsx +10 -46
  37. package/src/features/settings/utils/buildSettingsForPropagation.spec.ts +23 -1
  38. package/src/features/settings/utils/buildSettingsForPropagation.ts +7 -0
  39. package/src/features/storage/StorageBridge.spec.ts +82 -0
  40. package/src/features/storage/StorageBridge.tsx +25 -3
  41. package/src/features/storage/StoragePickerContext.tsx +3 -4
  42. package/src/features/storage/uploads/uploadQueue.ts +2 -5
  43. package/src/routes/components/NavigationItemRoute.tsx +30 -15
  44. package/src/routes/utils/navigationRouteAccess.spec.ts +85 -0
  45. package/src/routes/utils/navigationRouteAccess.ts +43 -0
  46. package/src/features/auth/utils/clearStoredAuthSession.spec.ts +0 -23
  47. package/src/features/auth/utils/clearStoredAuthSession.ts +0 -10
  48. package/src/features/auth/utils/persistAuthSession.spec.ts +0 -35
  49. package/src/features/auth/utils/persistAuthSession.ts +0 -12
  50. package/src/features/auth/utils/readStoredAuthSession.spec.ts +0 -34
  51. package/src/features/auth/utils/readStoredAuthSession.ts +0 -14
@@ -0,0 +1,85 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { NavigationItem } from '../../features/config/types';
3
+ import { getNavigationRouteAccess } from './navigationRouteAccess';
4
+
5
+ const staffRoute: NavigationItem = {
6
+ label: 'Staff tools',
7
+ path: 'staff',
8
+ url: 'https://app.example.com/staff',
9
+ requiresStaff: true,
10
+ };
11
+
12
+ const authRoute: NavigationItem = {
13
+ label: 'Billing',
14
+ path: 'billing',
15
+ url: 'https://app.example.com/billing',
16
+ requiresAuth: true,
17
+ };
18
+
19
+ const publicRoute: NavigationItem = {
20
+ label: 'Home',
21
+ path: 'home',
22
+ url: 'https://app.example.com/',
23
+ };
24
+
25
+ describe('getNavigationRouteAccess', () => {
26
+ it('allows public routes', () => {
27
+ expect(
28
+ getNavigationRouteAccess(
29
+ publicRoute,
30
+ { isLoading: false, isAuthenticated: false, isStaff: false },
31
+ '/home',
32
+ ),
33
+ ).toEqual({ kind: 'allow' });
34
+ });
35
+
36
+ it('redirects unauthenticated users on requiresAuth routes', () => {
37
+ expect(
38
+ getNavigationRouteAccess(
39
+ authRoute,
40
+ { isLoading: false, isAuthenticated: false, isStaff: false },
41
+ '/billing?tab=1',
42
+ ),
43
+ ).toEqual({ kind: 'login', next: '/billing?tab=1' });
44
+ });
45
+
46
+ it('denies non-staff users on requiresStaff routes', () => {
47
+ expect(
48
+ getNavigationRouteAccess(
49
+ staffRoute,
50
+ { isLoading: false, isAuthenticated: true, isStaff: false },
51
+ '/staff',
52
+ ),
53
+ ).toEqual({ kind: 'forbidden' });
54
+ });
55
+
56
+ it('denies signed-out users on requiresStaff routes', () => {
57
+ expect(
58
+ getNavigationRouteAccess(
59
+ staffRoute,
60
+ { isLoading: false, isAuthenticated: false, isStaff: false },
61
+ '/staff',
62
+ ),
63
+ ).toEqual({ kind: 'forbidden' });
64
+ });
65
+
66
+ it('allows staff users on requiresStaff routes', () => {
67
+ expect(
68
+ getNavigationRouteAccess(
69
+ staffRoute,
70
+ { isLoading: false, isAuthenticated: true, isStaff: true },
71
+ '/staff',
72
+ ),
73
+ ).toEqual({ kind: 'allow' });
74
+ });
75
+
76
+ it('waits while auth is loading', () => {
77
+ expect(
78
+ getNavigationRouteAccess(
79
+ authRoute,
80
+ { isLoading: true, isAuthenticated: false, isStaff: false },
81
+ '/billing',
82
+ ),
83
+ ).toEqual({ kind: 'loading' });
84
+ });
85
+ });
@@ -0,0 +1,43 @@
1
+ import type { NavigationItem } from '../../features/config/types';
2
+
3
+ export type NavigationRouteAccess =
4
+ | { kind: 'allow' }
5
+ | { kind: 'loading' }
6
+ | { kind: 'login'; next: string }
7
+ | { kind: 'forbidden' };
8
+
9
+ export type NavigationRouteAccessState = {
10
+ isLoading: boolean;
11
+ isAuthenticated: boolean;
12
+ isStaff: boolean;
13
+ };
14
+
15
+ /**
16
+ * Client-side route access for navigation items. APIs remain the source of truth for
17
+ * authorization; this is UX defense-in-depth for `requiresAuth` and `requiresStaff`.
18
+ */
19
+ export const getNavigationRouteAccess = (
20
+ item: NavigationItem,
21
+ state: NavigationRouteAccessState,
22
+ nextPath: string,
23
+ ): NavigationRouteAccess => {
24
+ if (item.requiresAuth) {
25
+ if (state.isLoading) {
26
+ return { kind: 'loading' };
27
+ }
28
+ if (!state.isAuthenticated) {
29
+ return { kind: 'login', next: nextPath };
30
+ }
31
+ }
32
+
33
+ if (item.requiresStaff) {
34
+ if (state.isLoading) {
35
+ return { kind: 'loading' };
36
+ }
37
+ if (!state.isAuthenticated || !state.isStaff) {
38
+ return { kind: 'forbidden' };
39
+ }
40
+ }
41
+
42
+ return { kind: 'allow' };
43
+ };
@@ -1,23 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { clearStoredAuthSession } from './clearStoredAuthSession';
3
-
4
- describe('clearStoredAuthSession', () => {
5
- beforeEach(() => {
6
- Object.defineProperty(globalThis, 'localStorage', {
7
- configurable: true,
8
- value: {
9
- getItem: vi.fn(),
10
- setItem: vi.fn(),
11
- removeItem: vi.fn(),
12
- clear: vi.fn(),
13
- key: vi.fn(() => null),
14
- length: 0,
15
- } as unknown as Storage,
16
- });
17
- });
18
-
19
- it('removes auth session from storage', () => {
20
- clearStoredAuthSession();
21
- expect(localStorage.removeItem).toHaveBeenCalledWith('shellui.auth.session');
22
- });
23
- });
@@ -1,10 +0,0 @@
1
- const AUTH_SESSION_STORAGE_KEY = 'shellui.auth.session';
2
-
3
- // Removes the persisted auth session from local storage.
4
- export const clearStoredAuthSession = () => {
5
- try {
6
- localStorage.removeItem(AUTH_SESSION_STORAGE_KEY);
7
- } catch {
8
- // Ignore storage errors.
9
- }
10
- };
@@ -1,35 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { persistAuthSession } from './persistAuthSession';
3
- import type { AuthSession } from '../types';
4
-
5
- const createStorageMock = () => {
6
- const data = new Map<string, string>();
7
- return {
8
- getItem: vi.fn((key: string) => data.get(key) ?? null),
9
- setItem: vi.fn((key: string, value: string) => data.set(key, value)),
10
- removeItem: vi.fn((key: string) => data.delete(key)),
11
- clear: vi.fn(() => data.clear()),
12
- key: vi.fn(() => null),
13
- get length() {
14
- return data.size;
15
- },
16
- } as unknown as Storage;
17
- };
18
-
19
- describe('persistAuthSession', () => {
20
- beforeEach(() => {
21
- Object.defineProperty(globalThis, 'localStorage', {
22
- configurable: true,
23
- value: createStorageMock(),
24
- });
25
- });
26
-
27
- it('stores session under auth storage key', () => {
28
- const session = { userId: '1' } as AuthSession;
29
- persistAuthSession(session);
30
- expect(localStorage.setItem).toHaveBeenCalledWith(
31
- 'shellui.auth.session',
32
- JSON.stringify(session),
33
- );
34
- });
35
- });
@@ -1,12 +0,0 @@
1
- import type { AuthSession } from '../types';
2
-
3
- const AUTH_SESSION_STORAGE_KEY = 'shellui.auth.session';
4
-
5
- // Persists the current auth session to local storage for browser reload recovery.
6
- export const persistAuthSession = (session: AuthSession) => {
7
- try {
8
- localStorage.setItem(AUTH_SESSION_STORAGE_KEY, JSON.stringify(session));
9
- } catch {
10
- // Ignore storage errors (e.g. private mode); user can still continue in-memory.
11
- }
12
- };
@@ -1,34 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { readStoredAuthSession } from './readStoredAuthSession';
3
-
4
- const createStorageMock = (value: string | null) =>
5
- ({
6
- getItem: vi.fn(() => value),
7
- setItem: vi.fn(),
8
- removeItem: vi.fn(),
9
- clear: vi.fn(),
10
- key: vi.fn(() => null),
11
- length: 0,
12
- }) as unknown as Storage;
13
-
14
- describe('readStoredAuthSession', () => {
15
- beforeEach(() => {
16
- Object.defineProperty(globalThis, 'localStorage', {
17
- configurable: true,
18
- value: createStorageMock(null),
19
- });
20
- });
21
-
22
- it('returns null when there is no stored session', () => {
23
- expect(readStoredAuthSession()).toBeNull();
24
- });
25
-
26
- it('parses and returns stored session data', () => {
27
- const stored = JSON.stringify({ userId: 'u1', expiresAt: 123 });
28
- Object.defineProperty(globalThis, 'localStorage', {
29
- configurable: true,
30
- value: createStorageMock(stored),
31
- });
32
- expect(readStoredAuthSession()).toEqual({ userId: 'u1', expiresAt: 123 });
33
- });
34
- });
@@ -1,14 +0,0 @@
1
- import type { AuthSession } from '../types';
2
-
3
- const AUTH_SESSION_STORAGE_KEY = 'shellui.auth.session';
4
-
5
- // Reads a previously persisted auth session from local storage.
6
- export const readStoredAuthSession = (): AuthSession | null => {
7
- try {
8
- const raw = localStorage.getItem(AUTH_SESSION_STORAGE_KEY);
9
- if (!raw) return null;
10
- return JSON.parse(raw) as AuthSession;
11
- } catch {
12
- return null;
13
- }
14
- };