|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { crossServerOrigin, panelAuth } from '../auth-token'; |
| 3 | + |
| 4 | +const DEVFRAME_KEY = '__DEVFRAME_CONNECTION_AUTH_TOKEN__'; |
| 5 | +const extension = { |
| 6 | + href: 'chrome-extension://dcogniffeelebaolkkfbopmjcblhblfk/ui/index.html?baseURL=x', |
| 7 | + origin: 'chrome-extension://dcogniffeelebaolkkfbopmjcblhblfk', |
| 8 | +}; |
| 9 | + |
| 10 | +function memoryStorage(initial: Record<string, string> = {}) { |
| 11 | + const data = new Map(Object.entries(initial)); |
| 12 | + return { |
| 13 | + data, |
| 14 | + getItem: (key: string) => data.get(key) ?? null, |
| 15 | + setItem: (key: string, value: string) => void data.set(key, value), |
| 16 | + removeItem: (key: string) => void data.delete(key), |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +describe('panel auth tokens', () => { |
| 21 | + it('treats a server on the panel origin as its own', () => { |
| 22 | + const hub = { |
| 23 | + href: 'http://localhost:4000/__devframes/pangular/', |
| 24 | + origin: 'http://localhost:4000', |
| 25 | + }; |
| 26 | + expect(crossServerOrigin('/__devframes/pangular/', hub)).toBeNull(); |
| 27 | + expect(crossServerOrigin(['/__pangular/', './'], hub)).toBeNull(); |
| 28 | + expect(crossServerOrigin(undefined, hub)).toBeNull(); |
| 29 | + const storage = memoryStorage({ [DEVFRAME_KEY]: 'own' }); |
| 30 | + expect(panelAuth('/__devframes/pangular/', hub, storage).token).toBeUndefined(); |
| 31 | + // devframe keeps managing the token of its own origin. |
| 32 | + expect(storage.data.get(DEVFRAME_KEY)).toBe('own'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('keeps one token per server origin in the extension', () => { |
| 36 | + const storage = memoryStorage(); |
| 37 | + const vite = 'http://localhost:5173/__devframes/pangular/'; |
| 38 | + const hub = 'http://localhost:4000/__devframes/pangular/'; |
| 39 | + expect(crossServerOrigin(vite, extension)).toBe('http://localhost:5173'); |
| 40 | + |
| 41 | + const first = panelAuth(vite, extension, storage); |
| 42 | + expect(first.token).toBeUndefined(); |
| 43 | + first.remember('vite-token'); |
| 44 | + panelAuth(hub, extension, storage).remember('hub-token'); |
| 45 | + |
| 46 | + expect(panelAuth(vite, extension, storage).token).toBe('vite-token'); |
| 47 | + expect(panelAuth(hub, extension, storage).token).toBe('hub-token'); |
| 48 | + }); |
| 49 | + |
| 50 | + it("never leaves devframe's shared slot for the next server to send", () => { |
| 51 | + const storage = memoryStorage({ [DEVFRAME_KEY]: 'token-of-another-server' }); |
| 52 | + const auth = panelAuth('http://localhost:5173/', extension, storage); |
| 53 | + expect(storage.data.has(DEVFRAME_KEY)).toBe(false); |
| 54 | + storage.data.set(DEVFRAME_KEY, 'copied-by-devframe'); |
| 55 | + auth.remember('new-token'); |
| 56 | + expect(storage.data.has(DEVFRAME_KEY)).toBe(false); |
| 57 | + expect(auth.token).toBe('new-token'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('ignores an empty token', () => { |
| 61 | + const storage = memoryStorage(); |
| 62 | + panelAuth('http://localhost:5173/', extension, storage).remember(undefined); |
| 63 | + expect(storage.data.size).toBe(0); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +/** Two documents on one origin: a shared storage area and a shared broadcast bus. */ |
| 68 | +function sharedOrigin() { |
| 69 | + const data = new Map<string, string>(); |
| 70 | + const bus = new Set<{ name: string; onmessage: ((event: { data: unknown }) => void) | null }>(); |
| 71 | + const panel = () => { |
| 72 | + class PanelStorage { |
| 73 | + getItem(key: string) { |
| 74 | + return data.get(key) ?? null; |
| 75 | + } |
| 76 | + setItem(key: string, value: string) { |
| 77 | + data.set(key, value); |
| 78 | + } |
| 79 | + removeItem(key: string) { |
| 80 | + data.delete(key); |
| 81 | + } |
| 82 | + } |
| 83 | + class PanelChannel { |
| 84 | + onmessage: ((event: { data: unknown }) => void) | null = null; |
| 85 | + constructor(readonly name: string) { |
| 86 | + bus.add(this); |
| 87 | + } |
| 88 | + postMessage(message: unknown) { |
| 89 | + for (const other of bus) { |
| 90 | + if (other !== this && other.name === this.name) other.onmessage?.({ data: message }); |
| 91 | + } |
| 92 | + } |
| 93 | + close() { |
| 94 | + bus.delete(this); |
| 95 | + } |
| 96 | + } |
| 97 | + const scope = { |
| 98 | + Storage: PanelStorage as unknown as typeof Storage, |
| 99 | + BroadcastChannel: PanelChannel as unknown as typeof BroadcastChannel, |
| 100 | + }; |
| 101 | + return { scope, storage: new PanelStorage() }; |
| 102 | + }; |
| 103 | + return { data, panel }; |
| 104 | +} |
| 105 | + |
| 106 | +/** What devframe's client does with the one-time code token, as in devframe/client. */ |
| 107 | +function devframeClient( |
| 108 | + scope: { BroadcastChannel: typeof BroadcastChannel }, |
| 109 | + storage: Pick<Storage, 'setItem'>, |
| 110 | + sentToServer: string[], |
| 111 | +) { |
| 112 | + let channel: BroadcastChannel | undefined; |
| 113 | + try { |
| 114 | + channel = new scope.BroadcastChannel('devframe-auth'); |
| 115 | + } catch { |
| 116 | + // devframe leaves the channel out when it cannot open one. |
| 117 | + } |
| 118 | + const store = (token: string) => { |
| 119 | + try { |
| 120 | + storage.setItem(DEVFRAME_KEY, token); |
| 121 | + } catch { |
| 122 | + // As devframe: storage may be blocked. |
| 123 | + } |
| 124 | + }; |
| 125 | + if (channel) { |
| 126 | + channel.onmessage = (event: MessageEvent) => { |
| 127 | + const data = event.data as { type?: string; authToken?: string }; |
| 128 | + if (data?.type === 'auth-update' && data.authToken) { |
| 129 | + store(data.authToken); |
| 130 | + sentToServer.push(data.authToken); |
| 131 | + } |
| 132 | + }; |
| 133 | + } |
| 134 | + return { |
| 135 | + trustWithCode(token: string) { |
| 136 | + store(token); |
| 137 | + try { |
| 138 | + channel?.postMessage({ type: 'auth-update', authToken: token }); |
| 139 | + } catch { |
| 140 | + // As devframe. |
| 141 | + } |
| 142 | + }, |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +describe('two extension panels on different servers', () => { |
| 147 | + it("never hand one server's token to the other", () => { |
| 148 | + const origin = sharedOrigin(); |
| 149 | + const first = origin.panel(); |
| 150 | + const second = origin.panel(); |
| 151 | + const authA = panelAuth('http://localhost:4000/', extension, first.storage, first.scope); |
| 152 | + const authB = panelAuth('http://localhost:5173/', extension, second.storage, second.scope); |
| 153 | + const sentToA: string[] = []; |
| 154 | + const sentToB: string[] = []; |
| 155 | + const clientA = devframeClient(first.scope, first.storage, sentToA); |
| 156 | + devframeClient(second.scope, second.storage, sentToB); |
| 157 | + |
| 158 | + clientA.trustWithCode('token-of-a'); |
| 159 | + authA.remember('token-of-a'); |
| 160 | + |
| 161 | + expect(sentToB).toEqual([]); |
| 162 | + expect(origin.data.has(DEVFRAME_KEY)).toBe(false); |
| 163 | + expect(authB.token).toBeUndefined(); |
| 164 | + expect(panelAuth('http://localhost:5173/', extension, second.storage, second.scope).token).toBe( |
| 165 | + undefined, |
| 166 | + ); |
| 167 | + expect(panelAuth('http://localhost:4000/', extension, first.storage, first.scope).token).toBe( |
| 168 | + 'token-of-a', |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + it('leave the channel and the slot to devframe for panels next to their server', () => { |
| 173 | + const origin = sharedOrigin(); |
| 174 | + const hub = { |
| 175 | + href: 'http://localhost:4000/__devframes/pangular/', |
| 176 | + origin: 'http://localhost:4000', |
| 177 | + }; |
| 178 | + const first = origin.panel(); |
| 179 | + const second = origin.panel(); |
| 180 | + panelAuth('/__devframes/pangular/', hub, first.storage, first.scope); |
| 181 | + panelAuth('/__devframes/pangular/', hub, second.storage, second.scope); |
| 182 | + const sent: string[] = []; |
| 183 | + const client = devframeClient(first.scope, first.storage, []); |
| 184 | + devframeClient(second.scope, second.storage, sent); |
| 185 | + client.trustWithCode('hub-token'); |
| 186 | + // Same server: devframe shares the token between its tabs, as it should. |
| 187 | + expect(sent).toEqual(['hub-token']); |
| 188 | + expect(origin.data.get(DEVFRAME_KEY)).toBe('hub-token'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('keep other broadcast channels and storage keys working', () => { |
| 192 | + const origin = sharedOrigin(); |
| 193 | + const panel = origin.panel(); |
| 194 | + panelAuth('http://localhost:4000/', extension, panel.storage, panel.scope); |
| 195 | + expect(() => new panel.scope.BroadcastChannel('other')).not.toThrow(); |
| 196 | + panel.storage.setItem('pangular-tab', 'routes'); |
| 197 | + expect(panel.storage.getItem('pangular-tab')).toBe('routes'); |
| 198 | + }); |
| 199 | +}); |
0 commit comments