-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (118 loc) · 4.66 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
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
134
135
const JWT = require("jsonwebtoken");
const errorMessages = require("./responses/errorMessages");
let blacklistCache = [];
module.exports.ensureAuth = async (req, res, next) => {
const jwt = req.headers[this.config.JwtHeaderKeyName];
if (blacklistCache.includes(jwt)) return errorMessages.blacklisted(res);
JWT.verify(jwt, this.config.secret, (err, decoded) => {
if (err || decoded.hasOwnProperty("isRefresh")) {
return errorMessages.invalidJwt(res);
}
req.session = decoded;
next();
});
};
module.exports.importConfig = (configJson) => {
this.config = configJson;
};
/**
* @function settings - Use this function to setup the module
* @param {String} secret - Secret used for jwt
* @param {String} jwtHeaderKeyName - Name of the header key used to store the jwt
*/
module.exports.settings = (secret, jwtHeaderKeyName = "jwt") => {
if (!secret) throw "secret is required in settings function";
if (typeof secret !== "string") throw "secret must be a string";
if (typeof jwtHeaderKeyName !== "string") throw "jwtHeaderKeyName must be a string";
if (!this.config) this.config = {};
this.config.secret = secret;
this.config.JwtHeaderKeyName = jwtHeaderKeyName;
};
/**
* @function newSession - Create new session (generate jwt and refreshToken) and save refereshToken in a cookie
* @param {Object} objData - Data to save in jwt
* @param {Object} res - Express response, used to set refresh cookie
* @param {String} role - Authorization level, listed in config file (only if you are using middleware)
* @param {function (Object, Object)} [callback] - Optional function, called after calculating jwt and refreshToken
* @returns {Promise} Return a promise with jwtToken and refreshToken as a Object
*/
module.exports.newSessionInCookies = async (objData, res, role = null, callback = null) => {
let sessionData = await this.newSession(objData, role, callback);
if (res)
res.cookie("refresh", sessionData.refreshToken, {
maxAge: 90000000,
httpOnly: true,
secure: true,
});
return sessionData;
};
module.exports.newSession = (objData, role = null, callback = null) => {
return new Promise((resolve) => {
Object.assign(objData, { role });
JWT.sign(objData, this.config.secret, { expiresIn: 400 }, (err, jwt) => {
Object.assign(objData, { isRefresh: true });
JWT.sign(objData, this.config.secret, (err, refreshToken) => {
if (callback) callback(jwt, refreshToken);
resolve({ jwt, refreshToken });
});
});
});
};
/**
* @function refresh - Recreate jwt if there is a valid refreshToken in req as cookie
* @param {Object} req - Express request
* @param {function (Object)} [callback] - Optional function, called after calculating jwt
* @returns {Promise} - Return a promise with jwtToken
*/
module.exports.refreshFromCookie = (req, callback) => {
return this.refresh(req.cookies.refresh, callback);
};
/**
* @function refresh - Recreate jwt if the refreshToken is valid
* @param {function (Object)} [callback] - Optional function, called after calculating jwt
* @returns {Promise} - Return a promise with jwtToken
*/
module.exports.refresh = (refreshToken, callback) => {
return new Promise((resolve, reject) => {
JWT.verify(refreshToken, this.config.secret, (err, obj) => {
if (err || !obj.hasOwnProperty("isRefresh")) {
if (callback) callback(err);
reject(err);
} else {
delete obj.isRefresh;
delete obj.iat;
JWT.sign(obj, this.config.secret, { expiresIn: 400 }, (err, jwt) => {
if (callback) callback(jwt);
resolve(jwt);
});
}
});
});
};
/**
* @function blacklist - blacklist an jwt if blacklisting is enable in config
* @param {String} jwt - Jwt to blacklist
*/
module.exports.blacklist = (jwt) => {
blacklistCache.push(jwt);
};
module.exports.deleteRefresh = (res) => {
res.clearCookie("refresh");
};
module.exports.hasRole = (role) => {
return (req, res, next) => {
if (req.session.role) return next();
errorMessages.invalidRole(res);
};
};
//function that remove expired jwt from blacklistcache every 2 minute
let intervalID = setInterval(() => {
for (let index = 0; index < blacklistCache.length; index++) {
const element = blacklistCache[parseInt(index, 10)];
JWT.verify(element, this.config.secret, (err, obj) => {
if (err) {
blacklistCache.splice(index, 1);
}
});
}
}, 2 * 60 * 60);