trilium/routes/api/login.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

"use strict";
const express = require('express');
const router = express.Router();
2017-11-03 08:48:02 +08:00
const options = require('../../services/options');
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');
const SOURCE_ID = require('../../services/source_id');
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 });
}
2017-11-03 08:48:02 +08:00
const documentSecret = await options.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({
sourceId: SOURCE_ID
});
});
module.exports = router;