-
Notifications
You must be signed in to change notification settings - Fork 52
/
app.js
178 lines (146 loc) · 4.83 KB
/
app.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var conf = require('./lib/config');
var fs = require('fs');
var http = require('http');
var https = require('https');
var express = require('express');
var bodyParser = require('body-parser');
var session = require('cookie-session');
var cookieParser = require('cookie-parser');
var flash = require('express-flash');
var everyauth = require('everyauth');
var Proxy = require('./lib/proxy');
var tls = require('./middlewares/tls');
log = require('./lib/log');
var proxy = new Proxy(conf.proxyTo.host, conf.proxyTo.port);
var proxyMiddleware = proxy.middleware();
// Set up our auth strategies
if (conf.modules.github) {
var github = require('./lib/modules/github');
github.setup(everyauth);
}
if (conf.modules.google) {
var google = require('./lib/modules/google');
google.setup(everyauth);
}
if(conf.modules.password) {
var password = require('./lib/modules/password');
password.setup(everyauth);
}
function userCanAccess(req) {
var auth = req.session && req.session.auth;
if(!auth) {
log.info("User rejected because they haven't authenticated.");
return false;
}
for(var authType in auth) {
if(everyauth[authType] && everyauth[authType].authorize(auth)) {
everyauth[authType].decorate(req, auth);
return true;
}
}
return false;
}
function isPublicPath(req) {
if(!conf.publicPaths) { return false; }
for(var i = 0, len = conf.publicPaths.length; i < len; i++) {
var path = conf.publicPaths[i];
if(typeof(path) == 'object') { // regex
if(req.url.match(path)) { return true; }
} else {
if(req.url.indexOf(path) == 0) { return true; }
}
}
return false;
}
function checkUser(req, res, next) {
// /_doorman requests never get proxied
if(req.url.indexOf('/_doorman') == 0) { return next(); }
if(userCanAccess(req) || isPublicPath(req)) {
proxyMiddleware(req, res, next);
} else {
if(req.session && req.session.auth) {
// User had an auth, but it wasn't an acceptable one
req.session.auth = null;
log.info("User successfully oauthed but their account does not meet the configured criteria.");
req.flash('error', "Sorry, your account is not authorized to access the system.");
}
next();
}
}
function loginPage(req, res, next) {
if(req.url.indexOf("/_doorman/logout") == 0) {
if(req && req.session) { req.session.auth = null; }
res.redirect("/");
return;
}
if(req.query.error) {
req.flash('error', "The authentication method reports: " + req.query.error_description);
}
req.session.redirectTo = req.originalUrl;
res.render('login.jade', { pageTitle: 'Login', providers: everyauth.enabled });
}
// Store the middleware since we use it in the websocket proxy
var sessionOptions = conf.sessionCookie || {
maxage: conf.sessionCookieMaxAge,
domain: conf.sessionCookieDomain,
secureProxy: conf.sessionSecureProxy,
secret: conf.sessionSecret,
name: '__doorman',
};
var doormanSession = session(sessionOptions);
var logMiddleware = function(req, res, next) {
log.info([req.method, req.headers.host, req.url].join(' '));
next();
};
var app = express();
app.use(logMiddleware);
app.use(tls);
app.use(cookieParser(conf.sessionSecret));
app.use(doormanSession);
app.use(flash());
app.use(checkUser);
app.use(bodyParser.urlencoded({extended: false}));
app.use(everyauth.middleware());
app.use(express.static(__dirname + "/public", {maxAge: 0 }));
app.use(loginPage);
// Uncaught error states
app.on('error', function(err) {
log.error(err);
});
everyauth.everymodule.moduleErrback(function(err, data) {
data.req.flash('error', "Perhaps something is misconfigured, or the provider is down.");
data.res.redirectTo('/');
});
// We don't actually use this
everyauth.everymodule.findUserById(function(userId, callback) { callback(userId); });
// WebSockets are also authenticated
function upgradeWebsocket(server) {
server.on('upgrade', function(req, socket, head) {
doormanSession(req, new http.ServerResponse(req), function() {
if(userCanAccess(req)) {
proxy.proxyWebSocketRequest(req, socket, head);
} else {
socket.destroy();
}
});
});
}
var notice = "Doorman on duty,";
var httpServer = http.createServer(app);
// Enable HTTPS if SSL options exist
if (conf.securePort && conf.ssl && conf.ssl.keyFile && conf.ssl.certFile) {
var options = {
key: fs.readFileSync(conf.ssl.keyFile),
cert: fs.readFileSync(conf.ssl.certFile)
};
if (conf.ssl.caFile) options.ca = fs.readFileSync(conf.ssl.caFile);
var httpsServer = https.createServer(options, app);
upgradeWebsocket(httpsServer);
httpsServer.listen(conf.securePort);
notice += " listening on secure port " + conf.securePort;
}
notice += " listening on port " + conf.port;
notice += " and proxying to " + conf.proxyTo.host + ":" + conf.proxyTo.port + ".";
upgradeWebsocket(httpServer);
httpServer.listen(conf.port);
log.error(notice);