mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-22 23:45:02 +00:00
better typing, authorization middleware, test for creating apikeys
This commit is contained in:
parent
187b7b3e78
commit
28862d70ca
6
server-node/declarations/ExpressUser.d.ts
vendored
6
server-node/declarations/ExpressUser.d.ts
vendored
@ -1,5 +1,7 @@
|
|||||||
|
type UserModel = import("../src/auth/user/user-model").User;
|
||||||
|
|
||||||
declare namespace Express {
|
declare namespace Express {
|
||||||
interface User {
|
interface User extends UserModel {
|
||||||
id?: number;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
8
server-node/src/auth/checkAuthorizedMiddleware.ts
Normal file
8
server-node/src/auth/checkAuthorizedMiddleware.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
|
||||||
|
export function checkAuthorized(req: Request, res: Response, next: NextFunction) {
|
||||||
|
if(import.meta.env.VITE_AUTH_ENABLED === "False" || req.user) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
return res.status(403).json({"Error": "Authentication failed."});
|
||||||
|
}
|
@ -1,18 +1,13 @@
|
|||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
|
|
||||||
|
import { checkAuthorized } from "../../auth/checkAuthorizedMiddleware";
|
||||||
|
|
||||||
import workflow from "./workflow-controller";
|
import workflow from "./workflow-controller";
|
||||||
import dynamicOperations from "./dynamic-operations-controller";
|
import dynamicOperations from "./dynamic-operations-controller";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.use((req, res, next) => {
|
router.use(checkAuthorized);
|
||||||
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
|
||||||
|
@ -4,9 +4,10 @@ import login from "./login-controller";
|
|||||||
import logout from "./logout-controller";
|
import logout from "./logout-controller";
|
||||||
import register from "./register-controller";
|
import register from "./register-controller";
|
||||||
import status from "./status-controller";
|
import status from "./status-controller";
|
||||||
|
import createAPIKey from "./create-api-key-controller"
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.use("/", [login, logout, register, status]);
|
router.use("/", [createAPIKey, login, logout, register, status]);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
11
server-node/src/routes/auth/create-api-key-controller.ts
Normal file
11
server-node/src/routes/auth/create-api-key-controller.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { checkAuthorized } from "../../auth/checkAuthorizedMiddleware";
|
||||||
|
import { APIKey } from "../../auth/user/user-model";
|
||||||
|
import express, { Request, Response } from "express";
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.post('/create-api-key', checkAuthorized, async function(req: Request, res: Response) {
|
||||||
|
const apikey: APIKey | undefined = await req.user?.createAPIKey({apikey: "test"}); //TODO: Replace with random string
|
||||||
|
res.json({apikey: apikey});
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
@ -1,8 +1,9 @@
|
|||||||
|
import { checkAuthorized } from "../../auth/checkAuthorizedMiddleware";
|
||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.get('/status', async function(req: Request, res: Response) {
|
router.get('/status', checkAuthorized, async function(req: Request, res: Response) {
|
||||||
res.json({user: req.user})
|
res.json({user: req.user});
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
Loading…
x
Reference in New Issue
Block a user