|
| 1 | +import { test, expect, CDPSession } from '@playwright/test'; |
| 2 | +import { asyncEvents } from './utils/async-events.js'; |
| 3 | +import { password, username } from './utils/demo-user.js'; |
| 4 | + |
| 5 | +test.use({ browserName: 'chromium' }); // ensure CDP/WebAuthn is available |
| 6 | + |
| 7 | +let cdp: CDPSession | undefined; |
| 8 | +let authenticatorId: string | undefined; |
| 9 | + |
| 10 | +test.beforeEach(async ({ context, page }) => { |
| 11 | + cdp = await context.newCDPSession(page); |
| 12 | + await cdp.send('WebAuthn.enable'); |
| 13 | + |
| 14 | + // A "platform" authenticator (aka internal) with UV+RK enabled is the usual default for passkeys. |
| 15 | + const response = await cdp.send('WebAuthn.addVirtualAuthenticator', { |
| 16 | + options: { |
| 17 | + protocol: 'ctap2', |
| 18 | + transport: 'internal', // platform authenticator |
| 19 | + hasResidentKey: true, // allow discoverable credentials (passkeys) |
| 20 | + hasUserVerification: true, // device supports UV |
| 21 | + isUserVerified: true, // simulate successful UV (PIN/biometric) |
| 22 | + automaticPresenceSimulation: true, // auto "touch"/presence |
| 23 | + }, |
| 24 | + }); |
| 25 | + authenticatorId = response.authenticatorId; |
| 26 | +}); |
| 27 | + |
| 28 | +test.afterEach(async () => { |
| 29 | + await cdp.send('WebAuthn.removeVirtualAuthenticator', { authenticatorId }); |
| 30 | + await cdp.send('WebAuthn.disable'); |
| 31 | +}); |
| 32 | + |
| 33 | +test('Register and authenticate with webauthn device', async ({ page }) => { |
| 34 | + const { navigate } = asyncEvents(page); |
| 35 | + |
| 36 | + await navigate('https://aj-test.pi.scrd.run:5829/?acr_values=ccff5c09002042bd86104da45cd7102e'); |
| 37 | + await expect(page).toHaveURL( |
| 38 | + 'https://aj-test.pi.scrd.run:5829/?acr_values=ccff5c09002042bd86104da45cd7102e', |
| 39 | + ); |
| 40 | + await expect(page.getByText('FIDO2 Test Form')).toBeVisible(); |
| 41 | + |
| 42 | + await page.getByRole('button', { name: 'USER_LOGIN' }).click(); |
| 43 | + await page.getByLabel('Username').fill(username); |
| 44 | + await page.getByLabel('Password').fill(password); |
| 45 | + await page.getByRole('button', { name: 'Sign On' }).click(); |
| 46 | + |
| 47 | + // Register WebAuthn credential |
| 48 | + const { credentials: intialCredentials } = await cdp.send('WebAuthn.getCredentials', { |
| 49 | + authenticatorId, |
| 50 | + }); |
| 51 | + await expect(intialCredentials).toHaveLength(0); |
| 52 | + |
| 53 | + await page.getByRole('button', { name: 'DEVICE_REGISTRATION' }).click(); |
| 54 | + await page.getByRole('button', { name: 'Biometrics/Security Key' }).click(); |
| 55 | + await page.getByRole('button', { name: 'FIDO Register' }).click(); |
| 56 | + |
| 57 | + const { credentials: recordedCredentials } = await cdp.send('WebAuthn.getCredentials', { |
| 58 | + authenticatorId, |
| 59 | + }); |
| 60 | + await expect(recordedCredentials).toHaveLength(1); |
| 61 | + |
| 62 | + await page.getByRole('button', { name: 'Continue' }).click(); |
| 63 | + |
| 64 | + // Verify we're back at home page if successful |
| 65 | + await expect(page.getByText('FIDO2 Test Form')).toBeVisible(); |
| 66 | + |
| 67 | + // Authenticate with the registered WebAuthn credential |
| 68 | + const initialSignCount = recordedCredentials[0].signCount; |
| 69 | + |
| 70 | + await page.getByRole('button', { name: 'DEVICE_AUTHENTICATION' }).click(); |
| 71 | + await page.getByRole('button', { name: 'Biometrics/Security Key' }).last().click(); |
| 72 | + await page.getByRole('button', { name: 'FIDO Authenticate' }).click(); |
| 73 | + |
| 74 | + const credentialsAfterAuth = await cdp.send('WebAuthn.getCredentials', { |
| 75 | + authenticatorId, |
| 76 | + }); |
| 77 | + await expect(credentialsAfterAuth.credentials).toHaveLength(1); |
| 78 | + |
| 79 | + // Signature counter should have incremented after successful authentication/assertion |
| 80 | + await expect(credentialsAfterAuth.credentials[0].signCount).toBeGreaterThan(initialSignCount); |
| 81 | + |
| 82 | + // Verify we're back at home page if successful |
| 83 | + await expect(page.getByText('FIDO2 Test Form')).toBeVisible(); |
| 84 | +}); |
0 commit comments