Notes/src/services/auth.ts

140 lines
3.9 KiB
TypeScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
import etapiTokenService from "./etapi_tokens.js";
import log from "./log.js";
import sqlInit from "./sql_init.js";
import utils from "./utils.js";
import passwordEncryptionService from "./encryption/password_encryption.js";
import config from "./config.js";
import passwordService from "./encryption/password.js";
2024-04-03 20:04:20 +03:00
import type { NextFunction, Request, Response } from 'express';
import { AppRequest } from '../routes/route-interface.js';
const noAuthentication = config.General && config.General.noAuthentication === true;
2017-10-25 22:39:21 -04:00
2024-04-03 20:04:20 +03:00
function checkAuth(req: AppRequest, res: Response, next: NextFunction) {
2020-06-20 12:31:38 +02:00
if (!sqlInit.isDbInitialized()) {
2017-12-03 22:29:23 -05:00
res.redirect("setup");
}
else if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) {
2022-01-12 19:32:23 +01: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
2023-06-30 11:18:34 +02:00
// currently, we're doing that for file upload because handling form data seems to be difficult
2024-04-03 20:04:20 +03:00
function checkApiAuthOrElectron(req: AppRequest, res: Response, next: NextFunction) {
if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) {
2021-04-05 22:37:12 +02:00
reject(req, res, "Logged in session not found");
2018-01-07 09:59:05 -05:00
}
else {
2017-10-15 16:32:49 -04:00
next();
}
}
2024-04-03 20:04:20 +03:00
function checkApiAuth(req: AppRequest, res: Response, next: NextFunction) {
if (!req.session.loggedIn && !noAuthentication) {
2021-04-05 22:37:12 +02:00
reject(req, res, "Logged in session not found");
}
else {
next();
}
}
2024-04-03 20:04:20 +03:00
function checkAppInitialized(req: AppRequest, res: Response, next: NextFunction) {
2020-06-20 12:31:38 +02:00
if (!sqlInit.isDbInitialized()) {
res.redirect("setup");
}
else {
next();
}
}
2024-04-03 20:04:20 +03:00
function checkPasswordSet(req: AppRequest, res: Response, next: NextFunction) {
if (!utils.isElectron() && !passwordService.isPasswordSet()) {
2021-12-29 23:37:12 +01:00
res.redirect("set-password");
2021-12-29 23:19:05 +01:00
} else {
next();
}
}
2024-04-03 20:04:20 +03:00
function checkPasswordNotSet(req: AppRequest, res: Response, next: NextFunction) {
2022-01-12 19:32:23 +01:00
if (!utils.isElectron() && passwordService.isPasswordSet()) {
res.redirect("login");
} else {
next();
}
}
2024-04-03 20:04:20 +03:00
function checkAppNotInitialized(req: AppRequest, res: Response, next: NextFunction) {
2020-06-20 12:31:38 +02:00
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();
}
}
2024-04-03 20:04:20 +03:00
function checkEtapiToken(req: AppRequest, res: Response, next: NextFunction) {
2022-01-10 17:09:20 +01:00
if (etapiTokenService.isValidAuthHeader(req.headers.authorization)) {
next();
}
else {
2022-01-10 17:09:20 +01:00
reject(req, res, "Token not found");
}
}
2024-04-03 19:22:49 +03:00
function reject(req: AppRequest, res: Response, message: string) {
2019-07-25 21:05:16 +02:00
log.info(`${req.method} ${req.path} rejected with 401 ${message}`);
res.setHeader("Content-Type", "text/plain")
.status(401)
.send(message);
2019-07-25 21:05:16 +02:00
}
2024-04-03 20:04:20 +03:00
function checkCredentials(req: AppRequest, res: Response, next: NextFunction) {
if (!sqlInit.isDbInitialized()) {
res.setHeader("Content-Type", "text/plain")
.status(400)
.send('Database is not initialized yet.');
return;
}
if (!passwordService.isPasswordSet()) {
res.setHeader("Content-Type", "text/plain")
.status(400)
.send('Password has not been set yet. Please set a password and repeat the action');
return;
}
const header = req.headers['trilium-cred'] || '';
2024-02-17 21:08:56 +02:00
const auth = Buffer.from(header, 'base64').toString();
const colonIndex = auth.indexOf(':');
const password = colonIndex === -1 ? "" : auth.substr(colonIndex + 1);
2021-12-29 23:37:12 +01:00
// username is ignored
2021-12-29 23:37:12 +01:00
if (!passwordEncryptionService.verifyPassword(password)) {
res.setHeader("Content-Type", "text/plain")
.status(401)
.send('Incorrect password');
}
else {
next();
}
}
export default {
2017-10-15 16:32:49 -04:00
checkAuth,
checkApiAuth,
checkAppInitialized,
2021-12-29 23:19:05 +01:00
checkPasswordSet,
2022-01-12 19:32:23 +01:00
checkPasswordNotSet,
2018-01-07 09:59:05 -05:00
checkAppNotInitialized,
checkApiAuthOrElectron,
2022-01-10 17:09:20 +01:00
checkEtapiToken,
checkCredentials
2020-06-20 12:31:38 +02:00
};