2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import utils from "../services/utils.js";
|
|
|
|
import optionService from "../services/options.js";
|
|
|
|
import myScryptService from "../services/encryption/my_scrypt.js";
|
|
|
|
import log from "../services/log.js";
|
|
|
|
import passwordService from "../services/encryption/password.js";
|
|
|
|
import assetPath from "../services/asset_path.js";
|
|
|
|
import appPath from "../services/app_path.js";
|
|
|
|
import ValidationError from "../errors/validation_error.js";
|
2024-04-07 14:22:01 +03:00
|
|
|
import { Request, Response } from 'express';
|
|
|
|
import { AppRequest } from './route-interface';
|
|
|
|
|
|
|
|
function loginPage(req: Request, res: Response) {
|
2022-10-26 23:50:54 +02:00
|
|
|
res.render('login', {
|
|
|
|
failedAuth: false,
|
2022-12-25 11:58:24 +01:00
|
|
|
assetPath: assetPath,
|
|
|
|
appPath: appPath
|
2022-10-26 23:50:54 +02:00
|
|
|
});
|
2018-03-30 19:31:22 -04:00
|
|
|
}
|
2017-10-15 16:32:49 -04:00
|
|
|
|
2024-04-07 14:22:01 +03:00
|
|
|
function setPasswordPage(req: Request, res: Response) {
|
2022-10-26 23:50:54 +02:00
|
|
|
res.render('set_password', {
|
|
|
|
error: false,
|
2022-12-25 11:58:24 +01:00
|
|
|
assetPath: assetPath,
|
|
|
|
appPath: appPath
|
2022-10-26 23:50:54 +02:00
|
|
|
});
|
2021-12-29 23:19:05 +01:00
|
|
|
}
|
|
|
|
|
2024-04-07 14:22:01 +03:00
|
|
|
function setPassword(req: Request, res: Response) {
|
2021-12-30 22:54:08 +01:00
|
|
|
if (passwordService.isPasswordSet()) {
|
2022-12-09 16:04:13 +01:00
|
|
|
throw new ValidationError("Password has been already set");
|
2021-12-29 23:37:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let {password1, password2} = req.body;
|
|
|
|
password1 = password1.trim();
|
|
|
|
password2 = password2.trim();
|
|
|
|
|
|
|
|
let error;
|
|
|
|
|
|
|
|
if (password1 !== password2) {
|
|
|
|
error = "Entered passwords don't match.";
|
|
|
|
} else if (password1.length < 4) {
|
|
|
|
error = "Password must be at least 4 characters long.";
|
|
|
|
}
|
2017-10-15 16:32:49 -04:00
|
|
|
|
2021-12-29 23:37:12 +01:00
|
|
|
if (error) {
|
2022-10-26 23:50:54 +02:00
|
|
|
res.render('set_password', {
|
|
|
|
error,
|
|
|
|
assetPath: assetPath
|
|
|
|
});
|
2021-12-29 23:37:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-30 22:54:08 +01:00
|
|
|
passwordService.setPassword(password1);
|
2021-12-29 23:37:12 +01:00
|
|
|
|
|
|
|
res.redirect('login');
|
|
|
|
}
|
|
|
|
|
2024-04-07 14:22:01 +03:00
|
|
|
function login(req: AppRequest, res: Response) {
|
2017-10-15 16:32:49 -04:00
|
|
|
const guessedPassword = req.body.password;
|
|
|
|
|
2021-12-29 23:37:12 +01:00
|
|
|
if (verifyPassword(guessedPassword)) {
|
2023-06-29 23:32:19 +02:00
|
|
|
const rememberMe = req.body.rememberMe;
|
2017-10-15 16:32:49 -04:00
|
|
|
|
2017-10-15 20:16:30 -04:00
|
|
|
req.session.regenerate(() => {
|
2017-10-16 19:14:15 -04:00
|
|
|
if (rememberMe) {
|
|
|
|
req.session.cookie.maxAge = 21 * 24 * 3600000; // 3 weeks
|
|
|
|
} else {
|
2024-04-07 16:56:45 +03:00
|
|
|
req.session.cookie.expires = null;
|
2017-10-16 19:14:15 -04:00
|
|
|
}
|
|
|
|
|
2017-10-15 20:16:30 -04:00
|
|
|
req.session.loggedIn = true;
|
2019-05-22 21:25:13 +02:00
|
|
|
res.redirect('.');
|
2017-10-15 20:16:30 -04:00
|
|
|
});
|
2017-10-15 16:32:49 -04:00
|
|
|
}
|
|
|
|
else {
|
2021-04-19 21:41:29 +02:00
|
|
|
// note that logged IP address is usually meaningless since the traffic should come from a reverse proxy
|
2021-12-29 23:37:12 +01:00
|
|
|
log.info(`WARNING: Wrong password from ${req.ip}, rejecting.`);
|
2021-04-19 21:41:29 +02:00
|
|
|
|
2023-04-23 22:22:05 +02:00
|
|
|
res.status(401).render('login', {
|
2022-10-26 23:50:54 +02:00
|
|
|
failedAuth: true,
|
|
|
|
assetPath: assetPath
|
|
|
|
});
|
2017-10-15 16:32:49 -04:00
|
|
|
}
|
2018-03-30 19:31:22 -04:00
|
|
|
}
|
2017-10-15 16:32:49 -04:00
|
|
|
|
2024-04-07 14:22:01 +03:00
|
|
|
function verifyPassword(guessedPassword: string) {
|
2020-06-20 12:31:38 +02:00
|
|
|
const hashed_password = utils.fromBase64(optionService.getOption('passwordVerificationHash'));
|
2017-10-15 16:32:49 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const guess_hashed = myScryptService.getVerificationHash(guessedPassword);
|
2017-10-15 16:32:49 -04:00
|
|
|
|
|
|
|
return guess_hashed.equals(hashed_password);
|
|
|
|
}
|
|
|
|
|
2024-04-07 14:22:01 +03:00
|
|
|
function logout(req: AppRequest, res: Response) {
|
2018-03-30 19:31:22 -04:00
|
|
|
req.session.regenerate(() => {
|
|
|
|
req.session.loggedIn = false;
|
|
|
|
|
2019-04-11 22:04:36 +02:00
|
|
|
res.redirect('login');
|
2018-03-30 19:31:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2018-03-30 19:31:22 -04:00
|
|
|
loginPage,
|
2021-12-29 23:19:05 +01:00
|
|
|
setPasswordPage,
|
2021-12-29 23:37:12 +01:00
|
|
|
setPassword,
|
2018-03-30 19:31:22 -04:00
|
|
|
login,
|
|
|
|
logout
|
|
|
|
};
|