-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
197 lines (167 loc) · 5.71 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// For Oauth2, See https://github.com/lelylan/simple-oauth2#express-and-github-example
/*jshint esversion: 6 */
// Trying Winston for logging on the console AND in a Logfile
var express = require('express');
var app = express();
var path = require('path');
var cors = require('cors');
var bodyParser = require('body-parser');
app.use(require('express-bunyan-logger')());
// Debug Stuff
var logger = require('./loggerToFile');
// File load
var views = require('./routes/view');
var goldens = require('./routes/golden');
var users = require('./routes/user');
var results = require('./routes/result');
var utils = require('./utils');
// Security layer:
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth2').Strategy;
var session = require( 'express-session' ); //Create a session middleware with the given options.
var GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID;
var GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET;
// Passport session setup.
passport.serializeUser(function(googUserInfo, done) {
// Note: This get called only once on the callback from Google.
// Step1: Extract only the info we need.
var user = {
email: googUserInfo.email,
username: googUserInfo.name.givenName,
name: googUserInfo.displayName,
role: 'manager'
};
// TODO: Fix the Role by getting it witht he DB directly
logger.debug("serialisation:", user);
// name include Family and given : {
// familyName: 'Chiodo',
// givenName: 'Mathieu'
// },
// Here it could be in a promesses or something or we keep it asynch
// Step2: Save the user data in the DB is it dosent exist.
utils.doesUserExist(user);
// eater case if exist already or not just continue since we have all info from Google.
done(null, user);
});
passport.deserializeUser(function(obj, done) {
// Note: This get call at each routes we get that are secured.
logger.debug("deserialisation: ", obj);
// Not sure what to do with this yet.
done(null, obj);
});
// Use the GoogleStrategy within Passport.
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:" + process.env.PORT + "/auth/google/callback",
passReqToCallback : true
},
function(request, accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// --> Here we have the profile, but the Next dosent pass it???? It get lost somewhere. Where can it endup in the res.user?
return done(null, profile);
});
}
));
app.use( session({
secret: 'cookie_secret',
resave: true,
saveUninitialized: true
}));
app.use( passport.initialize());
app.use( passport.session());
// Simple route middleware to ensure user is authenticated.
function ensureAuthenticated(req, res, next) {
if (process.env.FAKE_AUTH === "authenticated") {
logger.debug("Faking Auth");
return next();
}
if (req.isAuthenticated()) {return next();}
res.redirect('/login');
}
// GET /auth/google
app.get('/auth/google',
passport.authenticate('google', {scope: ['profile', 'email']})
);
// GET /auth/google/callback
app.get( '/auth/google/callback',
passport.authenticate( 'google', {
successRedirect: '/app',
failureRedirect: '/login'
}));
app.get('/logout', function(req, res){
req.logout();
res.redirect('/login');
});
// End of the security layer
// App itself
var env = process.env.NODE_ENV || 'development';
app.locals.ENV = env;
app.locals.ENV_DEVELOPMENT = env == 'development';
app.use(cors());
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// API Routing
app.use('/login', function(req, res){
res.render('login', { user: req.user });
});
app.use('/api/view', ensureAuthenticated, views);
app.use('/api/golden', ensureAuthenticated, goldens);
app.use('/api/user', ensureAuthenticated, users);
app.use('/api/result', ensureAuthenticated, results);
app.get('/api/whoislogin', ensureAuthenticated, function(req, res, next){
logger.info("UserData:", req.user);
if (req.user) {
res.json(req.user);
} else {
next(new Error('failed to load user details'));
}
});
// When all authenticated we can load the app and make sure all routes inside the app are secured.
app.use('/app/', ensureAuthenticated, express.static(__dirname + '/public/components/good-or-bad-img')); //Here it should link the the Angular App
// Probably went too far or something happened in the Angular State or wrong routes, should be catch before and be redirected in the app itself instead.
app.use('/*', ensureAuthenticated, views);
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
logger.warn('Error:', err);
res.status(err.status || 500);
res.json({
message: err.message,
error: err,
title: 'error'
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: {},
title: 'error'
});
});
var portToUse = process.env.PORT || 8010;
app.set('port', portToUse);
var server = app.listen(portToUse, function() {
logger.warn('Express server listening on port ' + server.address().port);
});
exports.closeServer = function(){
server.close();
};