|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import * as pwCore from 'playwright-core/types/protocol'; |
| 6 | +import { CDPSession } from '@playwright/test'; |
| 7 | + |
| 8 | +const DEFAULT_OPTS: pwCore.Protocol.WebAuthn.VirtualAuthenticatorOptions = { |
| 9 | + protocol: 'ctap2', |
| 10 | + transport: 'internal', |
| 11 | + hasResidentKey: true, |
| 12 | + hasUserVerification: true, |
| 13 | + isUserVerified: true, |
| 14 | + automaticPresenceSimulation: false, |
| 15 | +}; |
| 16 | + |
| 17 | +type Trigger = () => Promise<void>; |
| 18 | +type PostCheck = () => Promise<void>; |
| 19 | + |
| 20 | +export class PasskeyVirtualAuthenticator { |
| 21 | + private authenticatorId = ''; |
| 22 | + |
| 23 | + constructor( |
| 24 | + private readonly client: CDPSession, |
| 25 | + private readonly opts: pwCore.Protocol.WebAuthn.VirtualAuthenticatorOptions = DEFAULT_OPTS |
| 26 | + ) {} |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates the virtual authenticator in the browser via CDP. |
| 30 | + * |
| 31 | + * Be sure to call this before using any other methods. |
| 32 | + * @param overrides |
| 33 | + * @returns |
| 34 | + */ |
| 35 | + async create( |
| 36 | + overrides?: Partial<pwCore.Protocol.WebAuthn.VirtualAuthenticatorOptions> |
| 37 | + ) { |
| 38 | + await this.client.send('WebAuthn.enable'); |
| 39 | + |
| 40 | + const { authenticatorId } = await this.client.send( |
| 41 | + 'WebAuthn.addVirtualAuthenticator', |
| 42 | + { |
| 43 | + options: { |
| 44 | + ...this.opts, |
| 45 | + ...overrides, |
| 46 | + automaticPresenceSimulation: false, |
| 47 | + }, |
| 48 | + } |
| 49 | + ); |
| 50 | + |
| 51 | + this.authenticatorId = authenticatorId; |
| 52 | + return authenticatorId; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Removes the virtual authenticator from the browser via CDP. |
| 57 | + * @returns |
| 58 | + */ |
| 59 | + async dispose() { |
| 60 | + if (!this.authenticatorId) return; |
| 61 | + await this.client.send('WebAuthn.removeVirtualAuthenticator', { |
| 62 | + authenticatorId: this.authenticatorId, |
| 63 | + }); |
| 64 | + this.authenticatorId = ''; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Cleanup the virtual authenticator and detach the CDP session. |
| 69 | + * Safe to call even if not initialized - will silently skip. |
| 70 | + */ |
| 71 | + async cleanup() { |
| 72 | + await this.dispose(); |
| 73 | + try { |
| 74 | + await this.client.send('WebAuthn.disable'); |
| 75 | + } catch { |
| 76 | + // Ignore errors if already disabled |
| 77 | + } |
| 78 | + await this.client.detach(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check how many credentials are stored in the virtual authenticator. |
| 83 | + * |
| 84 | + * Useful for verifying that a credential was created during registration, |
| 85 | + * or testing multiple registered credential flows. |
| 86 | + * @returns |
| 87 | + */ |
| 88 | + async getCredentials() { |
| 89 | + this.assertInit(); |
| 90 | + const result = await this.client.send('WebAuthn.getCredentials', { |
| 91 | + authenticatorId: this.authenticatorId, |
| 92 | + }); |
| 93 | + return result.credentials; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Simulate a *successful* passkey operation. |
| 98 | + * |
| 99 | + * Example: |
| 100 | + * ```ts |
| 101 | + * await passkeyAuth.success(async () => { |
| 102 | + * // Action that would show the Passkey prompt - this should return a promise |
| 103 | + * return signInPage.signinWithPasskey(); |
| 104 | + * // event listener will resolve when operation completes |
| 105 | + * } |
| 106 | + * }); |
| 107 | + * // then test asserts we're signed in or other success condition |
| 108 | + * ``` |
| 109 | + * @param trigger |
| 110 | + */ |
| 111 | + async success(trigger: Trigger) { |
| 112 | + this.assertInit(); |
| 113 | + |
| 114 | + // Enable presence simulation FIRST, before anything else |
| 115 | + // This gives the browser maximum time to activate it |
| 116 | + await this.client.send('WebAuthn.setAutomaticPresenceSimulation', { |
| 117 | + authenticatorId: this.authenticatorId, |
| 118 | + enabled: true, |
| 119 | + }); |
| 120 | + |
| 121 | + const operationCompleted = this.waitForOneOf([ |
| 122 | + 'WebAuthn.credentialAdded', |
| 123 | + 'WebAuthn.credentialAsserted', |
| 124 | + ]); |
| 125 | + |
| 126 | + await this.client.send('WebAuthn.setUserVerified', { |
| 127 | + authenticatorId: this.authenticatorId, |
| 128 | + isUserVerified: true, |
| 129 | + }); |
| 130 | + |
| 131 | + // Wait to ensure presence simulation is fully active |
| 132 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 133 | + |
| 134 | + try { |
| 135 | + await trigger(); |
| 136 | + await operationCompleted; |
| 137 | + } finally { |
| 138 | + await this.client.send('WebAuthn.setAutomaticPresenceSimulation', { |
| 139 | + authenticatorId: this.authenticatorId, |
| 140 | + enabled: false, |
| 141 | + }); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Simulate a *failed/cancelled* passkey operation. |
| 147 | + * There’s no CDP event for failure, so you must provide a post-check or use a small timeout. |
| 148 | + * |
| 149 | + * Example: |
| 150 | + * ```ts |
| 151 | + * await passkeyAuth.fail(async () => { |
| 152 | + * // Same as `success`, trigger an action that |
| 153 | + * // would show the Passkey prompt - this should return a promise |
| 154 | + * return signInPage.signinWithPasskey(); |
| 155 | + * }, async () => { |
| 156 | + * // Return a promise that will resolve after the 'cancellation' is processed. |
| 157 | + * // This could be a small timeout, or an assertion that an error message is shown. |
| 158 | + * return expect(page.locator('#error-message')).toBeVisible(); |
| 159 | + * }); |
| 160 | + * // then test asserts we're still signed out or other failure condition |
| 161 | + * // or check the `getCredentials()` to verify no new credentials were created |
| 162 | + * ``` |
| 163 | + */ |
| 164 | + async fail(trigger: Trigger, postCheck: PostCheck) { |
| 165 | + this.assertInit(); |
| 166 | + |
| 167 | + // Enable presence simulation FIRST, before anything else |
| 168 | + await this.client.send('WebAuthn.setAutomaticPresenceSimulation', { |
| 169 | + authenticatorId: this.authenticatorId, |
| 170 | + enabled: true, |
| 171 | + }); |
| 172 | + |
| 173 | + await this.client.send('WebAuthn.setUserVerified', { |
| 174 | + authenticatorId: this.authenticatorId, |
| 175 | + isUserVerified: false, |
| 176 | + }); |
| 177 | + |
| 178 | + // Wait to ensure presence simulation is fully active |
| 179 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 180 | + |
| 181 | + try { |
| 182 | + await trigger(); |
| 183 | + await postCheck(); |
| 184 | + } finally { |
| 185 | + await this.client.send('WebAuthn.setAutomaticPresenceSimulation', { |
| 186 | + authenticatorId: this.authenticatorId, |
| 187 | + enabled: false, |
| 188 | + }); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private waitForOneOf( |
| 193 | + events: Array<'WebAuthn.credentialAdded' | 'WebAuthn.credentialAsserted'> |
| 194 | + ) { |
| 195 | + return new Promise<void>((resolve) => { |
| 196 | + let done = false; |
| 197 | + |
| 198 | + const handlers = events.map(() => { |
| 199 | + const handler = () => { |
| 200 | + if (done) return; |
| 201 | + done = true; |
| 202 | + // remove all handlers when one fires |
| 203 | + for (let i = 0; i < events.length; i++) { |
| 204 | + this.client.off(events[i], handlers[i]); |
| 205 | + } |
| 206 | + resolve(); |
| 207 | + }; |
| 208 | + return handler; |
| 209 | + }); |
| 210 | + |
| 211 | + for (let i = 0; i < events.length; i++) { |
| 212 | + this.client.on(events[i], handlers[i]); |
| 213 | + } |
| 214 | + }); |
| 215 | + } |
| 216 | + |
| 217 | + private assertInit() { |
| 218 | + if (!this.authenticatorId) { |
| 219 | + throw new Error( |
| 220 | + 'PasskeyVirtualAuthenticator not initialized. Call init() first.' |
| 221 | + ); |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments