mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-11 19:13:55 +08:00
feat: 🎸 Seperate auth check
This commit is contained in:
parent
083ee5d23b
commit
e957a17f1c
@ -7,6 +7,7 @@ import compression from "compression";
|
|||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
import { dirname } from "path";
|
import { dirname } from "path";
|
||||||
import sessionParser from "./routes/session_parser.js";
|
import sessionParser from "./routes/session_parser.js";
|
||||||
|
import checkAuthState from "./routes/auth_check.js";
|
||||||
import utils from "./services/utils.js";
|
import utils from "./services/utils.js";
|
||||||
import assets from "./routes/assets.js";
|
import assets from "./routes/assets.js";
|
||||||
import routes from "./routes/routes.js";
|
import routes from "./routes/routes.js";
|
||||||
@ -61,6 +62,7 @@ app.use(`/manifest.webmanifest`, express.static(path.join(scriptDir, "public/man
|
|||||||
app.use(`/robots.txt`, express.static(path.join(scriptDir, "public/robots.txt")));
|
app.use(`/robots.txt`, express.static(path.join(scriptDir, "public/robots.txt")));
|
||||||
app.use(`/icon.png`, express.static(path.join(scriptDir, "public/icon.png")));
|
app.use(`/icon.png`, express.static(path.join(scriptDir, "public/icon.png")));
|
||||||
app.use(sessionParser);
|
app.use(sessionParser);
|
||||||
|
app.use(checkAuthState);
|
||||||
app.use(favicon(`${scriptDir}/../images/app-icons/icon.ico`));
|
app.use(favicon(`${scriptDir}/../images/app-icons/icon.ico`));
|
||||||
|
|
||||||
// Check if TOTP is enabled and validate TOTP secret is set
|
// Check if TOTP is enabled and validate TOTP secret is set
|
||||||
|
29
src/routes/auth_check.ts
Normal file
29
src/routes/auth_check.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import totp from "../services/totp.js";
|
||||||
|
import open_id from "../services/open_id.js";
|
||||||
|
import type { Request, Response, NextFunction } from "express";
|
||||||
|
|
||||||
|
|
||||||
|
export default function checkAuthState(req: Request, res: Response, next: NextFunction) {
|
||||||
|
if (!req.session.loggedIn || req.path === '/login') return next();
|
||||||
|
|
||||||
|
const currentTotpStatus = totp.isTotpEnabled();
|
||||||
|
const currentSsoStatus = open_id.isOpenIDEnabled();
|
||||||
|
const lastAuthState = req.session.lastAuthState || { totpEnabled: false, ssoEnabled: false };
|
||||||
|
|
||||||
|
if (lastAuthState.totpEnabled !== currentTotpStatus ||
|
||||||
|
lastAuthState.ssoEnabled !== currentSsoStatus) {
|
||||||
|
req.session.destroy((err) => {
|
||||||
|
if (err) console.error('Error destroying session:', err);
|
||||||
|
|
||||||
|
if (typeof res.redirect === 'function') {
|
||||||
|
res.redirect('/login');
|
||||||
|
} else {
|
||||||
|
console.warn("res.redirect unavailable");
|
||||||
|
res.end?.();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
}
|
@ -3,9 +3,6 @@ import sessionFileStore from "session-file-store";
|
|||||||
import sessionSecret from "../services/session_secret.js";
|
import sessionSecret from "../services/session_secret.js";
|
||||||
import dataDir from "../services/data_dir.js";
|
import dataDir from "../services/data_dir.js";
|
||||||
import config from "../services/config.js";
|
import config from "../services/config.js";
|
||||||
import totp from "../services/totp.js";
|
|
||||||
import open_id from "../services/open_id.js";
|
|
||||||
import type { Request, Response, NextFunction } from "express";
|
|
||||||
|
|
||||||
const FileStore = sessionFileStore(session);
|
const FileStore = sessionFileStore(session);
|
||||||
|
|
||||||
@ -25,35 +22,4 @@ const sessionParser = session({
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
const checkAuthState = (req: Request, res: Response, next: NextFunction) => {
|
export default sessionParser;
|
||||||
if (!req.session.loggedIn || req.path === '/login') {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentTotpStatus = totp.isTotpEnabled();
|
|
||||||
const currentSsoStatus = open_id.isOpenIDEnabled();
|
|
||||||
|
|
||||||
const lastAuthState = req.session.lastAuthState || {
|
|
||||||
totpEnabled: false,
|
|
||||||
ssoEnabled: false
|
|
||||||
};
|
|
||||||
|
|
||||||
if (lastAuthState.totpEnabled !== currentTotpStatus ||
|
|
||||||
lastAuthState.ssoEnabled !== currentSsoStatus) {
|
|
||||||
req.session.destroy((err) => {
|
|
||||||
if (err) {
|
|
||||||
console.error('Error destroying session:', err);
|
|
||||||
}
|
|
||||||
res.redirect('/login');
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
next();
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function (req: Request, res: Response, next: NextFunction) {
|
|
||||||
sessionParser(req, res, () => {
|
|
||||||
checkAuthState(req, res, next);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user