forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (111 loc) · 3.79 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const express = require('express');
const passport = require('passport');
const next = require('next');
const cookieSession = require('cookie-session');
const session = require('express-session');
const ControlTowerStrategy = require('passport-control-tower');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const basicAuth = require('basic-auth');
const { parse } = require('url');
const routes = require('./routes');
// Load environment variables from .env file if present
const dotenv = require('dotenv').load();
const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== 'production';
// Next app creation
const app = next({ dev });
const handle = routes.getRequestHandler(app);
// Express app creation
const server = express();
function auth(username, password) {
return function authMiddleware(req, res, next) {
const user = basicAuth(req);
if (!user || user.name !== username || user.pass !== password) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.sendStatus(401);
}
return next();
};
}
function isAuthenticated(req, res, nextAction) {
if (req.isAuthenticated()) return nextAction();
// if they aren't redirect them to the home page
return res.redirect('/');
}
function isAdmin(req, res, nextAction) {
if (req.user.role === 'ADMIN') return nextAction();
// if they aren't redirect them to the home page
return res.redirect('/');
}
// Use the Control Tower Strategy within Passport.
const controlTowerStrategy = new ControlTowerStrategy({
controlTowerUrl: process.env.CONTROL_TOWER_URL,
callbackUrl: process.env.CALLBACK_URL
});
passport.use(controlTowerStrategy);
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session.
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
// configure Express
server.use(cookieParser());
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());
server.use(cookieSession({
name: 'session',
keys: [process.env.SECRET || 'keyboard cat']
}));
server.use(session({
secret: process.env.SECRET || 'keyboard cat',
resave: false,
saveUninitialized: true
}));
server.use('/static', express.static('static'));
if (process.env.NODE_ENV === 'production') {
server.use(auth(process.env.USERNAME, process.env.PASSWORD));
}
// Initialize Passport!
server.use(passport.initialize());
server.use(passport.session());
// Initializing next app before express server
app.prepare()
.then(() => {
// Public/landing page
server.get('/', function (req, res) {
return app.render(req, res, '/app/Home');
});
server.get('/auth', passport.authenticate('control-tower', { failureRedirect: '/' }), function (req, res) {
// On success, redirecting to My RW
res.redirect('/myrw');
});
server.get('/auth/user', function (req, res) {
// On success, redirecting to My RW
return res.json(req.user || {});
});
server.get('/login', function(req, res) {
controlTowerStrategy.login(req, res);
});
server.get('/logout', function (req, res) {
req.logout();
res.redirect(req.query.callbackUrl)
});
server.get('/myrw*?', isAuthenticated, function (req, res) {
const parsedUrl = parse(req.url, true);
return handle(req, res, parsedUrl);
});
server.get('/admin*?', isAuthenticated, isAdmin, function (req, res) {
const parsedUrl = parse(req.url, true);
return handle(req, res, parsedUrl);
});
server.use(handle);
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});