-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
65 lines (50 loc) · 1.72 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import express from "express";
import jwt from "jsonwebtoken";
import { JwtPayload, SecretCollection } from "./types";
const HOST = process.env.HOST ?? "0.0.0.0";
const PORT = process.env.PORT ?? 8080;
const SECRET = process.env.SECRET ?? "APP_SECRET";
const getSecrets = (secretRequest: JwtPayload): SecretCollection => {
const { projectId, nfObjectId, nfObjectType } = secretRequest;
return {
secrets: [
{
type: "FILE",
path: "/test-file",
data: `projectId ${projectId}; nfObjectId: ${nfObjectId}; nfObjectType: ${nfObjectType}`,
},
{ type: "ENV", key: "SECRET_ENV_1", value: "VALUE_1" },
{ type: "ENV", key: "SECRET_ENV_2", value: "VALUE_2" },
{ type: "ENV", key: "PROJECT_ID", value: projectId },
{ type: "ENV", key: "OBJECT_ID", value: nfObjectId },
{ type: "ENV", key: "OBJECT_TYPE", value: nfObjectType },
],
};
};
(async () => {
const app = express();
app.get("/", (req, res) => {
console.log();
console.log("Incoming request");
const authorization = req.header("Authorization");
if (!authorization) {
console.log("Unauthorized");
return res.status(401).send("Unauthorized");
}
const token = authorization.replace("Bearer ", "");
let payload: JwtPayload;
try {
payload = jwt.verify(token, SECRET) as JwtPayload;
} catch (e) {
console.log(`Unauthorized ${e}`);
return res.status(401).send("Unauthorized");
}
console.log(`Authorized`);
console.log("Payload", payload);
const secrets = getSecrets(payload);
return res.send(JSON.stringify(secrets));
});
app.listen(Number(PORT), HOST, () => {
console.log(`Server listening at http://${HOST}:${PORT}`);
});
})();