Skip to content

Commit c7d9ea5

Browse files
Merge pull request #152 from PeerPrep/questions-service-serverless-update
Add new endpoint
2 parents c698a62 + 3d17785 commit c7d9ea5

File tree

7 files changed

+88
-11
lines changed

7 files changed

+88
-11
lines changed

deployment/docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ services:
7575
MONGODB_URL: ${MONGODB_URL}
7676
BUCKET_NAME: ${BUCKET_NAME}
7777
USERS_SERVICE_URL: ${USERS_SERVICE_URL}
78+
INITIALIZATION_VECTOR: ${INITIALIZATION_VECTOR}
79+
ENCRYPTION_KEY: ${ENCRYPTION_KEY}
7880

7981
users:
8082
image: ghcr.io/peerprep/peerprep-users-service:latest

questions/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ MONGODB_URL=mongodb://localhost:27017/questions
22
BUCKET_NAME=peerprep3219.appspot.com
33
GOOGLE_APPLICATION_CREDENTIALS=./service-account.json
44
USERS_SERVICE_URL=http://localhost:6969
5+
INITIALIZATION_VECTOR=
6+
ENCRYPTION_KEY=

questions/src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import express from "express";
21
import bodyParser from "body-parser";
32
import compression from "compression";
43
import cors, { CorsOptions } from "cors";
4+
import dotenv from "dotenv";
5+
import express from "express";
6+
import { applicationDefault, initializeApp } from "firebase-admin/app";
7+
import { getAuth } from "firebase-admin/auth";
58
import http from "http";
69
import mongoose from "mongoose";
7-
import router from "./router";
8-
import { initializeApp } from "firebase-admin/app";
9-
import { applicationDefault } from "firebase-admin/app";
10-
import { getAuth } from "firebase-admin/auth";
11-
import getFirebaseMiddleware from "./middleware";
12-
import dotenv from "dotenv";
10+
import getFirebaseMiddleware from "./middleware/auth";
11+
import decryptRequestBody from "./middleware/serverless";
12+
import { normalRouter } from "./router";
1313

1414
dotenv.config();
1515

@@ -33,7 +33,8 @@ const app = express();
3333
app.use(cors(corsOptions));
3434
app.use(compression());
3535
app.use(bodyParser.json());
36-
app.use("/api/v1/", getFirebaseMiddleware(firebaseAuth), router());
36+
app.use("/api/v1/", getFirebaseMiddleware(firebaseAuth), normalRouter());
37+
app.use("/api/serverless", decryptRequestBody(), normalRouter());
3738

3839
const server = http.createServer(app);
3940

File renamed without changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import express, { NextFunction } from "express";
2+
import { handleServerError } from "../utils";
3+
import crypto from "crypto";
4+
5+
const decryptMessage = (iv: string, key: string, ciphertext: string) => {
6+
try {
7+
const decipher = crypto.createDecipheriv(
8+
"aes-256-cbc",
9+
Buffer.from(key, "hex"),
10+
Buffer.from(iv, "hex")
11+
);
12+
let decrypted = decipher.update(ciphertext, "hex", "utf-8");
13+
decrypted += decipher.final("utf-8");
14+
return decrypted;
15+
} catch (error) {
16+
return null;
17+
}
18+
};
19+
20+
const decryptRequestBody = () => {
21+
return async (
22+
req: express.Request,
23+
res: express.Response,
24+
next: NextFunction
25+
) => {
26+
try {
27+
const iv = process.env.INITIALIZATION_VECTOR;
28+
const key = process.env.ENCRYPTION_KEY;
29+
const ciphertext = req.body;
30+
31+
if (!key) {
32+
handleServerError(new Error("No encryption key provided"), res);
33+
return;
34+
}
35+
36+
if (!iv) {
37+
handleServerError(new Error("No initialization vector provided"), res);
38+
return;
39+
}
40+
41+
if (!ciphertext) {
42+
handleServerError(new Error("No request body provided"), res);
43+
return;
44+
}
45+
46+
const decryptedMsg = decryptMessage(iv, key, ciphertext);
47+
if (!decryptedMsg) {
48+
handleServerError(new Error("Unable to decrypt request body"), res);
49+
return;
50+
}
51+
52+
req.body = JSON.parse(decryptedMsg);
53+
54+
next();
55+
return;
56+
} catch (err: any) {
57+
handleServerError(err, res);
58+
return;
59+
}
60+
};
61+
};
62+
63+
export default decryptRequestBody;

questions/src/router/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import express from "express";
2-
import questions from "./questions";
2+
import { questions, serverlessQuestions } from "./questions";
33

44
const router = express.Router();
55

6-
export default (): express.Router => {
6+
export const normalRouter = (): express.Router => {
77
questions(router);
88
return router;
99
};
10+
11+
export const serverlessRouter = (): express.Router => {
12+
serverlessQuestions(router);
13+
return router;
14+
};

questions/src/router/questions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import {
88
updateQuestion,
99
} from "../controllers/questions";
1010

11-
export default (router: express.Router) => {
11+
export const questions = (router: express.Router) => {
1212
router.get("/questions", getAllQuestions);
1313
router.get("/questions/:id", getQuestion);
1414
router.get("/questions/group/:ids", getQuestionsByGroupOfIds);
1515
router.post("/questions", createQuestion);
1616
router.put("/questions/:id", updateQuestion);
1717
router.delete("/questions/:id", deleteQuestion);
1818
};
19+
20+
export const serverlessQuestions = (router: express.Router) => {
21+
router.post("/questions", createQuestion);
22+
};

0 commit comments

Comments
 (0)