-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManagementConsole.js
118 lines (106 loc) · 3.17 KB
/
ManagementConsole.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
// LectureConntect Managment Console
// Web frontend controller to communicate with appengine
// Author: Michael Timbrook <mpt2360@rit.edu>
var express = require('express')
, routes = require('./routes')
, account = require('./routes/account')
, control = require('./routes/control')
, rest = require('./routes/rest')
, https = require('https')
, http = require('http')
, path = require('path')
, crypto = require('crypto')
, fs = require('fs')
, passport = require('passport')
, LocalStrategy = require('passport-local').Strategy
, mongoose = require('mongoose')
, db = require('./db');
var app = express();
// Setup auth
passport.use(new LocalStrategy(
function(username, password, done) {
var User = mongoose.model('Users');
User.findOne({ account : username }).exec(function(err, obj) {
if (err) console.log("Error");
if (obj == null) {
done(null, null);
} else {
// Begin check
var sha512 = crypto.createHash('sha512');
sha512.update(password);
var hash = sha512.digest('hex');
if (obj.passhash == hash) {
console.log("Logged in " + obj.name);
done(null, obj.name);
} else {
done(null, null);
}
}
});
}
));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({secret: 'hSu6gT9' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.methodOverride());
// Check auth
app.use(function(req, res, next) {
// Check login
var pass = req.url.match(/\/stylesheets*|\/javascripts*|\/img*|\/signin|\/v1\/*/g);
if (req.user != null || pass ) {
next(); // Allow static past (for development)
} else {
res.render('signin', {title : 'LectureConnect'});
}
});
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// Routes
app.get('/', routes.index);
app.get('/create', routes.create);
app.get('/signin', routes.signin);
app.post('/signin', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/signin'
}));
app.get('/logout', function(req, res){
req.logout();
res.redirect('/signin');
});
app.get('/account', account.index);
app.post('/account/changepassword', account.changepassword);
app.post('/account/updatesysinfo', account.updatesysinfo);
app.get('/lecture/:id', routes.lecture);
app.get('/start/:id', control.start);
app.get('/kill/:id', control.destroy);
app.get('/clear/:id', control.cleardata);
// API routes
app.get('/v1/lectures/findall', rest.findall);
// Setup SSL
var options = {
key : fs.readFileSync('./src/certs/server.key'),
cert : fs.readFileSync('./src/certs/server.crt')
};
// Start webserver
var server = https.createServer(options, app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = server;