11// @effect -diagnostics globalTimers:off - Ceremonies settle inside IPC handlers, outside an Effect runtime.
22import type { IpcMainInvokeEvent , Session , WebContents , WebFrameMain } from "electron" ;
33import { app , BrowserWindow , dialog , webContents as electronWebContents } from "electron" ;
4- import type { WebauthnGetRequestOptions } from "electron-webauthn" ;
54import * as Context from "effect/Context" ;
65import * as Effect from "effect/Effect" ;
76import * as FileSystem from "effect/FileSystem" ;
87import * as Layer from "effect/Layer" ;
98import * as Schema from "effect/Schema" ;
10- import { getPublicSuffix } from "tldts" ;
119
1210import * as DesktopEnvironment from "../app/DesktopEnvironment.ts" ;
1311import { PASSKEY_CREATE_CHANNEL , PASSKEY_GET_CHANNEL } from "./GuestProtocol.ts" ;
14- import { authenticatorDataFromAttestation } from "./PasskeyAttestation.ts" ;
12+ import type { PasskeyBackend , PasskeyCeremonyContext } from "./PasskeyBackend.ts" ;
13+ import { macPasskeyBackend } from "./PasskeyBackendMac.ts" ;
1514import type { PasskeyCeremonyResult } from "./PasskeyBridge.ts" ;
1615
1716const PasskeyPackageMetadata = Schema . Struct ( {
@@ -69,11 +68,6 @@ export class PreviewPasskeys extends Context.Service<
6968
7069const notAllowed : PasskeyCeremonyResult = { success : false , error : "NotAllowedError" } ;
7170
72- // WebAuthn rejects RP IDs that are public suffixes, private registries
73- // included, so one github.io site cannot mint passkeys for every other one.
74- const isPublicSuffix = ( domain : string ) =>
75- domain !== "localhost" && getPublicSuffix ( domain , { allowPrivateDomains : true } ) === domain ;
76-
7771const accountLabel = ( account : Electron . WebAuthnAccount ) =>
7872 account . displayName && account . name && account . displayName !== account . name
7973 ? `${ account . displayName } (${ account . name } )`
@@ -105,13 +99,6 @@ const chooseAccount = async (details: Electron.SelectWebauthnAccountDetails) =>
10599 return details . accounts [ response ] ?. credentialId ;
106100} ;
107101
108- type WebAuthn = typeof import ( "electron-webauthn" ) ;
109- type Ceremony = (
110- webauthn : WebAuthn ,
111- options : WebauthnGetRequestOptions ,
112- ) => Promise < PasskeyCeremonyResult > ;
113-
114- const ES256 = - 7 ;
115102const NATIVE_DEFAULT_TIMEOUT_MS = 10 * 60 * 1000 ;
116103const NATIVE_MAX_TIMEOUT_MS = 60 * 60 * 1000 ;
117104
@@ -130,64 +117,25 @@ const withDeadline = (ceremony: Promise<PasskeyCeremonyResult>, milliseconds: nu
130117 return Promise . race ( [ ceremony , deadline ] ) . finally ( ( ) => clearTimeout ( timer ) ) ;
131118} ;
132119
133- // Secure contexts only, as in Chromium: https, or http on this machine.
120+ // Secure contexts only: https, or http on this machine. Chromium also trusts
121+ // `*.localhost`, but the macOS layer refuses it, so it is turned away here
122+ // instead of failing deeper with a less useful error.
134123const isTrustworthyOrigin = ( origin : string ) => {
135124 if ( ! URL . canParse ( origin ) ) return false ;
136125 const { protocol, hostname } = new URL ( origin ) ;
137126 return (
138127 protocol === "https:" ||
139128 ( protocol === "http:" &&
140- ( hostname === "localhost" ||
141- hostname . endsWith ( ".localhost" ) ||
142- hostname === "127.0.0.1" ||
143- hostname === "[::1]" ) )
129+ ( hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]" ) )
144130 ) ;
145131} ;
146132
147- const createCeremony =
148- ( publicKey : PublicKeyCredentialCreationOptions ) : Ceremony =>
149- async ( webauthn , options ) => {
150- // The native layer only converts P-256 keys and never settles for any
151- // other algorithm, so ES256 is the only one it may negotiate.
152- const params : unknown = publicKey . pubKeyCredParams ;
153- const allowsEs256 =
154- ! Array . isArray ( params ) ||
155- params . length === 0 ||
156- params . some (
157- ( param : unknown ) =>
158- typeof param === "object" && param !== null && "alg" in param && param . alg === ES256 ,
159- ) ;
160- if ( ! allowsEs256 ) return { success : false , error : "NotSupportedError" } ;
161- const result = await webauthn . createCredential (
162- { ...publicKey , pubKeyCredParams : [ { type : "public-key" , alg : ES256 } ] } ,
163- options ,
164- ) ;
165- if ( ! result . success ) return { success : false , error : result . error } ;
166- // The native layer reports parsed authenticator data as JSON and claims
167- // every credential is a synced platform passkey; neither is reliable.
168- const authData = authenticatorDataFromAttestation (
169- Buffer . from ( result . data . attestationObject , "base64url" ) ,
170- ) ;
171- return {
172- success : true ,
173- data : {
174- ...result . data ,
175- authData : authData ? Buffer . from ( authData ) . toString ( "base64url" ) : "" ,
176- transports : [ ] ,
177- } ,
178- } ;
179- } ;
180-
181- const getCeremony =
182- ( publicKey : PublicKeyCredentialRequestOptions , origin : string ) : Ceremony =>
183- async ( webauthn , options ) => {
184- // WebAuthn defaults the RP ID to the caller's host; the native layer requires it.
185- const result = await webauthn . getCredential (
186- { ...publicKey , rpId : publicKey . rpId ?? new URL ( origin ) . hostname } ,
187- options ,
188- ) ;
189- return result . success ? result : { success : false , error : result . error } ;
190- } ;
133+ /**
134+ * The platform that serves preview pages' passkey ceremonies, or none. Windows
135+ * needs none: Chromium already hands WebAuthn to Windows Hello there.
136+ */
137+ const backendFor = ( platform : NodeJS . Platform ) : PasskeyBackend | undefined =>
138+ platform === "darwin" ? macPasskeyBackend : undefined ;
191139
192140const make = Effect . gen ( function * ( ) {
193141 const environment = yield * DesktopEnvironment . DesktopEnvironment ;
@@ -204,7 +152,8 @@ const make = Effect.gen(function* () {
204152 )
205153 : undefined ;
206154 const keychainAccessGroup = metadata ?. touchIdKeychainAccessGroup ;
207- const bridgeEnabled = metadata ?. browserPasskeys === true ;
155+ const backend = metadata ?. browserPasskeys === true ? backendFor ( environment . platform ) : undefined ;
156+ const bridgeEnabled = backend !== undefined ;
208157 const sessionsWithHandlers = new WeakSet < Session > ( ) ;
209158
210159 return PreviewPasskeys . of ( {
@@ -228,12 +177,12 @@ const make = Effect.gen(function* () {
228177 } ) ;
229178 } ,
230179 attachGuest : ( guest ) => {
231- if ( ! bridgeEnabled ) return ( ) => { } ;
180+ if ( backend === undefined ) return ( ) => { } ;
232181 let ceremonyPending = false ;
233182 const serve = async (
234183 event : IpcMainInvokeEvent ,
235184 publicKey : { readonly timeout ?: unknown } | undefined ,
236- ceremony : ( origin : string ) => Ceremony ,
185+ ceremony : ( context : PasskeyCeremonyContext ) => Promise < PasskeyCeremonyResult > ,
237186 ) : Promise < PasskeyCeremonyResult > => {
238187 // Only the guest's main frame runs the bridge preload.
239188 const frame = event . senderFrame ;
@@ -256,14 +205,8 @@ const make = Effect.gen(function* () {
256205
257206 ceremonyPending = true ;
258207 try {
259- const webauthn = await import ( "electron-webauthn" ) ;
260208 const result = await withDeadline (
261- ceremony ( origin ) ( webauthn , {
262- currentOrigin : origin ,
263- topFrameOrigin : origin ,
264- nativeWindowHandle : host . getNativeWindowHandle ( ) ,
265- isPublicSuffix,
266- } ) ,
209+ ceremony ( { origin, nativeWindowHandle : host . getNativeWindowHandle ( ) } ) ,
267210 ceremonyDeadline ( publicKey . timeout ) ,
268211 ) ;
269212 // A credential minted for a document that navigated away belongs to nobody.
@@ -286,10 +229,10 @@ const make = Effect.gen(function* () {
286229 guest . ipc . handle (
287230 PASSKEY_CREATE_CHANNEL ,
288231 ( event , publicKey : PublicKeyCredentialCreationOptions ) =>
289- serve ( event , publicKey , ( ) => createCeremony ( publicKey ) ) ,
232+ serve ( event , publicKey , ( context ) => backend . create ( publicKey , context ) ) ,
290233 ) ;
291234 guest . ipc . handle ( PASSKEY_GET_CHANNEL , ( event , publicKey : PublicKeyCredentialRequestOptions ) =>
292- serve ( event , publicKey , ( origin ) => getCeremony ( publicKey , origin ) ) ,
235+ serve ( event , publicKey , ( context ) => backend . get ( publicKey , context ) ) ,
293236 ) ;
294237 return detach ;
295238 } ,
0 commit comments