From 601c0544ff68e12e6407b5a2beabc29103fe39d2 Mon Sep 17 00:00:00 2001 From: DarrenLo0530 Date: Fri, 10 Mar 2023 16:29:00 -0500 Subject: [PATCH] Added whitelisting --- src/server.ts | 29 +++++++++++++++++++++++++++++ src/whitelistedUser.json | 3 +++ tsconfig.json | 3 ++- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/whitelistedUser.json diff --git a/src/server.ts b/src/server.ts index 7b31b8c..2505665 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,11 +4,29 @@ const app = express(); const publicDirname = path.join(__dirname, "../public"); const DEVELOPMENT_PORT = 8000; +import whiteListedUsers from "./whitelistedUser.json"; + import * as dotenv from "dotenv"; import { makePullRequest } from "./giteaClient"; dotenv.config(); +const devQuestId = "cdalek" + +const validateAuthorization = (req, res, next) => { + // TODO: Replace with actual quest id from request + if (!whiteListedUsers.includes(devQuestId)) { + const err = Error("UnauthorizedError"); + err.name = "UnauthorizedError"; + next(err); + } + + return next(); +} + +// Validate user's quest id +app.use(validateAuthorization); + app.get("/", async (req, res) => { res.sendFile(path.join(publicDirname, "index.html")); @@ -19,6 +37,17 @@ app.get("/", async (req, res) => { // Allows serving static files app.use(express.static(publicDirname)); +// Catch errors at very end +app.use(async (err, req, res, next) => { + console.log(err); + if (err.name == "UnauthorizedError") { + return res.sendStatus(401); + } + + return res.sendStatus(404); +}); + app.listen(DEVELOPMENT_PORT, () => { console.log(`Listening on http://localhost:${DEVELOPMENT_PORT}`); }); + diff --git a/src/whitelistedUser.json b/src/whitelistedUser.json new file mode 100644 index 0000000..5edb958 --- /dev/null +++ b/src/whitelistedUser.json @@ -0,0 +1,3 @@ +[ + "cdalek" +] \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 8d9e329..4c5a4bf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,8 @@ "noEmitOnError": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, - "outDir": "build" + "outDir": "build", + "resolveJsonModule": true, }, "include": ["src"] }