A working request and response

This commit is contained in:
varjolintu 2022-10-19 10:42:11 +03:00
parent a70ee7431f
commit c3a17c430f
3 changed files with 36 additions and 17 deletions

View file

@ -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);

View file

@ -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);
}
}
});

View file

@ -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!');
}
});
})();