@@ -7,7 +7,7 @@ var fs = require('fs');
7
7
var http = require ( 'http' ) ;
8
8
var express = require ( 'express' ) ;
9
9
var bodyParser = require ( 'body-parser' ) ;
10
- var basicAuth = require ( 'basic-auth-connect ' ) ;
10
+ var basicAuth = require ( 'basic-auth' ) ;
11
11
var cookieParser = require ( 'cookie-parser' ) ;
12
12
var session = require ( 'express-session' ) ;
13
13
var Busboy = require ( 'busboy' ) ;
@@ -115,6 +115,8 @@ var start = function(config) {
115
115
resave : false ,
116
116
saveUninitialized : true
117
117
} ) ) ;
118
+
119
+ // Auth by query strings
118
120
app . use ( "/" , function ( req , res , next ) {
119
121
var args = _ . extend ( { } , req . query , req . body ) ;
120
122
if ( args . email && args . token ) {
@@ -128,21 +130,30 @@ var start = function(config) {
128
130
}
129
131
} ) ;
130
132
131
- // Static files
132
- app . use ( '/' , express . static ( path . resolve ( __dirname , '../build' ) ) ) ;
133
-
134
133
// Auth
135
134
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 )
138
142
. then ( function ( user ) {
139
- fn ( null , user )
143
+ req . user = user ;
144
+ next ( ) ;
140
145
} )
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
+ }
146
157
} ) ;
147
158
app . use ( function ( req , res , next ) {
148
159
if ( req . user ) {
@@ -166,6 +177,10 @@ var start = function(config) {
166
177
}
167
178
} ) ;
168
179
180
+ // Static files
181
+ app . use ( '/' , express . static ( path . resolve ( __dirname , '../build' ) ) ) ;
182
+
183
+
169
184
// Download packages
170
185
app . use ( '/packages' , _middleware ( function ( ) {
171
186
return express . static ( config . packages . root ) ;
0 commit comments