This repository was archived by the owner on Feb 13, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathwebui.js
More file actions
116 lines (94 loc) · 3.07 KB
/
Copy pathwebui.js
File metadata and controls
116 lines (94 loc) · 3.07 KB
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
/**
* Module dependencies.
*/
var express = require('express'),
routes = require('./routes'),
api = require('./routes/api'),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
config = require('./config').webui;
function findById(id, fn) {
var idx = id - 1;
if (config.users[idx]) {
fn(null, config.users[idx]);
} else {
fn(new Error('User ' + id + ' does not exist'));
}
}
function findByUsername(username, fn) {
for (var i = 0, len = config.users.length; i < len; i++) {
var user = config.users[i];
if (user.username === username) {
return fn(null, user);
}
}
return fn(null, null);
}
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
console.log('Unauthorized action!');
res.status(401).end();
}
function WebUI(hub) {
var self = this;
self.hub = hub;
// Set up logging in with passport
passport.serializeUser(function (user, done) { done(null, user.id); });
passport.deserializeUser(function (id, done) {
findById(id, function (err, user) { done(err, user); });
});
passport.use(new LocalStrategy(
function verify(username, password, done) {
findByUsername(username, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (user.password != password) { return done(null, false); }
return done(null, user);
});
}
));
var app = self.app = express();
// Configuration
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(express.cookieSession({secret: config.secret}));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.locals.pretty = true;
});
app.configure('production', function() {
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.post('/login', function(req, res, next) {
passport.authenticate('local', function (err, user, info) {
if (err) { res.status(500).end(); return; }
if (!user) { res.status(403).end(); return; } // IMPORTANT: Can't use 401 Unauthorized.
req.logIn(user, function (err) {
if (err) { res.status(500).end(); return; }
res.status(200).end();
});
})(req, res, next);
});
// JSON API
app.post('/api/broadcast', ensureAuthenticated, api.broadcast(hub));
app.get('/api/outputs', api.outputs(hub))
app.get('/api/messages/:count', api.messages(hub));
// redirect all others to the index (HTML5 history)
app.get('*', routes.index);
// Start server
app.listen(config.port, function () {
//console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
}
module.exports = WebUI;