-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
116 lines (103 loc) · 2.92 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
const jwt = require("jsonwebtoken");
const cookieParser = require("cookie-parser"); // Allows for parsing req.cookies & res.cookie(...)
const DEFAULT_TIMEOUT_DURATION = "2 hours";
class JwtInCookie {
#secret;
#timeoutDuration;
#tokenKey;
constructor(secret, timeoutDuration) {
this.#secret = secret;
this.#timeoutDuration = timeoutDuration;
this.#tokenKey = "jic"; // Default cookie key for JWT
}
getTokenKey() {
return this.#tokenKey;
}
getTimeout() {
return this.#timeoutDuration
}
getSecret() {
return this.#secret;
}
}
// Define a default instance cookie. Will be set in the configure method
let instance = new JwtInCookie("", DEFAULT_TIMEOUT_DURATION);
/**
* Configures the instance w/ a secret and optional timeout duration
*
* @param config, { secret: "", timeoutDuration: "" }
*/
exports.configure = function (config) {
const secret = config["secret"];
let timeout = config["timeoutDuration"] || DEFAULT_TIMEOUT_DURATION;
if (!secret) {
throw new Error("Secret must be specified");
}
instance = new JwtInCookie(secret, timeout);
};
/**
* Adds JWT token to the express response's cookie
*
* @param res
*/
exports.setJwtToken = function (res, payload, cookieOptions = {httpOnly: true, expires: 0}) {
const encodedToken = encodePayload(payload);
res.cookie(instance.getTokenKey(), encodedToken, cookieOptions);
return res;
};
/**
* Removes the JWT token from the express response's cookie. Used for log-out
*
* @param res
* @param payload
* @param cookieOptions
* @returns {*}
*/
exports.clearToken = function (res, cookieOptions = {httpOnly: true, expires: 0}) {
res.cookie(instance.getTokenKey(), "", cookieOptions);
return res;
};
/**
* Encodes input payload as token
*
* @param payload
* @returns {undefined|*}
*/
const encodePayload = function (payload) {
const jwtData = {
expiresIn: instance.getTimeout()
};
const encoded = jwt.sign(payload, instance.getSecret(), jwtData);
return encoded;
};
exports.encodePayload = encodePayload;
/**
* Retrieves token from the request cookie
*
* @param req - express request
* @returns {*}
*/
const retrieveTokenFromCookie = function (req) {
const token = req.cookies[instance.getTokenKey()];
if (token === undefined || token === null) {
throw new Error("JWT Token not defined in cookie");
}
const decodedToken = jwt.verify(token, instance.getSecret(), function (err, decoded) {
if (err) {
throw new Error("Invalid JWT Token");
}
return decoded
});
return decodedToken;
};
exports.retrieveTokenFromCookie = retrieveTokenFromCookie;
/**
* Returns decoded token if the request has a valid token in its cookie
*
* @param req
* @returns {*}
*/
exports.validateJwtToken = function (req) {
const decodedToken = retrieveTokenFromCookie(req);
return decodedToken;
};