From f68ffe1581d697338b9442a9da1a6a1f302e2124 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 10 Dec 2017 15:45:17 -0500 Subject: [PATCH] fixes for dates in sync --- migrations/0050__string_dates.sql | 2 +- routes/api/login.js | 14 +++++++++----- services/sync.js | 5 ++--- services/sync_update.js | 8 ++++---- services/utils.js | 17 +++-------------- 5 files changed, 19 insertions(+), 27 deletions(-) diff --git a/migrations/0050__string_dates.sql b/migrations/0050__string_dates.sql index 6750f6895..499c6c306 100644 --- a/migrations/0050__string_dates.sql +++ b/migrations/0050__string_dates.sql @@ -119,7 +119,7 @@ CREATE TABLE `source_ids` ( DROP TABLE recent_notes; CREATE TABLE `recent_notes` ( - 'note_tree_id'TEXT NOT NULL PRIMARY KEY, + `note_tree_id` TEXT NOT NULL PRIMARY KEY, `note_path` TEXT NOT NULL, `date_accessed` TEXT NOT NULL, is_deleted INT diff --git a/routes/api/login.js b/routes/api/login.js index 39565f3f9..2efb9cf46 100644 --- a/routes/api/login.js +++ b/routes/api/login.js @@ -11,11 +11,15 @@ const protected_session = require('../../services/protected_session'); const app_info = require('../../services/app_info'); router.post('/sync', async (req, res, next) => { - const timestamp = req.body.timestamp; + const timestampStr = req.body.timestamp; - const now = utils.nowTimestamp(); + console.log(req.body); - if (Math.abs(timestamp - now) > 5) { + const timestamp = utils.parseDate(timestampStr); + + const now = new Date(); + + if (Math.abs(timestamp.getTime() - now.getTime()) > 5000) { res.status(400); res.send({ message: 'Auth request time is out of sync' }); } @@ -28,13 +32,13 @@ router.post('/sync', async (req, res, next) => { } const documentSecret = await options.getOption('document_secret'); - const expectedHash = utils.hmac(documentSecret, timestamp); + const expectedHash = utils.hmac(documentSecret, timestampStr); const givenHash = req.body.hash; if (expectedHash !== givenHash) { res.status(400); - res.send({ message: "Hash doesn't match" }); + res.send({ message: "Sync login hash doesn't match" }); } req.session.loggedIn = true; diff --git a/services/sync.js b/services/sync.js index e7e5719e5..1d1e658ad 100644 --- a/services/sync.js +++ b/services/sync.js @@ -84,7 +84,7 @@ async function sync() { } async function login() { - const timestamp = utils.nowTimestamp(); + const timestamp = utils.nowDate(); const documentSecret = await options.getOption('document_secret'); const hash = utils.hmac(documentSecret, timestamp); @@ -135,8 +135,7 @@ async function pullSync(syncContext) { if (!resp) { log.error("Empty response to pull for " + sync.entity_name + ", id=" + sync.entity_id); } - - if (sync.entity_name === 'notes') { + else if (sync.entity_name === 'notes') { await syncUpdate.updateNote(resp.entity, syncContext.sourceId); } else if (sync.entity_name === 'notes_tree') { diff --git a/services/sync_update.js b/services/sync_update.js index 5b486cd28..f73561445 100644 --- a/services/sync_update.js +++ b/services/sync_update.js @@ -20,7 +20,7 @@ async function updateNote(entity, sourceId) { log.info("Update/sync note " + entity.note_id); } else { - await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note , " + utils.formatTwoTimestamps(origNote.date_modified, entity.date_modified)); + await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note , " + utils.formatTwoDates(origNote.date_modified, entity.date_modified)); } } @@ -38,7 +38,7 @@ async function updateNoteTree(entity, sourceId) { log.info("Update/sync note tree " + entity.note_tree_id); } else { - await eventLog.addNoteEvent(entity.note_tree_id, "Sync conflict in note tree , " + utils.formatTwoTimestamps(orig.date_modified, entity.date_modified)); + await eventLog.addNoteEvent(entity.note_tree_id, "Sync conflict in note tree , " + utils.formatTwoDates(orig.date_modified, entity.date_modified)); } }); } @@ -55,7 +55,7 @@ async function updateNoteHistory(entity, sourceId) { log.info("Update/sync note history " + entity.note_history_id); } else { - await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note history for , " + utils.formatTwoTimestamps(orig.date_modified_to, entity.date_modified_to)); + await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note history for , " + utils.formatTwoDates(orig.date_modified_to, entity.date_modified_to)); } }); } @@ -86,7 +86,7 @@ async function updateOptions(entity, sourceId) { await eventLog.addEvent("Synced option " + entity.opt_name); } else { - await eventLog.addEvent("Sync conflict in options for " + entity.opt_name + ", " + utils.formatTwoTimestamps(orig.date_modified, entity.date_modified)); + await eventLog.addEvent("Sync conflict in options for " + entity.opt_name + ", " + utils.formatTwoDates(orig.date_modified, entity.date_modified)); } }); } diff --git a/services/utils.js b/services/utils.js index 4097aefe9..09f5190d2 100644 --- a/services/utils.js +++ b/services/utils.js @@ -23,10 +23,6 @@ function randomSecureToken(bytes = 32) { return crypto.randomBytes(bytes).toString('base64'); } -function nowTimestamp() { - return Math.floor(Date.now() / 1000); -} - function nowDate() { return dateStr(new Date()); } @@ -66,14 +62,8 @@ function isElectron() { return !!process.versions['electron']; } -function formatDateTimeFromTS(timestamp) { - const date = new Date(timestamp * 1000); - - return date.toISOString(); -} - -function formatTwoTimestamps(origTS, newTS) { - return "orig: " + formatDateTimeFromTS(origTS) + ", new: " + formatDateTimeFromTS(newTS); +function formatTwoDates(origDate, newDate) { + return "orig: " + origDate + ", new: " + newDate; } function hash(text) { @@ -88,7 +78,6 @@ function isEmptyOrWhitespace(str) { module.exports = { randomSecureToken, randomString, - nowTimestamp, nowDate, dateStr, parseDate, @@ -99,7 +88,7 @@ module.exports = { fromBase64, hmac, isElectron, - formatTwoTimestamps, + formatTwoDates, hash, isEmptyOrWhitespace }; \ No newline at end of file