Skip to content

Commit

Permalink
Add HTTP Basic Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
bencevans committed Apr 1, 2014
1 parent 3cd847a commit 694b527
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ app.models = {
//
app.controllers = {
jobs: require('./lib/controllers/jobs')(app),
site: require('./lib/controllers/site')()
site: require('./lib/controllers/site')(),
auth: require('./lib/controllers/auth')(config)
};


Expand All @@ -92,6 +93,8 @@ app.controllers = {
/* API routes */
////////////////

app.use(app.controllers.auth.isAuth);

app.use(connectRoute(function (router) {

router.get('/', app.controllers.site.index);
Expand Down
39 changes: 39 additions & 0 deletions lib/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

'use strict';

var auth = require('basic-auth');


// Loads the controller
//
// @app {Object} The config
//
function main (config) {

return {

isAuth: function(req, res, next) {

if(!config.auth) {
return next();
}

var user = auth(req);

if(
config.auth &&
user &&
config.auth.username === user.name &&
config.auth.password === user.pass
) {
return next();
}

res.json(500, { message: 'Permission Denied' });
}

}

}

module.exports = main;
1 change: 0 additions & 1 deletion lib/controllers/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ function main () {
// GET /
//
index: function (req,res) {
console.log();
readFileFromCache(__dirname + '/../views/site/index.html', function(err, data){
if (err) {
res.json(500, err);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"lodash": "~2.0.0",
"mongoose": "~3.6.19",
"request": "~2.27.0",
"optimist": "~0.6.0"
"optimist": "~0.6.0",
"basic-auth": "0.0.1"
},
"bin": "bin/jobukyu"
}

0 comments on commit 694b527

Please sign in to comment.