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

236 lines
5.8 KiB
JavaScript
Raw Normal View History

2020-12-05 13:19:41 +05:00
import { CHECK_IDS, EV, sendCheckUserStatusEvent } from "../common";
2020-03-19 11:30:05 +05:00
import getId from "../utils/id";
const ERASE_TIME = 1000 * 60 * 30;
export default class Vault {
/**
*
2020-04-16 02:36:09 +05:00
* @param {import('./index').default} db
*/
2020-04-16 02:36:09 +05:00
constructor(db) {
this._db = db;
this._context = db.context;
2020-03-08 11:33:55 +05:00
this._key = "Notesnook";
this._password = "";
this.ERRORS = {
noVault: "ERR_NO_VAULT",
vaultLocked: "ERR_VAULT_LOCKED",
wrongPassword: "ERR_WRONG_PASSWORD",
};
2020-11-02 09:50:27 +05:00
EV.subscribe("user:loggedOut", () => {
this._password = "";
});
}
2020-04-16 02:36:09 +05:00
/**
* Creates a new vault (replacing if any older exists)
* @param {string} password The password
* @returns {Promise<Boolean>}
2020-04-16 02:36:09 +05:00
*/
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;
this._startEraser();
}
return true;
}
2020-04-16 02:36:09 +05:00
/**
* Unlocks the vault with the given password
* @param {string} password The password
* @throws ERR_NO_VAULT | ERR_WRONG_PASSWORD
* @returns {Promise<Boolean>}
2020-04-16 02:36:09 +05:00
*/
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(this.ERRORS.noVault);
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;
this._startEraser();
return true;
}
async changePassword(oldPassword, newPassword) {
if (await this.unlock(oldPassword)) {
const lockedNotes = this._db.notes.all.filter((v) => v.locked);
for (var note of lockedNotes) {
await this._unlockNote(note, true);
}
await this._context.remove("vaultKey");
await this.create(newPassword);
for (var note of lockedNotes) {
await this._lockNote(note);
}
}
}
_startEraser() {
setTimeout(() => {
this._password = "";
}, ERASE_TIME);
}
2020-04-16 02:36:09 +05:00
/**
* Locks (add to vault) a note
* @param {string} noteId The id of the note to lock
*/
async add(noteId) {
2020-12-05 13:19:41 +05:00
if (!(await sendCheckUserStatusEvent(CHECK_IDS.vaultAdd))) return;
await this._check();
await this._lockNote({ id: noteId });
}
2020-04-16 02:36:09 +05:00
/**
* Permanently unlocks (remove from vault) a note
* @param {string} noteId The note id
* @param {string} password The password to unlock note with
*/
async remove(noteId, password) {
if (await this.unlock(password)) {
2020-04-16 02:36:09 +05:00
const note = this._db.notes.note(noteId).data;
2020-03-08 11:33:55 +05:00
await this._unlockNote(note, true);
}
}
async clear(password) {}
2020-04-16 02:36:09 +05:00
/**
* Temporarily unlock (open) a note
* @param {string} noteId The note id
* @param {string} password The password to open note with
*/
async open(noteId, password) {
if (await this.unlock(password)) {
2020-04-16 02:36:09 +05:00
const note = this._db.notes.note(noteId).data;
2020-03-08 11:33:55 +05:00
return this._unlockNote(note, false);
}
}
2020-04-16 02:36:09 +05:00
/**
* Saves a note into the vault
* @param {{Object}} note The note to save into the vault
*/
2020-03-08 11:33:55 +05:00
async save(note) {
if (!note) return;
//await this._check();
//let id = note.id || getId();
return await this._lockNote(note);
2020-03-08 11:33:55 +05:00
}
async exists(vaultKey) {
2020-04-16 02:36:09 +05:00
if (!vaultKey) vaultKey = await this._context.read("vaultKey");
return vaultKey && vaultKey.cipher && vaultKey.iv;
}
// Private & internal methods
2020-04-16 02:36:09 +05:00
/** @private */
_locked() {
2020-04-16 02:36:09 +05:00
return !this._password || !this._password.length;
}
/** @private */
async _check() {
if (!(await this.exists())) {
2020-04-16 02:36:09 +05:00
throw new Error(this.ERRORS.noVault);
}
if (this._locked()) {
2020-04-16 02:36:09 +05:00
throw new Error(this.ERRORS.vaultLocked);
}
}
/** @private */
async _encryptContent(contentId, content, type) {
let encryptedContent = await this._context.encrypt(
{ password: this._password },
JSON.stringify(content)
);
await this._db.content.add({ id: contentId, data: encryptedContent, type });
2020-03-08 11:33:55 +05:00
}
2020-04-16 02:36:09 +05:00
/** @private */
async _decryptContent(contentId) {
let encryptedContent = await this._db.content.raw(contentId);
2020-03-19 11:30:05 +05:00
let decryptedContent = await this._context.decrypt(
{ password: this._password },
encryptedContent.data
);
return { type: encryptedContent.type, data: JSON.parse(decryptedContent) };
2020-03-08 11:33:55 +05:00
}
2020-04-16 02:36:09 +05:00
/** @private */
async _lockNote(note) {
let { id, content: { type, data } = {}, contentId } = note;
if (!data || !type || !contentId) {
note = this._db.notes.note(id);
if (note.data.locked) return;
contentId = note.data.contentId;
let content = await this._db.content.raw(contentId);
data = content.data;
type = content.type;
}
2020-03-19 11:30:05 +05:00
await this._encryptContent(contentId, data, type);
2020-03-08 11:33:55 +05:00
return await this._db.notes.add({
id,
locked: true,
2020-04-15 17:16:13 +05:00
headline: "",
2020-12-23 13:19:17 +05:00
title: note.title,
2020-03-08 11:33:55 +05:00
});
}
2020-04-16 02:36:09 +05:00
/** @private */
2020-03-08 11:33:55 +05:00
async _unlockNote(note, perm = false) {
let content = await this._decryptContent(note.contentId);
2020-03-08 11:33:55 +05:00
if (perm) {
await this._db.notes.add({
id: note.id,
locked: false,
headline: note.headline,
contentId: note.contentId,
content,
2020-03-08 11:33:55 +05:00
});
// await this._db.content.add({ id: note.contentId, data: content });
2020-03-19 11:30:05 +05:00
return;
}
2020-03-08 11:33:55 +05:00
return {
...note,
content,
2020-03-08 11:33:55 +05:00
};
}
2020-04-16 02:36:09 +05:00
/** @inner */
async _getKey() {
if (await this.exists()) return await this._context.read("vaultKey");
2020-04-16 02:36:09 +05:00
}
/** @inner */
async _setKey(vaultKey) {
if (!vaultKey) return;
await this._context.write("vaultKey", vaultKey);
}
}