trilium/src/routes/login.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
2017-10-16 04:32:49 +08:00
const express = require('express');
const router = express.Router();
2017-10-16 07:47:05 +08:00
const utils = require('../services/utils');
2017-11-03 08:48:02 +08:00
const options = require('../services/options');
2017-10-16 07:47:05 +08:00
const my_scrypt = require('../services/my_scrypt');
const wrap = require('express-promise-wrap').wrap;
2017-10-16 04:32:49 +08:00
router.get('', wrap(async (req, res, next) => {
2017-10-16 04:32:49 +08:00
res.render('login', { 'failedAuth': false });
}));
2017-10-16 04:32:49 +08:00
router.post('', wrap(async (req, res, next) => {
2017-11-03 08:48:02 +08:00
const userName = await options.getOption('username');
2017-10-16 04:32:49 +08:00
const guessedPassword = req.body.password;
if (req.body.username === userName && await verifyPassword(guessedPassword)) {
2017-10-17 07:14:15 +08:00
const rememberMe = req.body.remember_me;
2017-10-16 04:32:49 +08:00
2017-10-16 08:16:30 +08:00
req.session.regenerate(() => {
2017-10-17 07:14:15 +08:00
if (rememberMe) {
req.session.cookie.maxAge = 21 * 24 * 3600000; // 3 weeks
} else {
req.session.cookie.expires = false;
}
2017-10-16 08:16:30 +08:00
req.session.loggedIn = true;
res.redirect('/');
});
2017-10-16 04:32:49 +08:00
}
else {
res.render('login', {'failedAuth': true});
}
}));
2017-10-16 04:32:49 +08:00
async function verifyPassword(guessed_password) {
2017-11-03 08:48:02 +08:00
const hashed_password = utils.fromBase64(await options.getOption('password_verification_hash'));
2017-10-16 04:32:49 +08:00
const guess_hashed = await my_scrypt.getVerificationHash(guessed_password);
return guess_hashed.equals(hashed_password);
}
module.exports = router;