enforced authentication for APIs, vite .env configuration for auth & jobs

This commit is contained in:
Felix Kaspar 2024-05-30 01:03:15 +02:00
parent c19bc8d07a
commit fc3e2adc82
8 changed files with 48 additions and 29 deletions

View File

@ -1,7 +1,7 @@
JOBS_ENABLED=True VITE_JOBS_ENABLED=True
JOBS_DIR="./jobs" VITE_JOBS_DIR="./jobs"
AUTH_ENABLED=True VITE_AUTH_ENABLED=True
AUTH_SESSION_SECRET="default-secret" VITE_AUTH_SESSION_SECRET="default-secret"
SEQUELIZE_LOGGING=False VITE_SEQUELIZE_LOGGING=False

View File

@ -0,0 +1,9 @@
declare namespace NodeJS {
export interface ProcessEnv {
JOBS_ENABLED: "True" | "False",
JOBS_DIR: string,
AUTH_ENABLED: "True" | "False",
AUTH_SESSION_SECRET: string,
SEQUELIZE_LOGGING: "True" | "False"
}
}

View File

@ -6,7 +6,7 @@ import { Express } from "express";
export function connect(app: Express) { export function connect(app: Express) {
app.use(session({ app.use(session({
secret: process.env.SESSION_SECRET || "default-secret", secret: import.meta.env.VITE_SESSION_SECRET || "default-secret",
resave: false, resave: false,
saveUninitialized: false saveUninitialized: false
})); }));

View File

@ -1,10 +1,8 @@
import 'dotenv/config';
import { Sequelize, DataTypes } from "sequelize"; import { Sequelize, DataTypes } from "sequelize";
//TODO: Make this configurable //TODO: Make this configurable
const sequelize = new Sequelize("sqlite::memory:", { const sequelize = new Sequelize("sqlite::memory:", {
logging: process.env.SEQUELIZE_LOGGING === "True" ? console.log : false logging: import.meta.env.VITE_SEQUELIZE_LOGGING === "True" ? console.log : false
}); });
import { User, AccessRule, APIKey, Password } from "../auth/user/user-model"; import { User, AccessRule, APIKey, Password } from "../auth/user/user-model";

View File

@ -1,5 +1,3 @@
import 'dotenv/config';
/* /*
* translation * translation
*/ */
@ -27,14 +25,14 @@ console.log("Available Modules: ", listOperatorNames());
* jobs * jobs
*/ */
if(process.env.JOBS_ENABLED === "True") if(import.meta.env.VITE_JOBS_ENABLED === "True")
import("./jobs/jobs-controller"); import("./jobs/jobs-controller");
/** /**
* database * database
*/ */
if(process.env.AUTH_ENABLED === "True") if(import.meta.env.VITE_AUTH_ENABLED === "True")
import("./data/sequelize-relations"); import("./data/sequelize-relations");
/* /*
@ -45,22 +43,29 @@ import express from "express";
const app = express(); const app = express();
const PORT = 8000; const PORT = 8000;
import api from "./routes/api/api-controller";
/* /*
* auth * auth
*/ */
if(process.env.AUTH_ENABLED === "True") console.log(import.meta.env)
import("./auth/auth-controller.ts").then(router => router.connect(app));
if(import.meta.env.VITE_AUTH_ENABLED === "True") {
import("./auth/auth-controller.ts").then(router => router.connect(app)).finally(() => {
/* /*
* api * api
*/ */
import api from "./routes/api/api-controller";
app.use("/api", api); app.use("/api", api);
});
}
else {
app.use("/api", api);
}
// viteNode // viteNode
if (import.meta.env.PROD) { if (import.meta.env.VITE_PROD) {
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`http://localhost:${PORT}`); console.log(`http://localhost:${PORT}`);
}); });

View File

@ -1,11 +1,10 @@
import { traverseOperations } from '@stirling-pdf/shared-operations/src/workflow/traverseOperations'; import { traverseOperations } from '@stirling-pdf/shared-operations/src/workflow/traverseOperations';
import { PdfFile, RepresentationType } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile'; import { PdfFile, RepresentationType } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile';
import 'dotenv/config';
import fs from 'fs'; import fs from 'fs';
import path from "path"; import path from "path";
import toml from 'toml'; import toml from 'toml';
const jobsDir = process.env.JOBS_DIR; const jobsDir = import.meta.env.VITE_JOBS_DIR;
// TODO: Also remove watched folders // TODO: Also remove watched folders
const watchedFolders: { const watchedFolders: {

View File

@ -5,6 +5,15 @@ import dynamicOperations from "./dynamic-operations-controller";
const router = express.Router(); const router = express.Router();
router.use((req, res, next) => {
console.log(import.meta.env.VITE_AUTH_ENABLED);
if(import.meta.env.VITE_AUTH_ENABLED === "False" || req.user) {
next();
return;
}
res.status(403).json({"Error": "Authentication failed."});
});
router.get("/", (req: Request, res: Response) => { router.get("/", (req: Request, res: Response) => {
// TODO: Implement root api endpoint // TODO: Implement root api endpoint
res.status(501).json({"Error": "Unfinished Endpoint. This sould probably send some api docs?"}); res.status(501).json({"Error": "Unfinished Endpoint. This sould probably send some api docs?"});

View File

@ -3,7 +3,6 @@ const router = express.Router();
import multer from "multer"; import multer from "multer";
const upload = multer(); const upload = multer();
import { getOperatorByName } from "@stirling-pdf/shared-operations/src/workflow/operatorAccessor"; import { getOperatorByName } from "@stirling-pdf/shared-operations/src/workflow/operatorAccessor";
import { Operator } from "@stirling-pdf/shared-operations/src/functions";
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile"; import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
import { respondWithPdfFiles } from "../../utils/response-utils"; import { respondWithPdfFiles } from "../../utils/response-utils";