Skip to content

Commit 939768e

Browse files
committed
Cache auth details to save needlessly recalculating hashes
1 parent 1324f5e commit 939768e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

red.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ try {
196196
function basicAuthMiddleware(user,pass) {
197197
var basicAuth = require('basic-auth');
198198
var checkPassword;
199+
var localCachedPassword;
199200
if (pass.length == "32") {
200201
// Assume its a legacy md5 password
201202
checkPassword = function(p) {
@@ -207,12 +208,26 @@ function basicAuthMiddleware(user,pass) {
207208
}
208209
}
209210

211+
var checkPasswordAndCache = function(p) {
212+
// For BasicAuth routes we know the password cannot change without
213+
// a restart of Node-RED. This means we can cache the provided crypted
214+
// version to save recalculating each time.
215+
if (localCachedPassword === p) {
216+
return true;
217+
}
218+
var result = checkPassword(p);
219+
if (result) {
220+
localCachedPassword = p;
221+
}
222+
return result;
223+
}
224+
210225
return function(req,res,next) {
211226
if (req.method === 'OPTIONS') {
212227
return next();
213228
}
214229
var requestUser = basicAuth(req);
215-
if (!requestUser || requestUser.name !== user || !checkPassword(requestUser.pass)) {
230+
if (!requestUser || requestUser.name !== user || !checkPasswordAndCache(requestUser.pass)) {
216231
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
217232
return res.sendStatus(401);
218233
}

0 commit comments

Comments
 (0)