Add named-token auth support to middleware and configService

authMiddleware now falls back to checking named tokens from settings
when JWT verification fails. configService.getNamedTokens() filters by
endDate, skipping expired tokens. Tokens without an endDate never expire.
This commit is contained in:
X4Lo committed 2026-06-30 23:54:04 +01:00
1 parent c6a838a91c
commit 0a79ee9768
2 files changed
+57 -9

No files matched your search

+44 -9
View File
@@ -1,6 +1,45 @@
const jwt = require("jsonwebtoken");
const configService = require("../services/configService");
function validateToken(tokenValue) {
const settings = configService.getSettings();
const jwtSecret = settings.jwtSecret || "showy-default-secret";
// 1. Check dedicated dashboardToken
if (settings.dashboardToken && settings.dashboardToken === tokenValue) {
return true;
}
// 2. Check named tokens (supports expiration)
const namedTokens = configService.getNamedTokens();
if (namedTokens.some((t) => t.value === tokenValue)) {
return true;
}
// 3. Try JWT
try {
jwt.verify(tokenValue, jwtSecret);
return true;
} catch {
// not a valid JWT
}
return false;
}
function extractToken(req) {
// Authorization: Bearer <token>
const auth = req.headers.authorization;
if (auth && auth.startsWith("Bearer ")) {
return auth.slice(7);
}
// ?token= query parameter
if (req.query.token) {
return req.query.token;
}
return null;
}
function authMiddleware(req, res, next) {
// Skip auth for login/verify/media endpoints
if (
@@ -11,20 +50,16 @@ function authMiddleware(req, res, next) {
return next();
}
const auth = req.headers.authorization;
if (!auth || !auth.startsWith("Bearer ")) {
const tokenValue = extractToken(req);
if (!tokenValue) {
return res.status(401).json({ error: "Authentication required" });
}
const settings = configService.getSettings();
const jwtSecret = settings.jwtSecret || "showy-default-secret";
try {
jwt.verify(auth.slice(7), jwtSecret);
next();
} catch {
if (!validateToken(tokenValue)) {
return res.status(401).json({ error: "Invalid or expired token" });
}
next();
}
module.exports = authMiddleware;