-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
50 lines (48 loc) · 1.61 KB
/
Copy pathmiddleware.js
File metadata and controls
50 lines (48 loc) · 1.61 KB
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
const IFTTT_KEY = process.env.IFTTT_KEY;
const IFTTT_STAGING_KEY = process.env.IFTTT_STAGING_KEY;
const IFTTT_API_TOKEN_AUTH_KEY = process.env.IFTTT_API_TOKEN_AUTH_KEY;
const TEST_ACCESS_TOKEN = process.env.TEST_ACCESS_TOKEN;
const FULLSTACK_KEY = process.env.FULLSTACK_KEY;
const AUTH_ALWAYS_FAILS_KEY = process.env.AUTH_ALWAYS_FAILS_KEY;
module.exports = {
serviceKeyCheck: function (req, res, next) {
const key = req.get("IFTTT-Service-Key");
if ([IFTTT_KEY, IFTTT_STAGING_KEY, FULLSTACK_KEY, IFTTT_API_TOKEN_AUTH_KEY].includes(key)) {
next();
} else {
res.status(401).send({
errors: [
{
message: "Unauthorized",
},
],
});
}
},
accessTokenCheck: function (req, res, next) {
console.log('req.get("Authorization")', req.get("Authorization"));
console.log('req.get("Api-Key")', req.get("Api-Key"));
if (req.get("Authorization")) {
if (req.get("Authorization") == "Bearer") {
res.status(401).send({
errors: [{ message: "🔏 Empty token header" }],
});
} else if (req.get("Authorization") !== `Bearer ${TEST_ACCESS_TOKEN}`) {
res.status(401).send({
errors: [{ message: "🔒 Incorrect bearer token" }],
});
}
} else if (req.get("Api-Key")) {
if (!req.get("Api-Key").startsWith(`secret ${TEST_ACCESS_TOKEN}`)) {
res.status(401).send({
errors: [{ message: "🔒 Incorrect API key" }],
});
}
} else {
res.status(401).send({
errors: [{ message: "🔒 Missing authorization header" }],
});
}
next();
},
};