mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-04 13:59:09 +00:00
refactor: handle prefix on login
- post-login redirect should account for prefix - scope cookies to each path by prefix
This commit is contained in:
committed by
Carlos Valente
parent
deb1f043c7
commit
9c5116f197
@@ -40,6 +40,7 @@ override.css
|
|||||||
|
|
||||||
# working stuff
|
# working stuff
|
||||||
**/TODO.md
|
**/TODO.md
|
||||||
|
**.local.**
|
||||||
|
|
||||||
# docker utils
|
# docker utils
|
||||||
ontime-db
|
ontime-db
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import { consoleSuccess, consoleHighlight, consoleError } from './utils/console.
|
|||||||
// Import middleware configuration
|
// Import middleware configuration
|
||||||
import { bodyParser } from './middleware/bodyParser.js';
|
import { bodyParser } from './middleware/bodyParser.js';
|
||||||
import { compressedStatic } from './middleware/staticGZip.js';
|
import { compressedStatic } from './middleware/staticGZip.js';
|
||||||
import { loginRouter, makeAuthenticateMiddleware } from './middleware/authenticate.js';
|
import { makeLoginRouter, makeAuthenticateMiddleware } from './middleware/authenticate.js';
|
||||||
|
|
||||||
// Import Routers
|
// Import Routers
|
||||||
import { appRouter } from './api-data/index.js';
|
import { appRouter } from './api-data/index.js';
|
||||||
@@ -83,6 +83,7 @@ app.options('*splat', cors()); // enable pre-flight cors
|
|||||||
app.use(bodyParser);
|
app.use(bodyParser);
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
const { authenticate, authenticateAndRedirect } = makeAuthenticateMiddleware(prefix);
|
const { authenticate, authenticateAndRedirect } = makeAuthenticateMiddleware(prefix);
|
||||||
|
const loginRouter = makeLoginRouter(prefix);
|
||||||
|
|
||||||
// implement health check route
|
// implement health check route
|
||||||
app.get(`${prefix}/health`, (_req, res) => {
|
app.get(`${prefix}/health`, (_req, res) => {
|
||||||
|
|||||||
@@ -21,24 +21,33 @@ const publicAssets = new Set([
|
|||||||
'/site.webmanifest',
|
'/site.webmanifest',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
export const loginRouter = express.Router();
|
/**
|
||||||
|
* Creates a login router with the provided prefix
|
||||||
|
* @param {string} prefix - Prefix is used for the client hashes in Ontime Cloud
|
||||||
|
*/
|
||||||
|
export function makeLoginRouter(prefix: string) {
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
// serve static files at root
|
// serve static files at root
|
||||||
loginRouter.use('/', express.static(srcFiles.login));
|
router.use('/', express.static(srcFiles.login));
|
||||||
|
|
||||||
// verify password and set cookies + redirect appropriately
|
// verify password and set cookies + redirect appropriately
|
||||||
loginRouter.post('/', (req, res) => {
|
router.post('/', (req, res) => {
|
||||||
res.clearCookie('token');
|
res.clearCookie('token', { path: prefix || '/' });
|
||||||
const { password: reqPassword, redirect } = req.body;
|
const { password: reqPassword, redirect } = req.body;
|
||||||
|
|
||||||
if (hashPassword(reqPassword) === hashedPassword) {
|
if (hashPassword(reqPassword) === hashedPassword) {
|
||||||
setSessionCookie(res, hashedPassword);
|
setSessionCookie(res, hashedPassword, prefix);
|
||||||
res.redirect(redirect || '/');
|
// If redirect is provided, use it; otherwise redirect to the prefix root
|
||||||
return;
|
res.redirect(redirect || (prefix ? `${prefix}/` : '/'));
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
res.status(401).send('Unauthorized');
|
res.status(401).send('Unauthorized');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return router;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Express middleware to authenticate requests
|
* Express middleware to authenticate requests
|
||||||
@@ -77,7 +86,8 @@ export function makeAuthenticateMiddleware(prefix: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we shouldnt be here in the login route
|
// we shouldnt be here in the login route
|
||||||
if (req.originalUrl.startsWith('/login')) {
|
const loginPath = prefix ? `${prefix}/login` : '/login';
|
||||||
|
if (req.originalUrl.startsWith(loginPath)) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +103,7 @@ export function makeAuthenticateMiddleware(prefix: string) {
|
|||||||
// if the user gives is a token in the query params, we set the cookie to be used in further requests
|
// if the user gives is a token in the query params, we set the cookie to be used in further requests
|
||||||
if (req.query.token === hashedPassword) {
|
if (req.query.token === hashedPassword) {
|
||||||
if (hashedPassword !== undefined) {
|
if (hashedPassword !== undefined) {
|
||||||
setSessionCookie(res, hashedPassword);
|
setSessionCookie(res, hashedPassword, prefix);
|
||||||
}
|
}
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
@@ -148,11 +158,11 @@ export function authenticateSocket(_ws: WebSocket, req: IncomingMessage, next: (
|
|||||||
* Sets a cookie with the provided token
|
* Sets a cookie with the provided token
|
||||||
* We currently add a full 'rw' permission scope, this should be filtered when dealing with presets
|
* We currently add a full 'rw' permission scope, this should be filtered when dealing with presets
|
||||||
*/
|
*/
|
||||||
function setSessionCookie(res: Response, token: string) {
|
function setSessionCookie(res: Response, token: string, prefix: string) {
|
||||||
res.cookie('token', JSON.stringify({ token, scope: 'rw' }), {
|
res.cookie('token', JSON.stringify({ token, scope: 'rw' }), {
|
||||||
httpOnly: false, // allow websocket to access cookie
|
httpOnly: false, // allow websocket to access cookie
|
||||||
secure: true,
|
secure: true,
|
||||||
path: '/', // allow cookie to be accessed from any path
|
path: prefix || '/', // allow cookie to be accessed from prefix path or root
|
||||||
sameSite: 'none', // allow cookies to be sent in cross-origin requests (e.g., iframes)
|
sameSite: 'none', // allow cookies to be sent in cross-origin requests (e.g., iframes)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user