|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {zod} from '../third_party/index.js'; |
| 8 | + |
| 9 | +import {ToolCategory} from './categories.js'; |
| 10 | +import {definePageTool} from './ToolDefinition.js'; |
| 11 | + |
| 12 | +const ACTIONS = [ |
| 13 | + 'status', |
| 14 | + 'enable', |
| 15 | + 'disable', |
| 16 | + 'addAuthenticator', |
| 17 | + 'removeAuthenticator', |
| 18 | + 'setUserVerified', |
| 19 | +] as const; |
| 20 | + |
| 21 | +type WebauthnAction = (typeof ACTIONS)[number]; |
| 22 | + |
| 23 | +type CdpClient = { |
| 24 | + send(method: string, params?: unknown): Promise<unknown>; |
| 25 | +}; |
| 26 | + |
| 27 | +function getCdpClient(page: {pptrPage: unknown}): CdpClient { |
| 28 | + // Puppeteer does not expose this via a stable public API yet. |
| 29 | + // @ts-expect-error internal API |
| 30 | + const client = page.pptrPage._client?.(); |
| 31 | + if (!client || typeof client.send !== 'function') { |
| 32 | + throw new Error('Unable to access CDP session for the selected page.'); |
| 33 | + } |
| 34 | + return client as CdpClient; |
| 35 | +} |
| 36 | + |
| 37 | +async function getStatus(client: CdpClient) { |
| 38 | + try { |
| 39 | + const result = (await client.send( |
| 40 | + 'WebAuthn.getCredentials', |
| 41 | + {}, |
| 42 | + )) as {credentials?: unknown[]}; |
| 43 | + return { |
| 44 | + enabled: true, |
| 45 | + authenticators: [] as Array<Record<string, unknown>>, |
| 46 | + credentials: result.credentials ?? [], |
| 47 | + }; |
| 48 | + } catch { |
| 49 | + return { |
| 50 | + enabled: false, |
| 51 | + authenticators: [] as Array<Record<string, unknown>>, |
| 52 | + credentials: [] as unknown[], |
| 53 | + }; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function handleAction( |
| 58 | + action: WebauthnAction, |
| 59 | + params: { |
| 60 | + authenticatorId?: string; |
| 61 | + userVerified?: boolean; |
| 62 | + protocol?: 'ctap2' | 'u2f'; |
| 63 | + transport?: 'usb' | 'nfc' | 'ble' | 'internal'; |
| 64 | + hasResidentKey?: boolean; |
| 65 | + hasUserVerification?: boolean; |
| 66 | + automaticPresenceSimulation?: boolean; |
| 67 | + isUserVerified?: boolean; |
| 68 | + }, |
| 69 | + client: CdpClient, |
| 70 | +) { |
| 71 | + switch (action) { |
| 72 | + case 'status': |
| 73 | + return {action, result: 'ok'}; |
| 74 | + case 'enable': |
| 75 | + await client.send('WebAuthn.enable'); |
| 76 | + return {action, result: 'enabled'}; |
| 77 | + case 'disable': |
| 78 | + await client.send('WebAuthn.disable'); |
| 79 | + return {action, result: 'disabled'}; |
| 80 | + case 'addAuthenticator': { |
| 81 | + const addResult = (await client.send('WebAuthn.addVirtualAuthenticator', { |
| 82 | + options: { |
| 83 | + protocol: params.protocol ?? 'ctap2', |
| 84 | + transport: params.transport ?? 'internal', |
| 85 | + hasResidentKey: params.hasResidentKey ?? true, |
| 86 | + hasUserVerification: params.hasUserVerification ?? true, |
| 87 | + automaticPresenceSimulation: |
| 88 | + params.automaticPresenceSimulation ?? true, |
| 89 | + isUserVerified: params.isUserVerified ?? true, |
| 90 | + }, |
| 91 | + })) as {authenticatorId?: string}; |
| 92 | + return { |
| 93 | + action, |
| 94 | + result: 'addedAuthenticator', |
| 95 | + authenticatorId: addResult.authenticatorId, |
| 96 | + }; |
| 97 | + } |
| 98 | + case 'removeAuthenticator': { |
| 99 | + if (!params.authenticatorId) { |
| 100 | + throw new Error('authenticatorId is required for removeAuthenticator'); |
| 101 | + } |
| 102 | + await client.send('WebAuthn.removeVirtualAuthenticator', { |
| 103 | + authenticatorId: params.authenticatorId, |
| 104 | + }); |
| 105 | + return {action, result: 'removedAuthenticator'}; |
| 106 | + } |
| 107 | + case 'setUserVerified': { |
| 108 | + if (!params.authenticatorId) { |
| 109 | + throw new Error('authenticatorId is required for setUserVerified'); |
| 110 | + } |
| 111 | + await client.send('WebAuthn.setUserVerified', { |
| 112 | + authenticatorId: params.authenticatorId, |
| 113 | + isUserVerified: params.userVerified ?? true, |
| 114 | + }); |
| 115 | + return {action, result: 'setUserVerified'}; |
| 116 | + } |
| 117 | + default: |
| 118 | + throw new Error(`Unsupported action: ${action as string}`); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export const configureWebauthn = definePageTool({ |
| 123 | + name: 'configure_webauthn', |
| 124 | + description: |
| 125 | + 'Configure experimental WebAuthn virtual authenticator state. Always returns status in the response.', |
| 126 | + annotations: { |
| 127 | + category: ToolCategory.DEBUGGING, |
| 128 | + readOnlyHint: false, |
| 129 | + conditions: ['experimentalWebauthn'], |
| 130 | + }, |
| 131 | + schema: { |
| 132 | + action: zod |
| 133 | + .enum(ACTIONS) |
| 134 | + .default('status') |
| 135 | + .describe('Action to apply to WebAuthn virtual authenticator state.'), |
| 136 | + authenticatorId: zod |
| 137 | + .string() |
| 138 | + .optional() |
| 139 | + .describe('Virtual authenticator ID for targeted actions.'), |
| 140 | + userVerified: zod |
| 141 | + .boolean() |
| 142 | + .optional() |
| 143 | + .describe('User verification state for setUserVerified action.'), |
| 144 | + protocol: zod |
| 145 | + .enum(['ctap2', 'u2f']) |
| 146 | + .optional() |
| 147 | + .describe('Authenticator protocol for addAuthenticator.'), |
| 148 | + transport: zod |
| 149 | + .enum(['usb', 'nfc', 'ble', 'internal']) |
| 150 | + .optional() |
| 151 | + .describe('Authenticator transport for addAuthenticator.'), |
| 152 | + hasResidentKey: zod |
| 153 | + .boolean() |
| 154 | + .optional() |
| 155 | + .describe('Whether resident keys are supported for addAuthenticator.'), |
| 156 | + hasUserVerification: zod |
| 157 | + .boolean() |
| 158 | + .optional() |
| 159 | + .describe('Whether user verification is supported for addAuthenticator.'), |
| 160 | + automaticPresenceSimulation: zod |
| 161 | + .boolean() |
| 162 | + .optional() |
| 163 | + .describe('Whether presence simulation is enabled for addAuthenticator.'), |
| 164 | + isUserVerified: zod |
| 165 | + .boolean() |
| 166 | + .optional() |
| 167 | + .describe('Initial user verification value for addAuthenticator.'), |
| 168 | + }, |
| 169 | + handler: async ({params, page}, response) => { |
| 170 | + const client = getCdpClient(page); |
| 171 | + const actionResult = await handleAction(params.action, params, client); |
| 172 | + const status = await getStatus(client); |
| 173 | + |
| 174 | + response.appendResponseLine('WebAuthn status:'); |
| 175 | + response.appendResponseLine(`- enabled: ${status.enabled}`); |
| 176 | + response.appendResponseLine( |
| 177 | + `- authenticators: ${status.authenticators.length}`, |
| 178 | + ); |
| 179 | + response.appendResponseLine(`- credentials: ${status.credentials.length}`); |
| 180 | + response.appendResponseLine(`Action result: ${JSON.stringify(actionResult)}`); |
| 181 | + }, |
| 182 | +}); |
| 183 | + |
0 commit comments