Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the ability to set global password with http basic auth #255

Merged
merged 2 commits into from
Dec 4, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ async.waterfall([
var httpLogger = log4js.getLogger("http");
app.configure(function()
{
// Activate http basic auth if it has been defined in settings.json
if(settings.httpAuth != null) app.use(basic_auth);

// If the log level specified in the config file is WARN or ERROR the application server never starts listening to requests as reported in issue #158.
// Not installing the log4js connect logger when the log level has a higher severity than INFO since it would not log at that level anyway.
if (!(settings.loglevel === "WARN" || settings.loglevel == "ERROR"))
Expand Down Expand Up @@ -143,6 +146,26 @@ async.waterfall([
}
});
}

//checks for basic http auth
function basic_auth (req, res, next) {
if (req.headers.authorization && req.headers.authorization.search('Basic ') === 0) {
// fetch login and password
if (new Buffer(req.headers.authorization.split(' ')[1], 'base64').toString() == settings.httpAuth) {
next();
return;
}
}

res.header('WWW-Authenticate', 'Basic realm="Protected Area"');
if (req.headers.authorization) {
setTimeout(function () {
res.send('Authentication required', 401);
}, 5000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a timeout of 5 seconds?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea, new to node.js and express, found the example on : http://node-js.ru/3-writing-express-middleware

would it need to be removed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea was to slow down a brute force attack. I reduced it to 1s now

} else {
res.send('Authentication required', 401);
}
}

//serve read only pad
app.get('/ro/:id', function(req, res)
Expand Down
5 changes: 5 additions & 0 deletions node/utils/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ exports.abiword = null;
*/
exports.loglevel = "INFO";

/**
* Http basic auth, with "user:password" format
*/
exports.httpAuth = null;

//read the settings sync
var settingsStr = fs.readFileSync("../settings.json").toString();

Expand Down
3 changes: 3 additions & 0 deletions settings.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
Abiword is needed to enable the import/export of pads*/
"abiword" : null,

/* This setting is used if you need http basic auth */
// "httpAuth" : "user:pass",

/* The log level we are using, can be: DEBUG, INFO, WARN, ERROR */
"loglevel": "INFO"
}