mirror of
https://github.com/keepassxreboot/keepassxc-browser.git
synced 2026-03-11 08:54:43 +00:00
A working request and response
This commit is contained in:
parent
a70ee7431f
commit
c3a17c430f
3 changed files with 36 additions and 17 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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!');
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
|
|
|||
Loading…
Reference in a new issue