2017-06-02 19:38:05 +00:00
|
|
|
const EventEmitter = require('events');
|
2017-06-28 18:30:14 +00:00
|
|
|
const { hexToArray } = require('./utils');
|
2017-06-02 19:38:05 +00:00
|
|
|
|
|
|
|
class FileReceiver extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
download() {
|
|
|
|
return Promise.all([
|
|
|
|
new Promise((resolve, reject) => {
|
2017-06-09 17:44:12 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-06-02 19:49:56 +00:00
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
xhr.onprogress = event => {
|
2017-07-11 18:18:31 +00:00
|
|
|
if (event.lengthComputable && event.target.status !== 404) {
|
2017-07-13 14:05:45 +00:00
|
|
|
this.emit('progress', [event.loaded, event.total]);
|
2017-06-02 19:38:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-09 23:56:25 +00:00
|
|
|
xhr.onload = function(event) {
|
2017-06-02 21:11:54 +00:00
|
|
|
if (xhr.status === 404) {
|
2017-06-06 21:24:51 +00:00
|
|
|
reject(
|
|
|
|
new Error('The file has expired, or has already been deleted.')
|
|
|
|
);
|
2017-06-02 21:54:50 +00:00
|
|
|
return;
|
2017-06-02 21:11:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
const blob = new Blob([this.response]);
|
|
|
|
const fileReader = new FileReader();
|
2017-06-02 19:38:05 +00:00
|
|
|
fileReader.onload = function() {
|
2017-06-29 17:30:08 +00:00
|
|
|
const meta = JSON.parse(xhr.getResponseHeader('X-File-Metadata'));
|
2017-06-02 19:38:05 +00:00
|
|
|
resolve({
|
|
|
|
data: this.result,
|
2017-06-29 17:27:36 +00:00
|
|
|
aad: meta.aad,
|
2017-07-07 21:47:56 +00:00
|
|
|
filename: meta.filename,
|
2017-07-10 19:45:20 +00:00
|
|
|
iv: meta.id
|
2017-06-02 19:38:05 +00:00
|
|
|
});
|
2017-06-02 19:49:56 +00:00
|
|
|
};
|
2017-06-02 19:38:05 +00:00
|
|
|
|
|
|
|
fileReader.readAsArrayBuffer(blob);
|
2017-06-02 19:49:56 +00:00
|
|
|
};
|
|
|
|
|
2017-06-02 19:38:05 +00:00
|
|
|
xhr.open('get', '/assets' + location.pathname.slice(0, -1), true);
|
|
|
|
xhr.responseType = 'blob';
|
|
|
|
xhr.send();
|
|
|
|
}),
|
2017-06-02 19:49:56 +00:00
|
|
|
window.crypto.subtle.importKey(
|
|
|
|
'jwk',
|
|
|
|
{
|
|
|
|
kty: 'oct',
|
|
|
|
k: location.hash.slice(1),
|
2017-06-28 18:30:14 +00:00
|
|
|
alg: 'A128GCM',
|
2017-06-02 19:49:56 +00:00
|
|
|
ext: true
|
|
|
|
},
|
|
|
|
{
|
2017-06-20 20:03:04 +00:00
|
|
|
name: 'AES-GCM'
|
2017-06-02 19:49:56 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
)
|
2017-06-20 20:03:04 +00:00
|
|
|
]).then(([fdata, key]) => {
|
2017-07-11 20:30:25 +00:00
|
|
|
this.emit('decrypting', true);
|
2017-06-20 20:03:04 +00:00
|
|
|
return Promise.all([
|
|
|
|
window.crypto.subtle.decrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
2017-07-07 21:47:56 +00:00
|
|
|
iv: hexToArray(fdata.iv),
|
2017-07-10 18:25:03 +00:00
|
|
|
additionalData: hexToArray(fdata.aad)
|
2017-06-20 20:03:04 +00:00
|
|
|
},
|
|
|
|
key,
|
|
|
|
fdata.data
|
2017-07-11 20:30:25 +00:00
|
|
|
).then(decrypted => {
|
|
|
|
this.emit('decrypting', false);
|
2017-07-17 17:52:27 +00:00
|
|
|
return Promise.resolve(decrypted)
|
2017-07-11 20:30:25 +00:00
|
|
|
}),
|
2017-07-17 17:38:19 +00:00
|
|
|
fdata.filename,
|
|
|
|
hexToArray(fdata.aad)
|
2017-06-20 20:03:04 +00:00
|
|
|
]);
|
2017-07-10 18:25:03 +00:00
|
|
|
}).then(([decrypted, fname, proposedHash]) => {
|
2017-07-11 20:30:25 +00:00
|
|
|
this.emit('hashing', true);
|
2017-07-10 18:25:03 +00:00
|
|
|
return window.crypto.subtle.digest('SHA-256', decrypted).then(calculatedHash => {
|
2017-07-11 20:30:25 +00:00
|
|
|
this.emit('hashing', false);
|
2017-07-10 18:25:03 +00:00
|
|
|
const integrity = new Uint8Array(calculatedHash).toString() === proposedHash.toString();
|
|
|
|
if (!integrity) {
|
2017-07-13 16:53:59 +00:00
|
|
|
this.emit('unsafe', true)
|
2017-07-13 21:47:53 +00:00
|
|
|
return Promise.reject();
|
2017-07-13 16:53:59 +00:00
|
|
|
} else {
|
|
|
|
this.emit('safe', true);
|
|
|
|
return Promise.all([
|
2017-07-17 17:38:19 +00:00
|
|
|
decrypted,
|
2017-07-17 23:18:14 +00:00
|
|
|
decodeURIComponent(fname)
|
2017-07-13 16:53:59 +00:00
|
|
|
]);
|
2017-07-10 18:25:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-06-02 19:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 19:49:56 +00:00
|
|
|
module.exports = FileReceiver;
|