Electron BrowserWindow/BrowserView loadURL with a client certificate

Viewed 113

My Electron desktop app is visiting a https website in the renderer process with the methods of BrowserWindow.loadURL, BrowserView.loadURL or BrowserWindow.webContents.loadURL. However the target website needs to verify my client certificate. How can I make a request with the client certificate ? What I have are a .crt file, a .key file and two CA certificate files.

I tried the 'select-client-certificate' event, but it wasn't fired. I also tried app.commandLine.appendSwitch('client-certificate', <the path to .pfx certificate file>) but it didn't work either.

1 Answers

I had no success when trying select-client-certificate on the electron app but I was able to get it to work by doing the below. The other important thing is that select-client-certificate would only get called if when you try to do it in a typical browser (Safari, Chrome, Edge, etc) it shows a popup to select a certificate.

const child = new BrowserWindow({
    ...
});

child.webContents.on('select-client-certificate', (event: Event,
    url: string,
    certificateList: Certificate[],
    callback: (certificate: Certificate) => void) => {
    console.log('select-client-certificate url', url)
    console.log('select-client-certificate list', certificateList)
    // IMPORTANT: to prevent the default.
    event.preventDefault()

    // Selects the first certificate in the list of available.
    callback(certificateList[0])
});

// IMPORTANT: This is after .on('select-client-certificate')
await child.loadURL(url);
Related