diff --git a/routes/api/login.js b/routes/api/login.js index 69b242f83..69a433ec1 100644 --- a/routes/api/login.js +++ b/routes/api/login.js @@ -42,7 +42,7 @@ router.post('/sync', async (req, res, next) => { req.session.loggedIn = true; res.send({ - sourceId: source_id.currentSourceId + sourceId: await source_id.getCurrentSourceId() }); }); diff --git a/routes/api/notes.js b/routes/api/notes.js index 704caaf47..ab6a9994c 100644 --- a/routes/api/notes.js +++ b/routes/api/notes.js @@ -4,12 +4,10 @@ const express = require('express'); const router = express.Router(); const auth = require('../../services/auth'); const sql = require('../../services/sql'); -const utils = require('../../services/utils'); const notes = require('../../services/notes'); const log = require('../../services/log'); const protected_session = require('../../services/protected_session'); const data_encryption = require('../../services/data_encryption'); -const RequestContext = require('../../services/request_context'); router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { const noteId = req.params.noteId; @@ -50,9 +48,10 @@ router.post('/:parentNoteId/children', async (req, res, next) => { router.put('/:noteId', async (req, res, next) => { const note = req.body; const noteId = req.params.noteId; - const reqCtx = new RequestContext(req); + const sourceId = req.headers.source_id; + const dataKey = protected_session.getDataKey(req); - await notes.updateNote(noteId, note, reqCtx); + await notes.updateNote(noteId, note, dataKey, sourceId); res.send({}); }); diff --git a/services/notes.js b/services/notes.js index 521601320..dad86dca3 100644 --- a/services/notes.js +++ b/services/notes.js @@ -63,9 +63,9 @@ async function createNewNote(parentNoteId, note, sourceId) { }; } -async function encryptNote(note, ctx) { - note.detail.note_title = data_encryption.encrypt(ctx.getDataKey(), data_encryption.noteTitleIv(note.detail.note_id), note.detail.note_title); - note.detail.note_text = data_encryption.encrypt(ctx.getDataKey(), data_encryption.noteTextIv(note.detail.note_id), note.detail.note_text); +async function encryptNote(note, dataKey) { + note.detail.note_title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(note.detail.note_id), note.detail.note_title); + note.detail.note_text = data_encryption.encrypt(dataKey, data_encryption.noteTextIv(note.detail.note_id), note.detail.note_text); } async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { @@ -136,9 +136,9 @@ async function protectNoteHistory(noteId, dataKey, protect, sourceId) { } } -async function updateNote(noteId, newNote, ctx) { +async function updateNote(noteId, newNote, dataKey, sourceId) { if (newNote.detail.is_protected) { - await encryptNote(newNote, ctx); + await encryptNote(newNote, dataKey); } const now = new Date(); @@ -158,7 +158,7 @@ async function updateNote(noteId, newNote, ctx) { const oldNote = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); if (oldNote.is_protected) { - decryptNote(oldNote, ctx.getDataKey()); + decryptNote(oldNote, dataKey); } const newNoteHistoryId = utils.newNoteHistoryId(); @@ -174,10 +174,10 @@ async function updateNote(noteId, newNote, ctx) { date_modified_to: nowStr }); - await sync_table.addNoteHistorySync(newNoteHistoryId, ctx.sourceId); + await sync_table.addNoteHistorySync(newNoteHistoryId, sourceId); } - await protectNoteHistory(noteId, ctx.getDataKeyOrNull(), newNote.detail.is_protected); + await protectNoteHistory(noteId, dataKey, newNote.detail.is_protected); await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ?, date_modified = ? WHERE note_id = ?", [ newNote.detail.note_title, @@ -186,7 +186,7 @@ async function updateNote(noteId, newNote, ctx) { nowStr, noteId]); - await sync_table.addNoteSync(noteId, ctx.sourceId); + await sync_table.addNoteSync(noteId, sourceId); }); } diff --git a/services/request_context.js b/services/request_context.js deleted file mode 100644 index be0fb3e4f..000000000 --- a/services/request_context.js +++ /dev/null @@ -1,34 +0,0 @@ -"use strict"; - -const protected_session = require('./protected_session'); - -module.exports = function(req) { - const sourceId = req.headers.source_id; - - function isProtectedSessionAvailable() { - return protected_session.isProtectedSessionAvailable(req); - } - - function getDataKey() { - if (!isProtectedSessionAvailable()) { - throw new Error("Protected session is not available"); - } - - return protected_session.getDataKey(req); - } - - function getDataKeyOrNull() { - if (!isProtectedSessionAvailable()) { - return null; - } - - return protected_session.getDataKey(req); - } - - return { - sourceId, - isProtectedSessionAvailable, - getDataKey, - getDataKeyOrNull - }; -}; \ No newline at end of file diff --git a/services/source_id.js b/services/source_id.js index 0e74cd5e8..07c2a3410 100644 --- a/services/source_id.js +++ b/services/source_id.js @@ -31,10 +31,14 @@ function isLocalSourceId(srcId) { return allSourceIds.includes(srcId); } -const currentSourceId = generateSourceId(); +const currentSourceIdPromise = generateSourceId(); + +async function getCurrentSourceId() { + return await currentSourceIdPromise; +} module.exports = { generateSourceId, - currentSourceId, + getCurrentSourceId, isLocalSourceId }; \ No newline at end of file diff --git a/services/sync.js b/services/sync.js index f09e4147b..e4c20adb7 100644 --- a/services/sync.js +++ b/services/sync.js @@ -186,7 +186,7 @@ async function pushSync(syncContext) { log.info(`Skipping push #${sync.id} ${sync.entity_name} ${sync.entity_id} because it originates from sync target`); } else { - await readAndPushEntity(sync, syncContext); + await pushEntity(sync, syncContext); } lastSyncedPush = sync.id; @@ -195,7 +195,7 @@ async function pushSync(syncContext) { } } -async function readAndPushEntity(sync, syncContext) { +async function pushEntity(sync, syncContext) { let entity; if (sync.entity_name === 'notes') { @@ -230,16 +230,12 @@ async function readAndPushEntity(sync, syncContext) { log.info(`Pushing changes in sync #${sync.id} ${sync.entity_name} ${sync.entity_id}`); - await sendEntity(syncContext, entity, sync.entity_name); -} - -async function sendEntity(syncContext, entity, entityName) { const payload = { - sourceId: source_id.currentSourceId, + sourceId: await source_id.getCurrentSourceId(), entity: entity }; - await syncRequest(syncContext, 'PUT', '/api/sync/' + entityName, payload); + await syncRequest(syncContext, 'PUT', '/api/sync/' + sync.entity_name, payload); } async function checkContentHash(syncContext) { diff --git a/services/sync_table.js b/services/sync_table.js index 100ec4cc6..b642b08e0 100644 --- a/services/sync_table.js +++ b/services/sync_table.js @@ -32,7 +32,7 @@ async function addEntitySync(entityName, entityId, sourceId) { entity_name: entityName, entity_id: entityId, sync_date: utils.nowDate(), - source_id: sourceId || source_id.currentSourceId + source_id: sourceId || await source_id.getCurrentSourceId() }); if (!sync_setup.isSyncSetup) {