-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
133 lines (127 loc) · 5.63 KB
/
utils.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const { isValidObjectId } = require("mongoose");
const User = require("./models/User");
const jwt = require("jsonwebtoken");
require("dotenv").config();
const JWT_SECRET_KEY = `${process.env.DEMO1_JWT_SECRET_KEY}`;
module.exports.isValidUser = async (user) => {
let { _id } = user;
if (!isValidObjectId(_id)) {
return false;
}
const foundUser = await User.findById(_id);
return foundUser;
};
module.exports.createToken = async (user) => {
return jwt.sign(user.toObject(), JWT_SECRET_KEY);
};
module.exports.verifyToken = async (req, res, next) => {
let log = { name: "verifyToken", variables: {} };
req.log.middlewares.push(log);
let token = req.headers.authorization;
log.variables["token"] = token;
if (typeof token !== "string" || token.length < 8) {
log.variables["message"] = "401 No Token Found. Authentication Failed.";
req.log = { ...req.log, statusCode: 401, level: "error" };
return res.status(401).send({ error: "Access denied. Please provide valid credentials." });
}
token = token.slice(7, token.length);
log.variables["token"] = token;
jwt.verify(token, JWT_SECRET_KEY, async function (err, decodedToken) {
log.variables["err"] = err;
log.variables["decodedToken"] = decodedToken;
if (err) {
log.variables["message"] = "401 Token Verification Failed. Authentication Failed.";
req.log = { ...req.log, statusCode: 401, level: "error" };
return res.status(401).send({ error: "Access denied. Please provide valid credentials." });
} else {
let isValidUser = await exports.isValidUser(decodedToken);
log.variables["isValidUser"] = isValidUser;
if (isValidUser) {
log.variables["message"] = "Token Verification Successful. User Verification Successful.";
req.user = decodedToken;
next();
} else {
log.variables["message"] = "403 Token Verification Successful. User Verification Failed.";
req.log = { ...req.log, statusCode: 403, level: "error" };
return res.status(403).send({ error: "You do not have permission to access this resource." });
}
}
});
};
module.exports.decodeTokenIfItExists = async (req, res, next) => {
let log = { name: "decodeTokenIfItExists", variables: {} };
req.log.middlewares.push(log);
let token = req.headers.authorization;
log.variables["token"] = token;
if (typeof token !== "string" || token.length < 8) {
log.variables["message"] = "No Token Found. Authentication Failed.";
next();
}
token = token.slice(7, token.length);
log.variables["token"] = token;
jwt.verify(token, JWT_SECRET_KEY, async function (err, decodedToken) {
log.variables["err"] = err;
log.variables["decodedToken"] = decodedToken;
if (err) {
log.variables["message"] = "Token Verification Failed. Authentication Failed.";
next();
} else {
let isValidUser = await exports.isValidUser(decodedToken);
log.variables["isValidUser"] = isValidUser;
if (isValidUser) {
log.variables["message"] = "Token Verification Successful. User Verification Successful.";
req.user = decodedToken;
next();
} else {
log.variables["message"] = "Token Verification Successful. User Verification Failed.";
next();
}
}
});
};
module.exports.verifyAdminToken = async (req, res, next) => {
let log = { name: "verifyAdminToken", variables: {} };
req.log.middlewares.push(log);
let isAdmin = req.user.isAdmin;
log.variables["isAdmin"] = isAdmin;
if (isAdmin) {
log.variables["message"] = "Token Verification Successful. Admin Access Granted.";
next();
} else {
log.variables["message"] = "Token Verification Successful. Admin Verification Failed.";
req.log = { ...req.log, statusCode: 403, level: "error" };
return res.status(403).send({ error: "You do not have permission to access this resource." });
}
};
module.exports.verifyNoToken = async (req, res, next) => {
let log = { name: "verifyNoToken", variables: {} };
req.log.middlewares.push(log);
let token = req.headers.authorization;
log.variables["token"] = token;
if (typeof token === "string" && token.length > 7) {
token = token.slice(7, token.length);
log.variables["token"] = token;
jwt.verify(token, JWT_SECRET_KEY, async function (err, decodedToken) {
log.variables["err"] = err;
log.variables["decodedToken"] = decodedToken;
if (err) {
log.variables["message"] = "Token Verification Failed. Access Granted.";
next();
} else {
let isValidUser = await exports.isValidUser(decodedToken);
log.variables["isValidUser"] = isValidUser;
if (isValidUser) {
log.variables["message"] = "403 Token Verification Successful. Access Denied.";
req.log = { ...req.log, statusCode: 403, level: "error" };
return res.status(403).send({ error: "You do not have permission to access this resource." });
} else {
log.variables["message"] = "Token Verification Successful. User Verification Failed. Access Granted.";
next();
}
}
});
} else {
log.variables["message"] = "No Token Found. User Verification Failed. Access Granted.";
next();
}
};