2017-08-24 23:54:02 +02:00
|
|
|
import Nanobus from 'nanobus';
|
2017-08-31 18:43:36 +02:00
|
|
|
import { arrayToB64, b64ToArray, bytes } from './utils';
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
export default class FileSender extends Nanobus {
|
2017-06-02 05:59:27 +02:00
|
|
|
constructor(file) {
|
2017-08-24 23:54:02 +02:00
|
|
|
super('FileSender');
|
2017-06-02 05:59:27 +02:00
|
|
|
this.file = file;
|
2017-08-24 23:54:02 +02:00
|
|
|
this.msg = 'importingFile';
|
|
|
|
this.progress = [0, 1];
|
|
|
|
this.cancelled = false;
|
2017-06-20 22:03:04 +02:00
|
|
|
this.iv = window.crypto.getRandomValues(new Uint8Array(12));
|
2017-07-18 19:52:32 +02:00
|
|
|
this.uploadXHR = new XMLHttpRequest();
|
2017-08-31 18:43:36 +02:00
|
|
|
this.rawSecret = window.crypto.getRandomValues(new Uint8Array(16));
|
|
|
|
this.secretKey = window.crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
this.rawSecret,
|
|
|
|
'HKDF',
|
|
|
|
false,
|
|
|
|
['deriveKey']
|
2017-08-14 21:04:03 +02:00
|
|
|
);
|
2017-06-02 05:59:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
static delete(id, token) {
|
2017-06-02 05:59:27 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-24 23:54:02 +02:00
|
|
|
if (!id || !token) {
|
2017-06-09 15:45:06 +02:00
|
|
|
return reject();
|
2017-06-02 05:59:27 +02:00
|
|
|
}
|
2017-06-09 19:44:12 +02:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-08-24 23:54:02 +02:00
|
|
|
xhr.open('POST', `/api/delete/${id}`);
|
2017-06-02 05:59:27 +02:00
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-30 22:41:09 +01:00
|
|
|
xhr.send(JSON.stringify({ owner_token: token }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static changeLimit(id, owner_token, dlimit) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!id || !owner_token) {
|
|
|
|
return reject();
|
|
|
|
}
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('POST', `/api/params/${id}`);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(JSON.stringify({ owner_token, dlimit }));
|
2017-06-02 05:59:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
get progressRatio() {
|
|
|
|
return this.progress[0] / this.progress[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
get sizes() {
|
|
|
|
return {
|
|
|
|
partialSize: bytes(this.progress[0]),
|
|
|
|
totalSize: bytes(this.progress[1])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-18 19:52:32 +02:00
|
|
|
cancel() {
|
2017-08-24 23:54:02 +02:00
|
|
|
this.cancelled = true;
|
|
|
|
if (this.msg === 'fileSizeProgress') {
|
|
|
|
this.uploadXHR.abort();
|
|
|
|
}
|
2017-07-18 19:52:32 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:04:03 +02:00
|
|
|
readFile() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(this.file);
|
|
|
|
reader.onload = function(event) {
|
|
|
|
const plaintext = new Uint8Array(this.result);
|
|
|
|
resolve(plaintext);
|
|
|
|
};
|
|
|
|
reader.onerror = function(err) {
|
|
|
|
reject(err);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-08-31 18:43:36 +02:00
|
|
|
uploadFile(encrypted, metadata, rawAuth) {
|
2017-08-14 21:04:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const dataView = new DataView(encrypted);
|
2017-08-31 18:43:36 +02:00
|
|
|
const blob = new Blob([dataView], { type: 'application/octet-stream' });
|
2017-08-14 21:04:03 +02:00
|
|
|
const fd = new FormData();
|
2017-08-31 18:43:36 +02:00
|
|
|
fd.append('data', blob);
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-08-14 21:04:03 +02:00
|
|
|
const xhr = this.uploadXHR;
|
2017-06-02 21:49:56 +02:00
|
|
|
|
2017-08-14 21:04:03 +02:00
|
|
|
xhr.upload.addEventListener('progress', e => {
|
|
|
|
if (e.lengthComputable) {
|
2017-08-24 23:54:02 +02:00
|
|
|
this.progress = [e.loaded, e.total];
|
|
|
|
this.emit('progress', this.progress);
|
2017-08-14 21:04:03 +02:00
|
|
|
}
|
2017-06-02 05:59:27 +02:00
|
|
|
});
|
2017-08-14 21:04:03 +02:00
|
|
|
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (xhr.status === 200) {
|
2017-08-31 18:43:36 +02:00
|
|
|
const nonce = xhr
|
|
|
|
.getResponseHeader('WWW-Authenticate')
|
|
|
|
.split(' ')[1];
|
2017-08-24 23:54:02 +02:00
|
|
|
this.progress = [1, 1];
|
|
|
|
this.msg = 'notifyUploadDone';
|
2017-08-14 21:04:03 +02:00
|
|
|
const responseObj = JSON.parse(xhr.responseText);
|
|
|
|
return resolve({
|
|
|
|
url: responseObj.url,
|
2017-08-24 23:54:02 +02:00
|
|
|
id: responseObj.id,
|
2017-08-31 18:43:36 +02:00
|
|
|
secretKey: arrayToB64(this.rawSecret),
|
2017-11-30 22:41:09 +01:00
|
|
|
ownerToken: responseObj.owner,
|
2017-08-31 18:43:36 +02:00
|
|
|
nonce
|
2017-08-14 21:04:03 +02:00
|
|
|
});
|
|
|
|
}
|
2017-08-24 23:54:02 +02:00
|
|
|
this.msg = 'errorPageHeader';
|
|
|
|
reject(new Error(xhr.status));
|
2017-08-14 21:04:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
xhr.open('post', '/api/upload', true);
|
2017-08-14 21:04:03 +02:00
|
|
|
xhr.setRequestHeader(
|
|
|
|
'X-File-Metadata',
|
2017-08-31 18:43:36 +02:00
|
|
|
arrayToB64(new Uint8Array(metadata))
|
2017-08-14 21:04:03 +02:00
|
|
|
);
|
2017-08-31 18:43:36 +02:00
|
|
|
xhr.setRequestHeader('Authorization', `send-v1 ${arrayToB64(rawAuth)}`);
|
2017-08-14 21:04:03 +02:00
|
|
|
xhr.send(fd);
|
2017-08-24 23:54:02 +02:00
|
|
|
this.msg = 'fileSizeProgress';
|
2017-08-14 21:04:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async upload() {
|
2017-08-31 18:43:36 +02:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const secretKey = await this.secretKey;
|
|
|
|
const encryptKey = await window.crypto.subtle.deriveKey(
|
|
|
|
{
|
|
|
|
name: 'HKDF',
|
|
|
|
salt: new Uint8Array(),
|
|
|
|
info: encoder.encode('encryption'),
|
|
|
|
hash: 'SHA-256'
|
|
|
|
},
|
|
|
|
secretKey,
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
length: 128
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
['encrypt']
|
|
|
|
);
|
|
|
|
const authKey = await window.crypto.subtle.deriveKey(
|
|
|
|
{
|
|
|
|
name: 'HKDF',
|
|
|
|
salt: new Uint8Array(),
|
|
|
|
info: encoder.encode('authentication'),
|
|
|
|
hash: 'SHA-256'
|
|
|
|
},
|
|
|
|
secretKey,
|
|
|
|
{
|
|
|
|
name: 'HMAC',
|
|
|
|
hash: 'SHA-256'
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['sign']
|
|
|
|
);
|
|
|
|
const metaKey = await window.crypto.subtle.deriveKey(
|
|
|
|
{
|
|
|
|
name: 'HKDF',
|
|
|
|
salt: new Uint8Array(),
|
|
|
|
info: encoder.encode('metadata'),
|
|
|
|
hash: 'SHA-256'
|
|
|
|
},
|
|
|
|
secretKey,
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
length: 128
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
['encrypt']
|
|
|
|
);
|
2017-08-14 21:04:03 +02:00
|
|
|
const plaintext = await this.readFile();
|
2017-08-24 23:54:02 +02:00
|
|
|
if (this.cancelled) {
|
|
|
|
throw new Error(0);
|
|
|
|
}
|
|
|
|
this.msg = 'encryptingFile';
|
2017-08-14 21:04:03 +02:00
|
|
|
this.emit('encrypting');
|
|
|
|
const encrypted = await window.crypto.subtle.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv: this.iv,
|
|
|
|
tagLength: 128
|
|
|
|
},
|
2017-08-31 18:43:36 +02:00
|
|
|
encryptKey,
|
2017-08-14 21:04:03 +02:00
|
|
|
plaintext
|
|
|
|
);
|
2017-08-31 18:43:36 +02:00
|
|
|
const metadata = await window.crypto.subtle.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv: new Uint8Array(12),
|
|
|
|
tagLength: 128
|
|
|
|
},
|
|
|
|
metaKey,
|
|
|
|
encoder.encode(
|
|
|
|
JSON.stringify({
|
|
|
|
iv: arrayToB64(this.iv),
|
|
|
|
name: this.file.name,
|
2017-10-27 23:41:20 +02:00
|
|
|
type: this.file.type || 'application/octet-stream'
|
2017-08-31 18:43:36 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const rawAuth = await window.crypto.subtle.exportKey('raw', authKey);
|
2017-08-24 23:54:02 +02:00
|
|
|
if (this.cancelled) {
|
|
|
|
throw new Error(0);
|
|
|
|
}
|
2017-08-31 18:43:36 +02:00
|
|
|
return this.uploadFile(encrypted, metadata, new Uint8Array(rawAuth));
|
|
|
|
}
|
|
|
|
|
2017-11-30 22:41:09 +01:00
|
|
|
async getAuthHeader(authKey, nonce) {
|
|
|
|
const sig = await window.crypto.subtle.sign(
|
|
|
|
{
|
|
|
|
name: 'HMAC'
|
|
|
|
},
|
|
|
|
authKey,
|
|
|
|
b64ToArray(nonce)
|
|
|
|
);
|
|
|
|
return `send-v1 ${arrayToB64(new Uint8Array(sig))}`;
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:43:36 +02:00
|
|
|
static async setPassword(password, file) {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const secretKey = await window.crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
b64ToArray(file.secretKey),
|
|
|
|
'HKDF',
|
|
|
|
false,
|
|
|
|
['deriveKey']
|
|
|
|
);
|
|
|
|
const authKey = await window.crypto.subtle.deriveKey(
|
|
|
|
{
|
|
|
|
name: 'HKDF',
|
|
|
|
salt: new Uint8Array(),
|
|
|
|
info: encoder.encode('authentication'),
|
|
|
|
hash: 'SHA-256'
|
|
|
|
},
|
|
|
|
secretKey,
|
|
|
|
{
|
|
|
|
name: 'HMAC',
|
|
|
|
hash: 'SHA-256'
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['sign']
|
|
|
|
);
|
2017-11-30 22:41:09 +01:00
|
|
|
const authHeader = await this.getAuthHeader(authKey, file.nonce);
|
2017-08-31 18:43:36 +02:00
|
|
|
const pwdKey = await window.crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
encoder.encode(password),
|
|
|
|
{ name: 'PBKDF2' },
|
|
|
|
false,
|
|
|
|
['deriveKey']
|
|
|
|
);
|
|
|
|
const newAuthKey = await window.crypto.subtle.deriveKey(
|
|
|
|
{
|
|
|
|
name: 'PBKDF2',
|
|
|
|
salt: encoder.encode(file.url),
|
|
|
|
iterations: 100,
|
|
|
|
hash: 'SHA-256'
|
|
|
|
},
|
|
|
|
pwdKey,
|
|
|
|
{
|
|
|
|
name: 'HMAC',
|
|
|
|
hash: 'SHA-256'
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['sign']
|
|
|
|
);
|
|
|
|
const rawAuth = await window.crypto.subtle.exportKey('raw', newAuthKey);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
return resolve(xhr.response);
|
|
|
|
}
|
|
|
|
if (xhr.status === 401) {
|
|
|
|
const nonce = xhr
|
|
|
|
.getResponseHeader('WWW-Authenticate')
|
|
|
|
.split(' ')[1];
|
|
|
|
file.nonce = nonce;
|
|
|
|
}
|
|
|
|
reject(new Error(xhr.status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = () => reject(new Error(0));
|
|
|
|
xhr.ontimeout = () => reject(new Error(0));
|
|
|
|
xhr.open('post', `/api/password/${file.id}`);
|
2017-11-30 22:41:09 +01:00
|
|
|
xhr.setRequestHeader('Authorization', authHeader);
|
2017-08-31 18:43:36 +02:00
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
xhr.responseType = 'json';
|
|
|
|
xhr.timeout = 2000;
|
|
|
|
xhr.send(JSON.stringify({ auth: arrayToB64(new Uint8Array(rawAuth)) }));
|
|
|
|
});
|
2017-06-02 05:59:27 +02:00
|
|
|
}
|
|
|
|
}
|