Files
notesnook/packages/core/api/vault.js

178 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-03-19 11:30:05 +05:00
import getId from "../utils/id";
export default class Vault {
/**
*
* @param {import('./index').default} database
*/
constructor(database, context) {
2020-03-08 11:33:55 +05:00
this._db = database;
this._context = context;
this._key = "Notesnook";
this._password = "";
this.ERRORS = {
noVault: "ERR_NO_VAULT",
vaultLocked: "ERR_VAULT_LOCKED",
wrongPassword: "ERR_WRONG_PASSWORD",
};
}
2020-04-15 14:00:05 +05:00
async _getKey() {
if (await this._exists()) return await this._context.read("vaultKey");
}
async _setKey(vaultKey) {
if (!vaultKey) return;
await this._context.write("vaultKey", vaultKey);
}
async create(password) {
2020-04-15 14:00:05 +05:00
const vaultKey = await this._context.read("vaultKey");
if (!vaultKey || !vaultKey.cipher || !vaultKey.iv) {
const encryptedData = await this._context.encrypt(
{ password },
this._key
);
2020-04-15 14:00:05 +05:00
await this._context.write("vaultKey", encryptedData);
2020-03-08 11:33:55 +05:00
this._password = password;
}
return true;
}
async unlock(password) {
2020-04-15 14:00:05 +05:00
const vaultKey = await this._context.read("vaultKey");
if (!(await this._exists(vaultKey))) throw new Error("ERR_NO_VAULT");
var data;
try {
2020-04-15 14:00:05 +05:00
data = await this._context.decrypt({ password }, vaultKey);
} catch (e) {
2020-03-10 13:52:20 +05:00
throw new Error(this.ERRORS.wrongPassword);
}
2020-03-08 11:33:55 +05:00
if (data !== this._key) {
2020-03-10 13:52:20 +05:00
throw new Error(this.ERRORS.wrongPassword);
}
2020-03-08 11:33:55 +05:00
this._password = password;
return true;
}
2020-04-15 14:00:05 +05:00
async _exists(vaultKey) {
if (!vaultKey) vaultKey = await this._context.read("vaultKey");
return vaultKey && vaultKey.cipher && vaultKey.iv;
}
async _locked() {
2020-03-08 11:33:55 +05:00
return !this._password || !this._password.length;
}
async _check() {
if (!(await this._exists())) {
2020-03-10 13:52:20 +05:00
throw new Error(this.ERRORS.noVault);
}
if (await this._locked()) {
2020-03-10 13:52:20 +05:00
throw new Error(this.ERRORS.vaultLocked);
}
}
async add(id) {
await this._check();
//const note = this._db.notes.note(id).data;
await this._lockNote(id);
}
async remove(id, password) {
if (await this.unlock(password)) {
2020-03-08 11:33:55 +05:00
const note = this._db.notes.note(id).data;
await this._unlockNote(note, true);
}
}
async open(id, password) {
if (await this.unlock(password)) {
2020-03-08 11:33:55 +05:00
const note = this._db.notes.note(id).data;
return this._unlockNote(note, false);
}
}
async save(note) {
if (!note) return;
await this._check();
2020-03-19 11:30:05 +05:00
let id = note.id || getId();
2020-03-08 11:33:55 +05:00
return await this._lockNote(id, note);
}
2020-03-19 11:30:05 +05:00
async _encryptContent(content, ids) {
let { text, delta } = { ...content };
let { deltaId, textId } = ids;
2020-03-08 11:33:55 +05:00
2020-03-19 11:30:05 +05:00
if (!delta.ops) delta = await this._db.delta.get(deltaId);
if (text === textId) text = await this._db.text.get(textId);
2020-03-08 11:33:55 +05:00
text = await this._context.encrypt({ password: this._password }, text);
2020-04-15 16:45:40 +05:00
delta = await this._context.encrypt(
{ password: this._password },
JSON.stringify(delta)
);
2020-03-19 11:30:05 +05:00
await this._db.text.add({ id: textId, data: text });
await this._db.delta.add({ id: deltaId, data: delta });
2020-03-08 11:33:55 +05:00
}
2020-03-19 11:30:05 +05:00
async _decryptContent(content) {
let { text, delta } = { ...content };
text = await this._db.text.get(text);
text = await this._context.decrypt({ password: this._password }, text);
2020-03-19 11:30:05 +05:00
delta = await this._db.text.get(delta);
delta = await this._context.decrypt({ password: this._password }, delta);
2020-03-19 11:30:05 +05:00
if (typeof delta === "string") delta = JSON.parse(delta);
2020-03-19 11:30:05 +05:00
return {
delta,
text,
2020-03-19 11:30:05 +05:00
};
2020-03-08 11:33:55 +05:00
}
async _lockNote(id, note) {
note = note || this._db.notes.note(id).data;
2020-03-19 11:30:05 +05:00
let deltaId = 0;
let textId = 0;
if (note && note.content) {
deltaId = note.content.delta;
textId = note.content.text;
2020-03-19 11:30:05 +05:00
}
2020-03-08 11:33:55 +05:00
2020-03-19 11:30:05 +05:00
await this._encryptContent(note.content, { textId, deltaId });
2020-03-08 11:33:55 +05:00
return await this._db.notes.add({
id,
locked: true,
2020-03-08 11:33:55 +05:00
});
}
async _unlockNote(note, perm = false) {
if (!note.locked) return;
2020-03-19 11:30:05 +05:00
let { delta, text } = await this._decryptContent(note.content);
2020-03-08 11:33:55 +05:00
if (perm) {
await this._db.notes.add({
id: note.id,
locked: false,
2020-03-08 11:33:55 +05:00
});
2020-03-19 11:30:05 +05:00
await this._db.delta.add({ id: note.content.delta, data: delta });
await this._db.text.add({ id: note.content.text, data: text });
return;
}
2020-03-08 11:33:55 +05:00
return {
...note,
content: { delta, text },
2020-03-08 11:33:55 +05:00
};
}
}