2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2017-12-10 12:56:59 -05:00
|
|
|
const sql = require('./sql');
|
2018-04-02 21:25:20 -04:00
|
|
|
const sqlInit = require('./sql_init');
|
2017-11-05 17:58:55 -05:00
|
|
|
const utils = require('./utils');
|
2018-07-22 19:56:20 +02:00
|
|
|
const passwordEncryptionService = require('./password_encryption');
|
|
|
|
const optionService = require('./options');
|
2017-10-25 22:39:21 -04:00
|
|
|
|
|
|
|
async function checkAuth(req, res, next) {
|
2018-07-22 19:56:20 +02:00
|
|
|
if (!await sqlInit.isDbInitialized()) {
|
2017-12-03 22:29:23 -05:00
|
|
|
res.redirect("setup");
|
|
|
|
}
|
|
|
|
else if (!req.session.loggedIn && !utils.isElectron()) {
|
2017-10-15 16:32:49 -04:00
|
|
|
res.redirect("login");
|
2017-10-25 22:39:21 -04:00
|
|
|
}
|
2017-10-26 20:31:31 -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
|
|
|
|
async function checkApiAuthOrElectron(req, res, next) {
|
|
|
|
if (!req.session.loggedIn && !utils.isElectron()) {
|
|
|
|
res.status(401).send("Not authorized");
|
|
|
|
}
|
|
|
|
else {
|
2017-10-15 16:32:49 -04:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 15:49:22 -04:00
|
|
|
async function checkApiAuth(req, res, next) {
|
2017-11-30 23:29:21 -05:00
|
|
|
if (!req.session.loggedIn) {
|
2017-10-31 20:09:07 -04:00
|
|
|
res.status(401).send("Not authorized");
|
2017-10-26 20:31:31 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 20:35:03 +02:00
|
|
|
async function checkAppInitialized(req, res, next) {
|
|
|
|
if (!await sqlInit.isDbInitialized()) {
|
|
|
|
res.redirect("setup");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 22:29:23 -05:00
|
|
|
async function checkAppNotInitialized(req, res, next) {
|
2018-07-22 19:56:20 +02:00
|
|
|
if (await sqlInit.isDbInitialized()) {
|
2017-12-03 22:29:23 -05:00
|
|
|
res.status(400).send("App already initialized.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 13:12:40 +02:00
|
|
|
async function checkToken(req, res, next) {
|
2018-03-30 17:29:13 -04:00
|
|
|
const token = req.headers.authorization;
|
|
|
|
|
|
|
|
if (await sql.getValue("SELECT COUNT(*) FROM api_tokens WHERE isDeleted = 0 AND token = ?", [token]) === 0) {
|
|
|
|
res.status(401).send("Not authorized");
|
|
|
|
}
|
|
|
|
else {
|
2018-06-10 15:49:22 -04:00
|
|
|
next();
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 19:56:20 +02:00
|
|
|
async function checkBasicAuth(req, res, next) {
|
|
|
|
const header = req.headers.authorization || '';
|
|
|
|
const token = header.split(/\s+/).pop() || '';
|
|
|
|
const auth = new Buffer.from(token, 'base64').toString();
|
|
|
|
const [username, password] = auth.split(/:/);
|
|
|
|
|
|
|
|
const dbUsername = await optionService.getOption('username');
|
|
|
|
|
|
|
|
if (dbUsername !== username || !await passwordEncryptionService.verifyPassword(password)) {
|
|
|
|
res.status(401).send("Not authorized");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 16:32:49 -04:00
|
|
|
module.exports = {
|
|
|
|
checkAuth,
|
2017-10-26 20:31:31 -04:00
|
|
|
checkApiAuth,
|
2018-07-24 20:35:03 +02:00
|
|
|
checkAppInitialized,
|
2018-01-07 09:59:05 -05:00
|
|
|
checkAppNotInitialized,
|
2018-03-30 17:29:13 -04:00
|
|
|
checkApiAuthOrElectron,
|
2019-07-07 13:12:40 +02:00
|
|
|
checkToken,
|
2018-07-22 19:56:20 +02:00
|
|
|
checkBasicAuth
|
2017-10-15 16:32:49 -04:00
|
|
|
};
|