-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
50 lines (46 loc) · 1.66 KB
/
auth.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
const fs = require('fs');
const path = require('path');
module.exports.auth = class {
logger(req) {
console.log(new Date().toISOString()+": "+req.method+
" "+req.originalUrl+" - "+req.user+"@"+req.ip);
}
constructor(logger){
if(logger){this.logger = logger;}
this.apiKeys = JSON.parse(fs.readFileSync(path.join(__dirname, "api_keys.json")));
}
mw(requiredPermissions){
return (req, res, next) => {
let authSuccess = (index) => {
req.user = this.apiKeys[index].owner;
if(this.logger){this.logger(req);}
return next();
};
//Iterate over the API keys to identify who is making the request
for(let i = 0; i < this.apiKeys.length; i++){
//If we have the person
if(this.apiKeys[i].key == req.query.api_key) {
var userPermissions = this.apiKeys[i].permissions;
//If the person has master permissions, give them instant access
if(userPermissions.includes("full")){
return authSuccess(i);
}
//Loop through the required permissions to see if they have ALL of the required permissions
for(let j = 0; j < requiredPermissions.length; j++){
var found = false;
for(let k = 0; k < userPermissions.length; k++){
if(userPermissions[k] === requiredPermissions[j]){
found = true;
break;
}
}
//If they are missing any permissions, reject them
if(!found) return res.status(401).send("Unauthorized");
}
return authSuccess(i);
}
}
return res.status(401).send("Unauthorized");
};
}
}