Open History to see the last commit
1import { getPublicSuffix } from "tldts";
2
3import type { PasskeyBackend, PasskeyCeremonyContext } from "./PasskeyBackend.ts";
4import { authenticatorDataFromAttestation } from "./PasskeyAttestation.ts";
5import type { PasskeyCeremonyResult } from "./PasskeyBridge.ts";
6
7const ES256 = -7;
8
9// WebAuthn rejects RP IDs that are public suffixes, private registries
10// included, so one github.io site cannot mint passkeys for every other one.
11const isPublicSuffix = (domain: string) =>
12 domain !== "localhost" && getPublicSuffix(domain, { allowPrivateDomains: true }) === domain;
13
14// Loaded only once a ceremony runs: the module and its native addon ship on macOS alone.
15const nativeOptions = async (context: PasskeyCeremonyContext) => ({
16 webauthn: await import("electron-webauthn"),
17 options: {
18 currentOrigin: context.origin,
19 topFrameOrigin: context.origin,
20 nativeWindowHandle: context.nativeWindowHandle,
21 isPublicSuffix,
22 },
23});
24
25const isBufferSource = (value: unknown): value is BufferSource =>
26 value instanceof ArrayBuffer || ArrayBuffer.isView(value);
27
28const toBase64Url = (value: BufferSource) =>
29 Buffer.from(
30 ArrayBuffer.isView(value)
31 ? new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
32 : new Uint8Array(value),
33 ).toString("base64url");
34
35/**
36 * Needs Apple's managed browser entitlement in the signed build.
37 */
38export const macPasskeyBackend: PasskeyBackend = {
39 create: async (publicKey, context): Promise<PasskeyCeremonyResult> => {
40 // The native layer only converts P-256 keys and never settles for any
41 // other algorithm, so ES256 is the only one it may negotiate.
42 const params: unknown = publicKey.pubKeyCredParams;
43 if (params !== undefined && !Array.isArray(params))
44 return { success: false, error: "TypeError" };
45 const allowsEs256 =
46 params === undefined ||
47 params.length === 0 ||
48 params.some(
49 (param: unknown) =>
50 typeof param === "object" && param !== null && "alg" in param && param.alg === ES256,
51 );
52 if (!allowsEs256) return { success: false, error: "NotSupportedError" };
53 const { webauthn, options } = await nativeOptions(context);
54 const result = await webauthn.createCredential(
55 { ...publicKey, pubKeyCredParams: [{ type: "public-key", alg: ES256 }] },
56 options,
57 );
58 if (!result.success) return { success: false, error: result.error };
59 // The native layer reports parsed authenticator data as JSON and claims
60 // every credential is a synced platform passkey; neither is reliable.
61 const authData = authenticatorDataFromAttestation(
62 Buffer.from(result.data.attestationObject, "base64url"),
63 );
64 if (!authData) return { success: false, error: "NotAllowedError" };
65 return {
66 success: true,
67 data: {
68 ...result.data,
69 authData: Buffer.from(authData).toString("base64url"),
70 transports: [],
71 },
72 };
73 },
74 get: async (publicKey, context): Promise<PasskeyCeremonyResult> => {
75 const { webauthn, options } = await nativeOptions(context);
76 // WebAuthn defaults the RP ID to the caller's host; the native layer requires it.
77 const result = await webauthn.getCredential(
78 { ...publicKey, rpId: publicKey.rpId ?? new URL(context.origin).hostname },
79 options,
80 );
81 if (!result.success) return { success: false, error: result.error };
82 // The native layer applies the site's allow list to platform passkeys only,
83 // so a security key can answer with a credential the site did not ask for.
84 const allowed = (Array.isArray(publicKey.allowCredentials) ? publicKey.allowCredentials : [])
85 .filter((credential) => credential?.type === "public-key" && isBufferSource(credential.id))
86 .map((credential) => toBase64Url(credential.id));
87 if (allowed.length > 0 && !allowed.includes(result.data.credentialId)) {
88 return { success: false, error: "NotAllowedError" };
89 }
90 return result;
91 },
92};