Latest changes

This commit is contained in:
varjolintu 2022-10-20 17:51:21 +03:00
parent c3a17c430f
commit c44a80f9d1
2 changed files with 23 additions and 6 deletions

View file

@ -856,7 +856,7 @@ window.addEventListener('message', async (ev) => {
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);
window.postMessage({ action: 'webauthn-create-response', data: ret.response }, window.location.origin);
}
}
});

View file

@ -9,7 +9,7 @@ const postMessageToExtension = function(request) {
const handler = (msg) => {
if (msg && msg.data && msg.data.action === messageRequest + '-response') {
messageEvent.removeEventListener('message', listener);
resolve(msg.data);
resolve(msg.data.data);
return;
}
};
@ -22,6 +22,20 @@ const postMessageToExtension = function(request) {
});
};
const stringToArrayBuffer = function(str) {
const arr = Uint8Array.from(str, c => c.charCodeAt(0));
return arr.buffer;
};
const arrayToArrayBuffer = function(arr) {
const buf = Uint8Array.from(arr);
return buf.buffer;
}
const base64ToArrayBuffer = function(str) {
return stringToArrayBuffer(atob(str));
};
(async () => {
const webauthnCredentials = {
async create(options) {
@ -37,13 +51,16 @@ const postMessageToExtension = function(request) {
publicKey.user = options.publicKey.user;
publicKey.user.id = window.btoa(options.publicKey.user.id); // Use b64 instead
const response = await postMessageToExtension({ action: 'webauthn-create', publicKey: publicKey });
console.log('Response: ', response);
const publicKeyCredential = await postMessageToExtension({ action: 'webauthn-create', publicKey: publicKey });
console.log('Response: ', publicKeyCredential);
// Parse the response and change needed variables from b64 to ArrayBuffer/UInt8Array etc.
publicKeyCredential.rawId = base64ToArrayBuffer(publicKeyCredential.id);
publicKeyCredential.response.attestationObject.authData = arrayToArrayBuffer(publicKeyCredential.response.attestationObject.authData);
publicKeyCredential.response.clientDataJSON = stringToArrayBuffer(JSON.stringify(publicKeyCredential.response.clientDataJSON));
return [];
console.log('Final response: ', publicKeyCredential);
return publicKeyCredential;
},
async get(options) {
console.log('Overridden get');