Skip to content

Commit aab40c8

Browse files
authored
Merge pull request #150 from PeerPrep/bhcs/add-serverless-logic
Assignment 6 (placeholder PR)
2 parents df0eb63 + b0da66c commit aab40c8

File tree

14 files changed

+206
-60
lines changed

14 files changed

+206
-60
lines changed

deployment/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ MONGODB_URL=
55
BUCKET_NAME=
66
USERS_SERVICE_URL=
77
POSTGRES_URL=
8+
PASSWORD_HEADER=

deployment/docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ 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}
78+
PASSWORD_HEADER: ${PASSWORD_HEADER}
8079

8180
users:
8281
image: ghcr.io/peerprep/peerprep-users-service:latest

questions/.env.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ 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=
5+
PASSWORD_HEADER=peerprep-password

questions/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"cors": "^2.8.5",
1818
"express": "^4.18.2",
1919
"firebase-admin": "^11.11.0",
20-
"mongoose": "^7.5.1"
20+
"mongoose": "^7.5.1",
21+
"openpgp": "^5.11.0"
2122
},
2223
"devDependencies": {
2324
"@types/body-parser": "^1.19.2",

questions/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { getAuth } from "firebase-admin/auth";
88
import http from "http";
99
import mongoose from "mongoose";
1010
import getFirebaseMiddleware from "./middleware/auth";
11-
import decryptRequestBody from "./middleware/serverless";
12-
import { normalRouter } from "./router";
11+
import validatePasswordHeader from "./middleware/serverless";
12+
import { normalRouter, serverlessRouter } from "./router";
1313

1414
dotenv.config();
1515

@@ -34,7 +34,7 @@ app.use(cors(corsOptions));
3434
app.use(compression());
3535
app.use(bodyParser.json());
3636
app.use("/api/v1/", getFirebaseMiddleware(firebaseAuth), normalRouter());
37-
app.use("/api/serverless/", decryptRequestBody(), normalRouter());
37+
app.use("/api/serverless/", validatePasswordHeader(), serverlessRouter());
3838

3939
const server = http.createServer(app);
4040

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,37 @@
11
import express, { NextFunction } from "express";
2-
import { handleServerError } from "../utils";
3-
import crypto from "crypto";
2+
import { StatusMessageType } from "../types";
3+
import { handleCustomError, handleServerError } from "../utils";
44

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 = () => {
5+
const validatePasswordHeader = () => {
216
return async (
227
req: express.Request,
238
res: express.Response,
249
next: NextFunction
2510
) => {
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();
11+
const password = process.env.PASSWORD_HEADER;
12+
if (!password) {
13+
handleServerError(new Error("No password provided"), res);
5514
return;
56-
} catch (err: any) {
57-
handleServerError(err, res);
15+
}
16+
17+
const receivedPassword = req.headers["password_header"];
18+
19+
if (receivedPassword != password) {
20+
console.dir(req.headers);
21+
handleCustomError(
22+
res,
23+
{
24+
type: StatusMessageType.ERROR,
25+
message: "No password provided",
26+
},
27+
401
28+
);
5829
return;
5930
}
31+
32+
next();
33+
return;
6034
};
6135
};
6236

63-
export default decryptRequestBody;
37+
export default validatePasswordHeader;

questions/yarn.lock

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,16 @@ arrify@^2.0.0:
516516
resolved "https://registry.yarnpkg.com/arrify/-/arrify-2.0.1.tgz#c9655e9331e0abcd588d2a7cad7e9956f66701fa"
517517
integrity sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==
518518

519+
asn1.js@^5.0.0:
520+
version "5.4.1"
521+
resolved "https://registry.yarnpkg.com/asn1.js/-/asn1.js-5.4.1.tgz#11a980b84ebb91781ce35b0fdc2ee294e3783f07"
522+
integrity sha512-+I//4cYPccV8LdmBLiX8CYvf9Sp3vQsrqu2QNXRcrbiWvcx/UdlFiqUJJzxRQxgsZmvhXhn4cSKeSmoFjVdupA==
523+
dependencies:
524+
bn.js "^4.0.0"
525+
inherits "^2.0.1"
526+
minimalistic-assert "^1.0.0"
527+
safer-buffer "^2.1.0"
528+
519529
async-retry@^1.3.3:
520530
version "1.3.3"
521531
resolved "https://registry.yarnpkg.com/async-retry/-/async-retry-1.3.3.tgz#0e7f36c04d8478e7a58bdbed80cedf977785f280"
@@ -562,6 +572,11 @@ bluebird@^3.7.2:
562572
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
563573
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==
564574

575+
bn.js@^4.0.0:
576+
version "4.12.0"
577+
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
578+
integrity sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==
579+
565580
566581
version "1.20.1"
567582
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.20.1.tgz#b1812a8912c195cd371a3ee5e66faa2338a5c668"
@@ -1313,7 +1328,7 @@ inflight@^1.0.4:
13131328
once "^1.3.0"
13141329
wrappy "1"
13151330

1316-
inherits@2, [email protected], inherits@^2.0.3:
1331+
inherits@2, [email protected], inherits@^2.0.1, inherits@^2.0.3:
13171332
version "2.0.4"
13181333
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
13191334
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
@@ -1652,6 +1667,11 @@ mime@^3.0.0:
16521667
resolved "https://registry.yarnpkg.com/mime/-/mime-3.0.0.tgz#b374550dca3a0c18443b0c950a6a58f1931cf7a7"
16531668
integrity sha512-jSCU7/VB1loIWBZe14aEYHU/+1UMEHoaO7qxCOVJOw9GgH72VAWppxNcjU+x9a2k3GSIBXNKxXQFqRvvZ7vr3A==
16541669

1670+
minimalistic-assert@^1.0.0:
1671+
version "1.0.1"
1672+
resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7"
1673+
integrity sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A==
1674+
16551675
minimatch@^3.1.1, minimatch@^3.1.2:
16561676
version "3.1.2"
16571677
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
@@ -1814,6 +1834,13 @@ once@^1.3.0, once@^1.4.0:
18141834
dependencies:
18151835
wrappy "1"
18161836

1837+
openpgp@^5.11.0:
1838+
version "5.11.0"
1839+
resolved "https://registry.yarnpkg.com/openpgp/-/openpgp-5.11.0.tgz#cec5b285d188148f7b5201b9aceb53850cc286a2"
1840+
integrity sha512-hytHsxIPtRhuh6uAmoBUThHSwHSX3imLu7x4453T+xkVqIw49rl22MRD4KQIAQdCDoVdouejzYgcuLmMA/2OAA==
1841+
dependencies:
1842+
asn1.js "^5.0.0"
1843+
18171844
optionator@^0.8.1:
18181845
version "0.8.3"
18191846
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.3.tgz#84fa1d036fe9d3c7e21d99884b601167ec8fb495"
@@ -2035,7 +2062,7 @@ [email protected], safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@~5.2.0:
20352062
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
20362063
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
20372064

2038-
"safer-buffer@>= 2.1.2 < 3":
2065+
"safer-buffer@>= 2.1.2 < 3", safer-buffer@^2.1.0:
20392066
version "2.1.2"
20402067
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
20412068
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

serverless/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BASE_URL=""
2+
PASSWORD_HEADER=""

serverless/.firebaserc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "neat-ring-403815"
4+
}
5+
}

serverless/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

0 commit comments

Comments
 (0)