trilium/routes/api/login.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
2017-10-29 10:17:00 +08:00
const utils = require('../../services/utils');
2017-10-30 02:55:48 +08:00
const migration = require('../../services/migration');
router.post('', async (req, res, next) => {
2017-10-29 10:17:00 +08:00
const timestamp = req.body.timestamp;
2017-10-29 10:17:00 +08:00
const now = utils.nowTimestamp();
2017-10-29 10:17:00 +08:00
if (Math.abs(timestamp - now) > 5) {
res.status(400);
res.send({ message: 'Auth request time is out of sync' });
}
2017-10-30 02:55:48 +08:00
const dbVersion = req.body.dbVersion;
2017-10-29 10:17:00 +08:00
if (dbVersion !== migration.APP_DB_VERSION) {
res.status(400);
res.send({ message: 'Non-matching db versions, local is version ' + migration.APP_DB_VERSION });
}
const documentSecret = await sql.getOption('document_secret');
2017-10-30 02:55:48 +08:00
const expectedHash = utils.hmac(documentSecret, timestamp);
2017-10-29 10:17:00 +08:00
const givenHash = req.body.hash;
if (expectedHash !== givenHash) {
res.status(400);
res.send({ message: "Hash doesn't match" });
}
req.session.loggedIn = true;
res.send({});
});
module.exports = router;