Skip to content

Commit 3dd0877

Browse files
committed
Improve authentication
Support option “redirect”
1 parent 370bfa2 commit 3dd0877

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

lib/configs/default.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ module.exports = function(options) {
3030

3131
// Authentication settings
3232
'auth': {
33-
'basic': true
33+
// Redirect user to this url for auth
34+
'redirect': undefined,
3435
},
3536

3637
// Hooks
38+
// If value is string: POST to the url
39+
// If function: executed
3740
'hooks': {
3841
'users.auth': function(data) {
3942
if (!data.email || !data.token) throw "Need 'token' and 'email' for auth hook";
@@ -59,8 +62,13 @@ module.exports = function(options) {
5962

6063
// Packages
6164
'packages': {
65+
// Path to store all packages for the user
6266
'root': undefined,
67+
68+
// Path to default packages
6369
'defaults': path.resolve(__dirname, "../../packages"),
70+
71+
// Packages to install when booting
6472
'install': {}
6573
}
6674
}, _.defaults);

lib/index.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var fs = require('fs');
77
var http = require('http');
88
var express = require('express');
99
var bodyParser = require('body-parser');
10-
var basicAuth = require('basic-auth-connect');
10+
var basicAuth = require('basic-auth');
1111
var cookieParser = require('cookie-parser');
1212
var session = require('express-session');
1313
var Busboy = require('busboy');
@@ -115,6 +115,8 @@ var start = function(config) {
115115
resave: false,
116116
saveUninitialized: true
117117
}));
118+
119+
// Auth by query strings
118120
app.use("/", function(req, res, next) {
119121
var args = _.extend({}, req.query, req.body);
120122
if (args.email && args.token) {
@@ -128,21 +130,30 @@ var start = function(config) {
128130
}
129131
});
130132

131-
// Static files
132-
app.use('/', express.static(path.resolve(__dirname, '../build')));
133-
134133
// Auth
135134
app.use(function(req, res, next) {
136-
var doAuth = basicAuth(function(user, pass, fn){
137-
users.auth(user, pass)
135+
if (req.session.userId) return next();
136+
137+
var auth = basicAuth(req);
138+
139+
// Do basic auth
140+
if (auth && auth.name && auth.pass) {
141+
users.auth(auth.name, auth.pass, req)
138142
.then(function(user) {
139-
fn(null, user)
143+
req.user = user;
144+
next();
140145
})
141-
.fail(fn);
142-
});
143-
144-
if (req.session.userId || !config.auth.basic) return next();
145-
doAuth(req, res, next);
146+
.fail(next);
147+
} else {
148+
if (config.auth.redirect) {
149+
console.log('no auth, redirect to', config.auth.redirect);
150+
res.redirect(config.auth.redirect);
151+
} else {
152+
res.header('WWW-Authenticate', 'Basic realm="codebox"');
153+
res.status(401);
154+
res.end();
155+
}
156+
}
146157
});
147158
app.use(function(req, res, next) {
148159
if (req.user) {
@@ -166,6 +177,10 @@ var start = function(config) {
166177
}
167178
});
168179

180+
// Static files
181+
app.use('/', express.static(path.resolve(__dirname, '../build')));
182+
183+
169184
// Download packages
170185
app.use('/packages', _middleware(function() {
171186
return express.static(config.packages.root);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"commander": "2.8.0",
4343
"open": "0.0.5",
4444
"ini": "1.2.1",
45-
"basic-auth-connect": "1.0.0",
45+
"basic-auth": "1.0.0",
4646
"mime": "1.3.4",
4747
"busboy": "0.2.9",
4848
"uuid": "2.0.1",

0 commit comments

Comments
 (0)