2022-07-07 13:10:57 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
import auth, {BasicAuthCheckerCallback} from "http-auth";
|
|
|
|
import authConnect from "http-auth-connect";
|
|
|
|
import bodyParser from "body-parser";
|
|
|
|
import bcrypt from "bcrypt";
|
|
|
|
import compress from "compression";
|
|
|
|
import express, {Express, NextFunction, Request, Response} from "express";
|
|
|
|
import {promises as fs} from "graceful-fs";
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-04-09 20:18:13 +02:00
|
|
|
import {config} from "./config";
|
2022-07-07 13:10:57 +02:00
|
|
|
import type {CleartextPassword, PasswordHash, Username} from "./types";
|
|
|
|
import {isString} from "./types";
|
|
|
|
import Logger from "./logger";
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
export const app: Express = express();
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2022-07-07 13:10:57 +02:00
|
|
|
/**
|
|
|
|
* Used to have some password comparison in case the user does not exist to avoid timing attacks.
|
|
|
|
*/
|
|
|
|
const INVALID_PASSWORD_HASH = "$2b$05$JebmV1q/ySuxa89GoJYlc.6SEnj1OZYBOfTf.TYAehcC5HLeJiWPi";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trying to implement a timing safe string compare.
|
|
|
|
*
|
|
|
|
* TODO: Write tests for timing.
|
|
|
|
*/
|
|
|
|
function timingSafeEqual(a: string, b: string): boolean {
|
|
|
|
const lenA = a.length;
|
|
|
|
const lenB = b.length;
|
|
|
|
|
|
|
|
// Greater than 0 for differing strings.
|
|
|
|
let different = Math.abs(lenA - lenB);
|
|
|
|
|
|
|
|
// Make sure b is always the same length as a. Use slice to try avoiding optimizations.
|
|
|
|
b = different === 0 ? b.slice() : a.slice();
|
|
|
|
|
|
|
|
for (let i = 0; i < lenA; i += 1) {
|
|
|
|
different += Math.abs(a.charCodeAt(i) - b.charCodeAt(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return different === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function isValidLogin(username: Username, password: CleartextPassword): Promise<boolean> {
|
|
|
|
if (!config.server.internal.active) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let passwordHash: PasswordHash | undefined = undefined;
|
|
|
|
|
|
|
|
// Iterate over all users every time to reduce risk of timing attacks.
|
|
|
|
for (const userConfig of config.server.internal.users) {
|
|
|
|
if (timingSafeEqual(username, userConfig.username)) {
|
|
|
|
passwordHash = userConfig.passwordHash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always compare some password even if the user does not exist to reduce risk of timing attacks.
|
|
|
|
const isValidPassword = await bcrypt.compare(
|
|
|
|
password,
|
|
|
|
passwordHash || INVALID_PASSWORD_HASH
|
|
|
|
);
|
|
|
|
|
|
|
|
// Make sure password is only considered valid is user exists and therefor passwordHash is not undefined.
|
|
|
|
return isString(passwordHash) && isValidPassword;
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
export function init(): void {
|
|
|
|
const router = express.Router();
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
// urls beneath /internal are protected
|
|
|
|
const internalAuth = auth.basic(
|
|
|
|
{
|
|
|
|
realm: 'Knotenformular - Intern'
|
|
|
|
},
|
2022-07-07 13:10:57 +02:00
|
|
|
function (username: Username, password: CleartextPassword, callback: BasicAuthCheckerCallback): void {
|
|
|
|
isValidLogin(username, password)
|
|
|
|
.then(result => callback(result))
|
|
|
|
.catch(err => {
|
|
|
|
Logger.tag('login').error(err);
|
|
|
|
});
|
2020-06-30 01:10:18 +02:00
|
|
|
}
|
|
|
|
);
|
2020-06-30 17:44:41 +02:00
|
|
|
router.use('/internal', authConnect(internalAuth));
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
router.use(bodyParser.json());
|
2022-07-07 13:10:57 +02:00
|
|
|
router.use(bodyParser.urlencoded({extended: true}));
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
const adminDir = __dirname + '/../admin';
|
|
|
|
const clientDir = __dirname + '/../client';
|
|
|
|
const templateDir = __dirname + '/templates';
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
const jsTemplateFiles = [
|
|
|
|
'/config.js'
|
|
|
|
];
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
function usePromise(f: (req: Request, res: Response) => Promise<void>): void {
|
|
|
|
router.use((req: Request, res: Response, next: NextFunction): void => {
|
|
|
|
f(req, res).then(next).catch(next)
|
|
|
|
});
|
|
|
|
}
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
router.use(compress());
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2022-07-07 13:10:57 +02:00
|
|
|
async function serveTemplate(mimeType: string, req: Request, res: Response): Promise<void> {
|
2020-06-30 01:10:18 +02:00
|
|
|
const body = await fs.readFile(templateDir + '/' + req.path + '.template', 'utf8');
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2022-07-07 13:10:57 +02:00
|
|
|
res.writeHead(200, {'Content-Type': mimeType});
|
|
|
|
res.end(_.template(body)({config: config.client}));
|
2020-04-08 03:19:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
usePromise(async (req: Request, res: Response): Promise<void> => {
|
|
|
|
if (jsTemplateFiles.indexOf(req.path) >= 0) {
|
|
|
|
await serveTemplate('application/javascript', req, res);
|
|
|
|
}
|
|
|
|
});
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
router.use('/internal/admin', express.static(adminDir + '/'));
|
|
|
|
router.use('/', express.static(clientDir + '/'));
|
2020-04-08 03:19:55 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
app.use(config.server.rootPath, router);
|
|
|
|
}
|