-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.js
157 lines (140 loc) · 4.35 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
/**
* Module dependencies.
*/
var express = require('express');
var session = require('cookie-session');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var methodOverride = require('method-override');
var basicAuth = require('basic-auth');
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth2').Strategy;
var http = require('http');
var path = require('path');
var ENV;
if (process.env.NODE_ENV) {
ENV = process.env.NODE_ENV
} else {
ENV = 'development';
}
// Config
var config = require('./config/config.json');
// MongoDB setup
var mongo = require('./config/mongo.json');
var mongoose = require('mongoose');
var mongoUri = process.env.MONGOHQ_URL || mongo[ENV].uri;
var mongoOpts = mongo[ENV].opts;
mongoose.connect(mongoUri, mongoOpts);
// Auth setup
var auth = require('./config/auth.json')[ENV];
global.auth = auth;
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new GoogleStrategy({
clientID: auth.google.client_id,
clientSecret: auth.google.client_secret,
callbackURL: auth.google.callback_url
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
}
));
var routes = require('./routes');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(__dirname + '/public/images/favicon.ico'));
app.use(morgan('dev'));
app.use(bodyParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method;
delete req.body._method;
return method;
}
}));
app.use(session({keys: config.session_keys, secureProxy: false}))
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
if(auth.basic.enabled) {
var credentials = basicAuth(req);
if(!credentials || !(auth.basic.name === credentials.name &&
auth.basic.pass === credentials.pass)) {
res.writeHead(401, {
'WWW-Authenticate': 'Basic realm="Welcome to Mongri"'
});
res.end();
} else {
next();
}
} else {
next();
}
});
app.use(function(req, res, next){
res.locals.isAuthenticated = req.isAuthenticated();
res.locals.logoTitle = config.title;
res.locals.dropCollection = config.drop_collection;
next();
});
// development only
if ('development' == app.get('env')) {
mongoose.set('debug', true);
}
var collection = require('./routes/collection');
var document = require('./routes/document');
app.get('/', routes.index);
app.all('/collections*', ensureAuthenticated);
app.get('/collections', collection.collections);
app.get('/collections/:collection/drop', collection.dropCollection);
app.get('/collections/:document', document.documents);
app.get('/collections/:document/new', document.newDocument);
app.post('/collections/:document', document.postDocument);
app.get('/collections/:document/:id', document.document);
app.delete('/collections/:document/:id', document.deleteDocument);
app.post('/collections/:document/:id', document.documentUpdate);
app.get('/auth/google',
passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/userinfo.email'] }),
function(req, res){});
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/' }),
function(req, res) {
email = req.user.emails[0].value
domain = email.split("@")[1]
if (domain != auth.google.allow_domain){
res.redirect('/logout');
} else {
res.redirect('/');
}
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
function ensureAuthenticated(req, res, next) {
if (auth.google.enabled){
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
}else{
next();
}
}
http.createServer(app).listen(app.get('port'), '0.0.0.0', function(){
console.log('Express server listening on port ' + app.get('port'));
});