-
Notifications
You must be signed in to change notification settings - Fork 149
/
express.js
160 lines (127 loc) · 6.27 KB
/
express.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
var express = require('express'),
path = require('path'),
fs = require('fs'),
config = require('./config'),
serveIndex = require('serve-index');
var favicon = require('serve-favicon'), //express middleware
errorHandler = require('errorhandler'),
logger = require('morgan'),
methodOverride = require('method-override'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser');
//CORS middleware , add more controls for security like site names, timeout etc.
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Credentials', true);
res.header('Vary', "Origin"); //https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
res.header('Access-Control-Expose-Headers', 'Content-Length');
res.header('Access-Control-Allow-Methods', 'HEAD,GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length,Response-Type, X-Requested-With,origin,accept,Authorization,x-access-token,Last-Modified');
if (req.method == 'OPTIONS') {
res.sendStatus(200);
}
else {
next();
}
}
var basicHttpAuth = function(req,res,next) {
var auth = req.headers['authorization']; // auth is in base64(username:password) so we need to decode the base64
if(!auth) { // No Authorization header was passed in so it's the first time the browser hit us
// Sending a 401 will require authentication, we need to send the 'WWW-Authenticate' to tell them the sort of authentication to use
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="Secure Area"');
res.end('<html><body>Authentication required to access this path</body></html>');
} else {
var tmp = auth.split(' '); // Split on a space, the original auth looks like "Basic Y2hhcmxlczoxMjM0NQ==" and we need the 2nd part
var buf = Buffer.from(tmp[1], 'base64'); // create a buffer and tell it the data coming in is base64
var plain_auth = buf.toString(); // read it back out as a string
//console.log("Decoded Authorization ", plain_auth);
// At this point plain_auth = "username:password"
var creds = plain_auth.split(':'); // split on a ':'
var username = creds[0];
var password = creds[1];
var pathComponents = req.path.split('/');
//console.log(pathComponents);
require('../app/controllers/licenses').getSettingsModel(function(err,settings){
if( (!settings.authCredentials) ||
(!settings.authCredentials.user || username == settings.authCredentials.user) &&
(!settings.authCredentials.password || password == settings.authCredentials.password)) {
//console.log("http request authorized for download for "+req.path);
next();
} else {
console.log("http request rejected for download for "+req.path);
res.statusCode = 401; // or alternatively just reject them altogether with a 403 Forbidden
res.setHeader('WWW-Authenticate', 'Basic realm="Secure Area"');
res.end('<html><body>Authentication required to access this path</body></html>');
}
})
}
}
module.exports = function (app) {
//CORS related http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
app.use(allowCrossDomain);
if (process.env.NODE_ENV == 'development') {
// Disable caching of scripts for easier testing
app.use(function noCache(req, res, next) {
if (req.url.indexOf('/scripts/') === 0) {
res.header('Cache-Control', 'no-cache, no-store, must-revalidate');
res.header('Pragma', 'no-cache');
res.header('Expires', 0);
}
next();
});
app.use(errorHandler());
app.locals.pretty = true;
app.locals.compileDebug = true;
}
if (process.env.NODE_ENV == 'production') {
app.use(favicon(path.join(config.root, 'public/app/img', 'favicon.ico')));
};
//app.use(auth.connect(digest)); //can specify specific routes for auth also
app.use(basicHttpAuth);
//app.use('/sync_folders',serveIndex(config.syncDir));
app.use('/sync_folders',function(req, res, next){
// Player uses --no-cache header in wget to download assets. The --no-cache flag sends the following headers
// Cache-Control: no-cache , Pragma: no-cache
// This causes 200 OK response for all requests. Hence remove this header to minimise data-transfer costs.
delete req.headers['cache-control']; // delete header
delete req.headers['pragma']; // delete header
fs.stat(path.join(config.syncDir,req.path), function(err, stat){
if (!err && stat.isDirectory()) {
res.setHeader('Last-Modified', (new Date()).toUTCString());
}
next();
})
},
serveIndex(config.syncDir)
);
app.use('/sync_folders',express.static(config.syncDir));
app.use('/releases',express.static(config.releasesDir));
app.use('/licenses',express.static(config.licenseDir));
app.use('/media', express.static(path.join(config.mediaDir)));
app.use(express.static(path.join(config.root, 'public')));
app.set('view engine', 'pug');
app.locals.basedir = config.viewDir; //for jade root
app.set('views', config.viewDir);
//app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride());
app.use(cookieParser());
app.use(require('./routes'));
// custom error handler
app.use(function (err, req, res, next) {
if (err.message.indexOf('not found') >= 0)
return next();
//ignore range error as well
if (err.message.indexOf('Range Not Satisfiable') >=0 )
return res.send();
console.error(err.stack)
res.status(500).render('500')
})
app.use(function (req, res, next) {
//res.redirect('/');
res.status(404).render('404', {url: req.originalUrl})
})
};