forked from keaaa/radix-example-front-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (71 loc) · 2.21 KB
/
index.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use strict";
const fetch = require("node-fetch");
const express = require("express");
const PORT = process.env.PORT || 8002;
const HOST = process.env.HOST || "0.0.0.0";
const app = express();
const jwt = require("jsonwebtoken");
const azureADPublicKey = [];
const resourceID = process.env.API_RESOURCE_ID;
// get public keys used for signing tokens from azure ad
const getADPublicKeys = async url => {
try {
const response = await fetch(url);
const json = await response.json();
json.keys.forEach(key => {
azureADPublicKey[
key.kid
] = `-----BEGIN CERTIFICATE-----\n${key.x5c}\n-----END CERTIFICATE-----`;
});
} catch (error) {
console.log(error);
}
};
/**
* authorize request using Authorization header, expecting a Bearer token
* req - request Request<Dictionary<string>>
* [roles] - array of roles. If empty skip check. The token is authorized if it has any of the roles
* returns - isAuthorized = true/false.
*/
const isAuthorized = (req, roles) => {
let token = req.header("authorization").replace("Bearer ", "");
let isAuthorized = false;
try {
const decodedToken = jwt.decode(token, { complete: true });
const publicKey = azureADPublicKey[decodedToken.header.kid];
const validatedToken = jwt.verify(token, publicKey, {
audience: resourceID
});
if (roles && roles.length > 0) {
isAuthorized =
validatedToken.roles &&
roles.some(role =>
validatedToken.roles.some(userRole => userRole === role)
);
} else {
isAuthorized = true;
}
} catch (err) {
console.log(err);
}
return isAuthorized;
};
// Generic request handler
app.get("*", (req, res) => {
console.log(`Request received by the API: ${req.method} ${req.originalUrl}`);
// if (!isAuthorized(req, ["Radix"])){
if (!isAuthorized(req, [])) {
res.sendStatus(403);
return;
}
let output = `
Request received by the API: ${req.method} ${req.originalUrl}
Headers: ${JSON.stringify(req.headers, null, 2)}
`;
res.send(output);
});
// get public keys used for signing tokens from azure ad
getADPublicKeys(process.env.AZURE_AD_PUBLIC_KEY_URL);
// Start server
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);