-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented timeout and default error page
- Loading branch information
Carlos Rodriguez
committed
Jul 21, 2012
1 parent
4009f8d
commit fbb23a5
Showing
2 changed files
with
17 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
|
||
module.exports = function(options) { | ||
if (typeof options.timeout === 'undefined') { | ||
options.timeout = 2000; | ||
} | ||
function accessDenied(res) { | ||
res.writeHead(401, {'WWW-Authenticate': 'Basic realm="' + options.realm + '"'}); | ||
res.write(options.errorContent || '<html><head><title>Access Denied</title></head><body><h1>Access Denied</h1></body></html>'); | ||
res.end(); | ||
} | ||
return function tinyAuth(req, res, next) { | ||
if (req.headers['authorization'] && req.headers['authorization'].indexOf('Basic ') === 0) { | ||
if (options.accounts.indexOf(new Buffer(req.headers['authorization'].split(' ')[1], 'base64').toString()) !== -1) { | ||
next(); | ||
return; | ||
} | ||
else { | ||
// A timeout makes it slightly harder to brute-force. | ||
setTimeout(accessDenied.bind(null, res), options.timeout); | ||
} | ||
} | ||
else { | ||
// Immediate prompt. | ||
accessDenied(res); | ||
} | ||
|
||
res.writeHead(401, {'WWW-Authenticate': 'Basic realm="' + options.realm + '"'}); | ||
res.end(); | ||
}; | ||
}; |