-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
114 lines (90 loc) · 3.16 KB
/
index.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
/** index.js
*
* @author Victor Petrov <[email protected]>
* @copyright (c) 2012, The Neuroinformatics Research Group at Harvard University.
* @copyright (c) 2012, The President and Fellows of Harvard College.
* @license New BSD License (see LICENSE file for details).
*/
/** app must have 'log' and 'dirname' properties */
var moduleName = require("./package.json").name,
path = require('path'),
fs = require('fs'),
passport = require('passport'),
auth = require('./lib/auth'),
HTTP_UNAUTHORIZED = 401;
exports.config = require('./config');
/**
* Ensures that all admin pages are only accessible to authenticated users
* @param req
* @param res
* @param next
* @return {*}
*/
exports.ensureAuthenticated = function (req, res, next) {
"use strict";
if (req.isAuthenticated()) {
return next();
}
//AJAX?
if ((req.header('Content-Type') === 'application/json') || (req.header('X-Requested-With') === 'XMLHttpRequest')) {
return res.send({
success: 0,
logged_out: true,
message: "Your session has expired. Please <a href='/login'>login</a>"
}, HTTP_UNAUTHORIZED);
}
return res.redirect('login');
};
exports.routing = function (app, config, method, route, controller, action, handler) {
"use strict";
var routes = config.auth.routes;
//check to see if there is a special auth.routes entry for this route
if (routes[method] && (routes[method][route] !== undefined)) {
//if there is, then if it's true, this route needs to be protected
if (routes[method][route] === true) {
return exports.ensureAuthenticated;
}
} else if (config.auth['default'] === true) {
//else, there is no exception for this route and by default, it must be protected
return exports.ensureAuthenticated;
}
//by default do not return any middleware routing functions
return null;
};
exports.server = function (survana, express) {
"use strict";
var app = express.createServer();
this.app = app;
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options', {
layout: false
});
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'Survana:)' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(express['static'](__dirname + '/public')); //'static' is a reserved keyword
app.use(app.router);
app.log = survana.log.sub(moduleName);
app.dirname = __dirname;
});
//set up routes
survana.routing(app, this.config, this.routing);
app.log.info('reporting in!');
app.config = this.config;
app.dbserver = new survana.db(this.config.db);
//open a database connection
app.dbserver.connect(function (db) {
app.db = db;
},
function (error) {
throw error;
});
this.config.publishers = survana.readKeys(this.config.publishers);
auth.setup(app);
return this.app;
};