Files
zitadel/acceptance/tests/passkey.ts

110 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-11-15 13:48:42 +01:00
import { expect, Page } from "@playwright/test";
import { CDPSession } from "playwright-core";
2024-10-31 16:53:23 +01:00
2024-11-13 19:48:25 +01:00
interface session {
2024-11-15 13:48:42 +01:00
client: CDPSession;
authenticatorId: string;
2024-11-13 19:48:25 +01:00
}
async function client(page: Page): Promise<session> {
2024-11-15 13:48:42 +01:00
const cdpSession = await page.context().newCDPSession(page);
await cdpSession.send("WebAuthn.enable", { enableUI: false });
const result = await cdpSession.send("WebAuthn.addVirtualAuthenticator", {
options: {
protocol: "ctap2",
transport: "internal",
hasResidentKey: true,
hasUserVerification: true,
isUserVerified: true,
automaticPresenceSimulation: true,
},
});
return { client: cdpSession, authenticatorId: result.authenticatorId };
2024-11-13 19:48:25 +01:00
}
export async function passkeyRegister(page: Page): Promise<string> {
2024-11-15 13:48:42 +01:00
const session = await client(page);
await passkeyNotExisting(session.client, session.authenticatorId);
await simulateSuccessfulPasskeyRegister(session.client, session.authenticatorId, () =>
page.getByTestId("submit-button").click(),
);
await passkeyRegistered(session.client, session.authenticatorId);
return session.authenticatorId;
2024-11-13 19:48:25 +01:00
}
export async function passkey(page: Page, authenticatorId: string) {
2024-11-15 13:48:42 +01:00
const cdpSession = await page.context().newCDPSession(page);
await cdpSession.send("WebAuthn.enable", { enableUI: false });
2024-11-13 19:48:25 +01:00
2024-11-15 13:48:42 +01:00
const signCount = await passkeyExisting(cdpSession, authenticatorId);
2024-11-13 19:48:25 +01:00
2024-11-15 13:48:42 +01:00
await simulateSuccessfulPasskeyInput(cdpSession, authenticatorId, () => page.getByTestId("submit-button").click());
2024-10-31 16:53:23 +01:00
2024-11-15 13:48:42 +01:00
await passkeyUsed(cdpSession, authenticatorId, signCount);
2024-11-13 19:48:25 +01:00
}
async function passkeyNotExisting(client: CDPSession, authenticatorId: string) {
2024-11-15 13:48:42 +01:00
const result = await client.send("WebAuthn.getCredentials", { authenticatorId });
expect(result.credentials).toHaveLength(0);
2024-11-13 19:48:25 +01:00
}
async function passkeyRegistered(client: CDPSession, authenticatorId: string) {
2024-11-15 13:48:42 +01:00
const result = await client.send("WebAuthn.getCredentials", { authenticatorId });
expect(result.credentials).toHaveLength(1);
await passkeyUsed(client, authenticatorId, 0);
2024-11-13 19:48:25 +01:00
}
async function passkeyExisting(client: CDPSession, authenticatorId: string): Promise<number> {
2024-11-15 13:48:42 +01:00
const result = await client.send("WebAuthn.getCredentials", { authenticatorId });
expect(result.credentials).toHaveLength(1);
return result.credentials[0].signCount;
2024-11-13 19:48:25 +01:00
}
async function passkeyUsed(client: CDPSession, authenticatorId: string, signCount: number) {
2024-11-15 13:48:42 +01:00
const result = await client.send("WebAuthn.getCredentials", { authenticatorId });
expect(result.credentials).toHaveLength(1);
expect(result.credentials[0].signCount).toBeGreaterThan(signCount);
2024-11-13 19:48:25 +01:00
}
2024-11-15 13:48:42 +01:00
async function simulateSuccessfulPasskeyRegister(
client: CDPSession,
authenticatorId: string,
operationTrigger: () => Promise<void>,
) {
// initialize event listeners to wait for a successful passkey input event
const operationCompleted = new Promise<void>((resolve) => {
client.on("WebAuthn.credentialAdded", () => {
console.log("Credential Added!");
resolve();
2024-10-31 16:53:23 +01:00
});
2024-11-15 13:48:42 +01:00
});
2024-10-31 16:53:23 +01:00
2024-11-15 13:48:42 +01:00
// perform a user action that triggers passkey prompt
await operationTrigger();
2024-10-31 16:53:23 +01:00
2024-11-15 13:48:42 +01:00
// wait to receive the event that the passkey was successfully registered or verified
await operationCompleted;
2024-11-13 19:48:25 +01:00
}
2024-10-31 16:53:23 +01:00
2024-11-15 13:48:42 +01:00
async function simulateSuccessfulPasskeyInput(
client: CDPSession,
authenticatorId: string,
operationTrigger: () => Promise<void>,
) {
// initialize event listeners to wait for a successful passkey input event
const operationCompleted = new Promise<void>((resolve) => {
client.on("WebAuthn.credentialAsserted", () => {
console.log("Credential Asserted!");
resolve();
2024-11-13 19:48:25 +01:00
});
2024-11-15 13:48:42 +01:00
});
2024-10-31 16:53:23 +01:00
2024-11-15 13:48:42 +01:00
// perform a user action that triggers passkey prompt
await operationTrigger();
2024-11-13 19:48:25 +01:00
2024-11-15 13:48:42 +01:00
// wait to receive the event that the passkey was successfully registered or verified
await operationCompleted;
2024-11-13 19:48:25 +01:00
}