mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Cache auth details to save needlessly recalculating hashes
This commit is contained in:
parent
1324f5e59c
commit
939768eec0
17
red.js
17
red.js
@ -196,6 +196,7 @@ try {
|
||||
function basicAuthMiddleware(user,pass) {
|
||||
var basicAuth = require('basic-auth');
|
||||
var checkPassword;
|
||||
var localCachedPassword;
|
||||
if (pass.length == "32") {
|
||||
// Assume its a legacy md5 password
|
||||
checkPassword = function(p) {
|
||||
@ -207,12 +208,26 @@ function basicAuthMiddleware(user,pass) {
|
||||
}
|
||||
}
|
||||
|
||||
var checkPasswordAndCache = function(p) {
|
||||
// For BasicAuth routes we know the password cannot change without
|
||||
// a restart of Node-RED. This means we can cache the provided crypted
|
||||
// version to save recalculating each time.
|
||||
if (localCachedPassword === p) {
|
||||
return true;
|
||||
}
|
||||
var result = checkPassword(p);
|
||||
if (result) {
|
||||
localCachedPassword = p;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return function(req,res,next) {
|
||||
if (req.method === 'OPTIONS') {
|
||||
return next();
|
||||
}
|
||||
var requestUser = basicAuth(req);
|
||||
if (!requestUser || requestUser.name !== user || !checkPassword(requestUser.pass)) {
|
||||
if (!requestUser || requestUser.name !== user || !checkPasswordAndCache(requestUser.pass)) {
|
||||
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user