diff --git a/keepassxc-browser/background/keepass.js b/keepassxc-browser/background/keepass.js index 4074594..24c5db0 100755 --- a/keepassxc-browser/background/keepass.js +++ b/keepassxc-browser/background/keepass.js @@ -645,8 +645,7 @@ keepass.webauthnRegister = async function(tab, args = []) { const response = await keepassClient.sendMessage(kpAction, tab, messageData, nonce); if (response) { - - return []; + return response; } browserAction.showDefault(tab); diff --git a/keepassxc-browser/content/keepassxc-browser.js b/keepassxc-browser/content/keepassxc-browser.js index 3e35640..4c961a6 100755 --- a/keepassxc-browser/content/keepassxc-browser.js +++ b/keepassxc-browser/content/keepassxc-browser.js @@ -849,16 +849,14 @@ const webauthn = document.createElement('script'); webauthn.src = browser.runtime.getURL('content/webauthn.js'); document.documentElement.appendChild(webauthn); -window.addEventListener('message', (ev) => { +window.addEventListener('message', async (ev) => { if (ev.data.action === 'webauthn-create') { console.log('Request from: ', ev.origin); console.log(ev.data.publicKey); - sendMessage('webauthn-register', [ ev.data.publicKey, ev.origin ]); - - // Send a test response after a delay. webauthn.js should wait for this. - setTimeout(() => { - window.postMessage({ action: 'webauthn-create-response' }, window.location.origin); - }, 2000); + const ret = await sendMessage('webauthn-register', [ ev.data.publicKey, ev.origin ]); + if (ret.response) { + window.postMessage({ action: 'webauthn-create-response', data: ret.response.response }, window.location.origin); + } } }); diff --git a/keepassxc-browser/content/webauthn.js b/keepassxc-browser/content/webauthn.js index 0ab96bf..e500ab9 100644 --- a/keepassxc-browser/content/webauthn.js +++ b/keepassxc-browser/content/webauthn.js @@ -1,5 +1,27 @@ 'use strict'; +// Posts a message to extension's content script and waits for response +const postMessageToExtension = function(request) { + return new Promise((resolve, reject) => { + const ev = window; + + const listener = ((messageEvent, messageRequest) => { + const handler = (msg) => { + if (msg && msg.data && msg.data.action === messageRequest + '-response') { + messageEvent.removeEventListener('message', listener); + resolve(msg.data); + return; + } + }; + return handler; + })(ev, request.action); + ev.addEventListener('message', listener); + + // Send the request + window.postMessage(request, window.location.origin); + }); +}; + (async () => { const webauthnCredentials = { async create(options) { @@ -15,23 +37,23 @@ publicKey.user = options.publicKey.user; publicKey.user.id = window.btoa(options.publicKey.user.id); // Use b64 instead - window.postMessage({ action: 'webauthn-create', publicKey: publicKey }, window.location.origin); - //await waitForCreateResponse(); // How to do this? + const response = await postMessageToExtension({ action: 'webauthn-create', publicKey: publicKey }); + console.log('Response: ', response); + + // Parse the response and change needed variables from b64 to ArrayBuffer/UInt8Array etc. + + + return []; }, async get(options) { console.log('Overridden get'); } }; + // Overwrite navigator.credentials try { Object.assign(navigator.credentials, webauthnCredentials); } catch (err) { console.log(err); } - - window.addEventListener('message', (ev) => { - if (ev.data.action === 'webauthn-create-response') { - console.log('Response received!'); - } - }); })();