Skip to content

Commit 6518bb8

Browse files
committed
wip
1 parent 2ee79e6 commit 6518bb8

File tree

7 files changed

+68
-77
lines changed

7 files changed

+68
-77
lines changed

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/src/router/index.ts

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

4-
const router = express.Router();
5-
64
export const normalRouter = (): express.Router => {
75
questions(router);
86
return router;
97
};
108

119
export const serverlessRouter = (): express.Router => {
10+
const router = express.Router();
1211
serverlessQuestions(router);
1312
return router;
1413
};

questions/src/router/questions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ export const questions = (router: express.Router) => {
1818
};
1919

2020
export const serverlessQuestions = (router: express.Router) => {
21-
router.post("/questions", createQuestion);
21+
router.post("/questions", (req, res) => {
22+
console.log("Hello world");
23+
res.send("Hello world");
24+
});
2225
};

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/run.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,22 @@
22
import os
33
import tempfile
44

5-
import Crypto
6-
import Crypto.Random
75
import requests
8-
from Crypto.Cipher import AES
96
from dotenv import load_dotenv
107
from git import Repo
118

129
load_dotenv()
1310
BASE_URL = os.environ.get("BASE_URL")
14-
INITIALIZATION_VECTOR = os.environ.get("INITIALIZATION_VECTOR")
15-
ENCRYPTION_KEY = os.environ.get("ENCRYPTION_KEY")
11+
PASSWORD_HEADER = os.environ.get("PASSWORD_HEADER")
1612

17-
def make_api_request(request_data):
18-
# Define the API endpoint and the request payload (in JSON format)
19-
print (str.encode(ENCRYPTION_KEY))
2013

21-
# Encrypt the request_data as needed with the INITIALIZATION_VECTOR and ENCRYPTION_KEY
22-
cipher = AES.new(str.encode(ENCRYPTION_KEY), AES.MODE_CBC, iv=str.encode(INITIALIZATION_VECTOR))
23-
encrypted = cipher.encrypt(request_data)
2414

15+
def make_api_request(request_data):
2516

2617
# Make an HTTP POST request to the API
27-
url = f"{BASE_URL}/api/serverless"
18+
url = f"{BASE_URL}/api/serverless/questions"
2819
try:
29-
response = requests.post(url, data=encrypted)
20+
response = requests.post(url, json=request_data, headers={"PASSWORD_HEADER": PASSWORD_HEADER})
3021
response.raise_for_status() # Raise an exception for HTTP errors (4xx and 5xx)
3122

3223
# Handle the API response here
@@ -96,11 +87,7 @@ def send_to_questions_service(qn):
9687
data['difficulty'] = qn["difficulty"]
9788
data['question'] = qn["question"]
9889

99-
json_data = json.dumps(data)
100-
print(json_data)
101-
response = make_api_request(json_data)
102-
103-
print(f"How send ah...")
90+
response = make_api_request(data)
10491

10592
problems = load_problems()
10693
problems = [parse(qn) for qn in problems]

0 commit comments

Comments
 (0)