|
| 1 | +import type { DevToolsNodeContext } from '@vitejs/devtools-kit' |
| 2 | +import type { RpcFunctionsHost } from './host-functions' |
| 3 | +import { getInternalContext } from './context-internal' |
| 4 | + |
| 5 | +/** |
| 6 | + * Revoke an auth token: remove from storage and notify all connected clients |
| 7 | + * using this token that they are no longer trusted. |
| 8 | + */ |
| 9 | +export async function revokeAuthToken(context: DevToolsNodeContext, token: string): Promise<void> { |
| 10 | + const internal = getInternalContext(context) |
| 11 | + const storage = internal.storage.auth |
| 12 | + |
| 13 | + // Remove from persistent storage |
| 14 | + storage.mutate((state) => { |
| 15 | + delete state.trusted[token] |
| 16 | + }) |
| 17 | + |
| 18 | + const rpcHost = context.rpc as unknown as RpcFunctionsHost |
| 19 | + if (!rpcHost._rpcGroup) |
| 20 | + return |
| 21 | + |
| 22 | + // Collect affected session IDs before modifying meta |
| 23 | + const affectedSessionIds = new Set<string>() |
| 24 | + for (const client of rpcHost._rpcGroup.clients) { |
| 25 | + if (client.$meta.clientAuthId === token) { |
| 26 | + affectedSessionIds.add(client.$meta.id) |
| 27 | + client.$meta.isTrusted = false |
| 28 | + client.$meta.clientAuthId = undefined! |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (affectedSessionIds.size === 0) |
| 33 | + return |
| 34 | + |
| 35 | + // Notify affected clients |
| 36 | + await rpcHost.broadcast({ |
| 37 | + method: 'devtoolskit:internal:auth:revoked', |
| 38 | + args: [], |
| 39 | + filter: client => affectedSessionIds.has(client.$meta.id), |
| 40 | + }) |
| 41 | +} |
0 commit comments