@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
26 lines (25 loc) • 1.18 kB
JavaScript
import { AuthenticationError } from "../exceptions/runtime.exceptions.js";
import { inject } from "awilix-express";
//#region src/middleware/slicer-api-key.middleware.ts
/**
* Middleware to authenticate requests using a slicer API key.
* This allows slicers like PrusaSlicer to upload files without requiring bearer token authentication.
*
* The API key should be passed in the X-Api-Key header.
* If the header is not present or invalid, the middleware will throw.
*/
const slicerApiKeyAuth = () => inject((loggerFactory, settingsStore) => async (req, res, next) => {
const logger = loggerFactory("Middleware:slicerApiKeyAuth");
const apiKey = req.headers["x-api-key"];
if (!apiKey?.length) throw new AuthenticationError("Header x-api-key is missing");
if (settingsStore.validateSlicerApiKey(apiKey)) {
logger.log(`Slicer API key authentication successful for ${req.originalUrl}`);
req.slicerApiKeyAuthenticated = true;
return next();
}
logger.warn(`Invalid slicer API key provided for ${req.originalUrl}`);
throw new AuthenticationError("Header x-api-key is wrong");
});
//#endregion
export { slicerApiKeyAuth };
//# sourceMappingURL=slicer-api-key.middleware.js.map