2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
const migrationService = require('./migration');
|
2017-12-11 01:56:59 +08:00
|
|
|
const sql = require('./sql');
|
2017-11-06 06:58:55 +08:00
|
|
|
const utils = require('./utils');
|
2017-10-26 10:39:21 +08:00
|
|
|
|
|
|
|
async function checkAuth(req, res, next) {
|
2017-12-28 07:23:24 +08:00
|
|
|
if (!await sql.isUserInitialized()) {
|
2017-12-04 11:29:23 +08:00
|
|
|
res.redirect("setup");
|
|
|
|
}
|
|
|
|
else if (!req.session.loggedIn && !utils.isElectron()) {
|
2017-10-16 04:32:49 +08:00
|
|
|
res.redirect("login");
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
2017-12-11 01:56:59 +08:00
|
|
|
else if (!await sql.isDbUpToDate()) {
|
2017-12-04 11:29:23 +08:00
|
|
|
res.redirect("migration");
|
2017-10-16 04:32:49 +08:00
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
else {
|
2017-12-04 11:29:23 +08:00
|
|
|
next();
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
2017-10-16 04:32:49 +08:00
|
|
|
}
|
|
|
|
|
2017-12-01 09:51:35 +08:00
|
|
|
async function checkAuthForMigrationPage(req, res, next) {
|
2017-11-06 06:58:55 +08:00
|
|
|
if (!req.session.loggedIn && !utils.isElectron()) {
|
2017-10-27 08:31:31 +08:00
|
|
|
res.redirect("login");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:59:05 +08: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 if (await sql.isDbUpToDate()) {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.status(409).send("Mismatched app versions"); // need better response than that
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
async function checkApiAuth(req, res, next) {
|
2017-12-01 12:29:21 +08:00
|
|
|
if (!req.session.loggedIn) {
|
2017-11-01 08:09:07 +08:00
|
|
|
res.status(401).send("Not authorized");
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
2017-12-11 01:56:59 +08:00
|
|
|
else if (await sql.isDbUpToDate()) {
|
2017-10-16 04:32:49 +08:00
|
|
|
next();
|
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
else {
|
2017-11-01 08:09:07 +08:00
|
|
|
res.status(409).send("Mismatched app versions"); // need better response than that
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
2017-10-16 04:32:49 +08:00
|
|
|
}
|
|
|
|
|
2017-12-01 09:51:35 +08:00
|
|
|
async function checkApiAuthForMigrationPage(req, res, next) {
|
2017-12-01 12:29:21 +08:00
|
|
|
if (!req.session.loggedIn) {
|
2017-11-01 08:09:07 +08:00
|
|
|
res.status(401).send("Not authorized");
|
2017-10-27 08:31:31 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 11:29:23 +08:00
|
|
|
async function checkAppNotInitialized(req, res, next) {
|
2017-12-28 07:23:24 +08:00
|
|
|
if (await sql.isUserInitialized()) {
|
2017-12-04 11:29:23 +08:00
|
|
|
res.status(400).send("App already initialized.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function checkSenderToken(req, res, next) {
|
|
|
|
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 if (await sql.isDbUpToDate()) {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.status(409).send("Mismatched app versions"); // need better response than that
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 04:32:49 +08:00
|
|
|
module.exports = {
|
|
|
|
checkAuth,
|
2017-12-01 09:51:35 +08:00
|
|
|
checkAuthForMigrationPage,
|
2017-10-27 08:31:31 +08:00
|
|
|
checkApiAuth,
|
2017-12-04 11:29:23 +08:00
|
|
|
checkApiAuthForMigrationPage,
|
2018-01-07 22:59:05 +08:00
|
|
|
checkAppNotInitialized,
|
2018-03-31 05:29:13 +08:00
|
|
|
checkApiAuthOrElectron,
|
|
|
|
checkSenderToken
|
2017-10-16 04:32:49 +08:00
|
|
|
};
|