Notes/src/services/auth.js

104 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
const sql = require('./sql');
2019-07-25 21:05:16 +02:00
const log = require('./log');
const sqlInit = require('./sql_init');
const utils = require('./utils');
const passwordEncryptionService = require('./password_encryption');
const optionService = require('./options');
const config = require('./config');
const noAuthentication = config.General && config.General.noAuthentication === true;
2017-10-25 22:39:21 -04:00
2020-06-20 12:31:38 +02:00
function checkAuth(req, res, next) {
if (!sqlInit.isDbInitialized()) {
2017-12-03 22:29:23 -05:00
res.redirect("setup");
}
else if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) {
2017-10-15 16:32:49 -04:00
res.redirect("login");
2017-10-25 22:39:21 -04:00
}
else {
next();
}
}
2018-01-07 09:59:05 -05:00
// for electron things which need network stuff
// currently we're doing that for file upload because handling form data seems to be difficult
2020-06-20 12:31:38 +02:00
function checkApiAuthOrElectron(req, res, next) {
if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) {
2019-07-25 21:05:16 +02:00
reject(req, res, "Not authorized");
2018-01-07 09:59:05 -05:00
}
else {
2017-10-15 16:32:49 -04:00
next();
}
}
2020-06-20 12:31:38 +02:00
function checkApiAuth(req, res, next) {
if (!req.session.loggedIn && !noAuthentication) {
2019-07-25 21:05:16 +02:00
reject(req, res, "Not authorized");
}
else {
next();
}
}
2020-06-20 12:31:38 +02:00
function checkAppInitialized(req, res, next) {
if (!sqlInit.isDbInitialized()) {
res.redirect("setup");
}
else {
next();
}
}
2020-06-20 12:31:38 +02:00
function checkAppNotInitialized(req, res, next) {
if (sqlInit.isDbInitialized()) {
2019-07-25 21:05:16 +02:00
reject(req, res, "App already initialized.");
2017-12-03 22:29:23 -05:00
}
else {
next();
}
}
2020-06-20 12:31:38 +02:00
function checkToken(req, res, next) {
const token = req.headers.authorization;
2020-06-20 12:31:38 +02:00
if (sql.getValue("SELECT COUNT(*) FROM api_tokens WHERE isDeleted = 0 AND token = ?", [token]) === 0) {
2019-07-25 21:05:16 +02:00
reject(req, res, "Not authorized");
}
else {
next();
}
}
2019-07-25 21:05:16 +02:00
function reject(req, res, message) {
log.info(`${req.method} ${req.path} rejected with 401 ${message}`);
res.status(401).send(message);
}
function checkCredentials(req, res, next) {
const header = req.headers['trilium-cred'] || '';
2021-02-07 21:50:34 +01:00
const auth = new Buffer.from(header, 'base64').toString();console.log("auth", auth);
const [username, password] = auth.split(/:/);
2020-06-20 12:31:38 +02:00
const dbUsername = optionService.getOption('username');
2020-06-20 12:31:38 +02:00
if (dbUsername !== username || !passwordEncryptionService.verifyPassword(password)) {
2019-12-28 12:55:53 +01:00
res.status(401).send('Incorrect username and/or password');
}
else {
next();
}
}
2017-10-15 16:32:49 -04:00
module.exports = {
checkAuth,
checkApiAuth,
checkAppInitialized,
2018-01-07 09:59:05 -05:00
checkAppNotInitialized,
checkApiAuthOrElectron,
checkToken,
checkCredentials
2020-06-20 12:31:38 +02:00
};