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