@@ -196,6 +196,7 @@ try {
196
196
function basicAuthMiddleware ( user , pass ) {
197
197
var basicAuth = require ( 'basic-auth' ) ;
198
198
var checkPassword ;
199
+ var localCachedPassword ;
199
200
if ( pass . length == "32" ) {
200
201
// Assume its a legacy md5 password
201
202
checkPassword = function ( p ) {
@@ -207,12 +208,26 @@ function basicAuthMiddleware(user,pass) {
207
208
}
208
209
}
209
210
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
+
210
225
return function ( req , res , next ) {
211
226
if ( req . method === 'OPTIONS' ) {
212
227
return next ( ) ;
213
228
}
214
229
var requestUser = basicAuth ( req ) ;
215
- if ( ! requestUser || requestUser . name !== user || ! checkPassword ( requestUser . pass ) ) {
230
+ if ( ! requestUser || requestUser . name !== user || ! checkPasswordAndCache ( requestUser . pass ) ) {
216
231
res . set ( 'WWW-Authenticate' , 'Basic realm=Authorization Required' ) ;
217
232
return res . sendStatus ( 401 ) ;
218
233
}
0 commit comments