Skip to content

Commit 3532110

Browse files
committed
feat(a2): implement save frontend
1 parent 53c2c5b commit 3532110

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

a2/actions.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -175,29 +175,37 @@ async function signup(userInput, passInput, passInput2, emailInput) {
175175
/**
176176
* Called when the add password form is submitted.
177177
*/
178-
function save(siteInput, userInput, passInput) {
178+
async function save(siteInput, userInput, passInput) {
179179
var site = siteInput.value,
180180
siteuser = userInput.value,
181-
sitepasswd = passInput.value,
182-
encrypted; // this will need to be populated
181+
sitepasswd = passInput.value;
183182

184-
// send the data, along with the encrypted password, to the server
185-
serverRequest("save", // the resource to call
186-
{
187-
"site": site,
188-
"siteuser": siteuser,
189-
"sitepasswd": encrypted
190-
} // this should be populated with any parameters the server needs
191-
).then(function (result) {
192-
if (result.response.ok) {
193-
// any work after a successful save should be done here
183+
const rawKey = sessionStorage.getItem('encryption_key');
184+
const key = await importKey(rawKey);
185+
const {
186+
encryptedMessage,
187+
iv
188+
} = await encryptMessage(sitepasswd, key);
194189

195-
// update the sites list
196-
sites("save");
197-
}
198-
// show any server status messages
199-
serverStatus(result);
190+
// send the data, along with the encrypted password, to the server
191+
const result = await serverRequest("save", {
192+
"site": site,
193+
"siteuser": siteuser,
194+
"sitepasswd": encryptedMessage,
195+
"siteiv": iv
200196
});
197+
198+
if (result.response.ok) {
199+
// any work after a successful save should be done here
200+
siteInput.value = '';
201+
userInput.value = '';
202+
passInput.value = '';
203+
204+
// update the sites list
205+
sites("save");
206+
}
207+
// show any server status messages
208+
serverStatus(result);
201209
}
202210

203211
/**
@@ -337,3 +345,24 @@ async function hashMessage(message) {
337345

338346
return bufferToHexString(digestValue);
339347
}
348+
349+
async function encryptMessage(message, key) {
350+
const data = utf8ToUint8Array(message);
351+
const iv = window.crypto.getRandomValues(new Uint8Array(12));
352+
const encryptedData = await window.crypto.subtle.encrypt({
353+
name: 'AES-GCM',
354+
iv: iv
355+
}, key, data);
356+
357+
return {
358+
encryptedMessage: bufferToHexString(encryptedData),
359+
iv: bufferToHexString(iv)
360+
};
361+
}
362+
363+
async function importKey(rawKey) {
364+
const rawKeyBuffer = hexStringToUint8Array(rawKey);
365+
const key = await window.crypto.subtle.importKey('raw', rawKeyBuffer, 'AES-GCM', false, ['encrypt', 'decrypt']);
366+
367+
return key;
368+
}

0 commit comments

Comments
 (0)