-
Notifications
You must be signed in to change notification settings - Fork 12
/
users.js
167 lines (138 loc) · 4.35 KB
/
users.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var path = require('path');
var jwt = require('jsonwebtoken');
var cookie = require('cookie');
var crypto = require('crypto');
var bodyParser = require('body-parser');
var APP_DIR = path.join(__dirname, './app');
var JWT_COOKIE_EXPIRY = 604800000; // 7 days
var log,
redSettings,
usersConfig;
function getTokenFromRequest(req) {
var header = req.headers.cookie;
// read from cookie header
if (header) {
var cookies = cookie.parse(header);
return cookies[usersConfig.jwtCookieName];
}
}
function verifyJwt(req) {
var jwtCookie = getTokenFromRequest(req);
if (!jwtCookie) {
log.trace("Node users: jwt cookie not found");
return false;
}
try {
return jwt.verify(jwtCookie, usersConfig.credentials.jwtSecret);
} catch (err) {
log.trace("Node users: Failed to verify jwt - " + err);
return false;
}
}
function createJwtToken(req, res, jwtSecret, jwtCookieName, payload) {
var token = jwt.sign(payload, jwtSecret);
res.cookie(jwtCookieName, token, {
maxAge: JWT_COOKIE_EXPIRY,
secure: usersConfig.jwtHttpsOnly === true
});
}
function clearJwt(res) {
res.clearCookie(usersConfig.jwtCookieName);
}
function hash(username, password) {
// username is used as part of the salt for the hash
var hash = crypto.createHash('sha512').update(password+"."+username, 'utf8').digest('hex');
return hash;
}
function getUser(username, password) {
var user = usersConfig.credentials.nodeUsers.filter(function (u) {
return u.username === username && u.password === hash(username, password);
})[0];
return user;
}
function handleLogin(req, res) {
if (!usersConfig || !usersConfig.credentials || !usersConfig.credentials.jwtSecret) {
log.error("Node users: missing or incomplete users config");
return res.status(503).send("Node users not initialized");
}
var username = req.body.username;
var password = req.body.password;
var user = getUser(username, password);
if (!user) {
res.status(401).send('Unauthorized');
return;
}
log.debug('Authenticated node user:'+user.username);
createJwtToken(req, res, usersConfig.credentials.jwtSecret, usersConfig.jwtCookieName, {
username: user.username,
scope: user.scope
});
res.status(204).send();
}
function handleLogout(req, res) {
var returnUrl = req.query.return;
clearJwt(res);
if (returnUrl) {
res.status(301).redirect(returnUrl);
} else {
returnUrl = redSettings.httpNodeRoot+usersConfig.appPath;
var re = new RegExp('\/{1,}','g');
returnUrl = returnUrl.replace(re,'/');
res.status(301).redirect(returnUrl);
}
}
function appendTrailingSlash(req, res, next) {
if (req.originalUrl.slice(-1) !== '/') {
res.redirect(req.originalUrl + '/');
return;
}
next();
}
function init(server, app, _log, redSettings) {
log = _log;
if (!usersConfig.appPath) {
log.error("Node users config not initialized");
return;
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get(path.posix.join(usersConfig.appPath, 'static/app.css'), function (req, res) {
res.sendFile(path.join(APP_DIR, 'static', 'app.css'));
});
app.get(path.posix.join(usersConfig.appPath, 'static/jquery.min.js'), function (req, res) {
res.sendFile(path.join(APP_DIR, 'static', 'jquery.min.js'));
});
app.post(usersConfig.appPath, handleLogin);
app.get(path.posix.join(usersConfig.appPath, 'logout'), handleLogout);
app.get(usersConfig.appPath, appendTrailingSlash, function (req, res) {
var payload = verifyJwt(req);
if (payload) {
res.sendFile(path.join(APP_DIR, 'index.html'));
} else {
res.sendFile(path.join(APP_DIR, 'login.html'));
}
});
var fullPath = path.posix.join(redSettings.httpNodeRoot, usersConfig.appPath);
log.info("Node users started " + fullPath);
}
module.exports = {
init: function (RED, _usersConfig) {
usersConfig = _usersConfig;
redSettings = RED.settings;
usersConfig.on("close",function() {
// clean up routes created by this node on close
var node = this;
var routes = RED.httpNode._router.stack;
for(var i=0; i<routes.length; i++) {
var r = routes[i].route;
var rgx = new RegExp("^"+node.appPath);
if (r && rgx.test(r.path)) {
routes.splice(i,1);
i--;
}
}
});
init(RED.server, RED.httpNode, RED.log, RED.settings);
},
verify: verifyJwt
};