-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (88 loc) · 3.16 KB
/
app.js
File metadata and controls
105 lines (88 loc) · 3.16 KB
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
import express from 'express';
import path from 'path';
import logger from 'morgan';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import passport from 'passport';
import session from 'express-session';
import { fileURLToPath } from 'url';
import OpenIDConnectStrategy from 'passport-openidconnect';
import config from './config.js';
import index from './routes/index.js';
import resumes from './routes/resumes.js';
import upload from './routes/upload.js';
import comment from './routes/comment.js';
// importing this module will schedule the job that communicates with ResumeBot
import resumeBot from './slackbot.js';
// https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
// __dirname isn't defined in ES Modules and this code was initially written before ES Modules were a thing
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('combined'));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
secret: config.auth.session_secret,
saveUninitialized: true,
resave: true,
}));
passport.use(new OpenIDConnectStrategy({
issuer: 'https://sso.csh.rit.edu/auth/realms/csh',
authorizationURL: 'https://sso.csh.rit.edu/auth/realms/csh/protocol/openid-connect/auth',
tokenURL: 'https://sso.csh.rit.edu/auth/realms/csh/protocol/openid-connect/token',
userInfoURL: 'https://sso.csh.rit.edu/auth/realms/csh/protocol/openid-connect/userinfo',
clientID: config.auth.client_id,
clientSecret: config.auth.client_secret,
callbackURL: config.auth.callback_url,
},
function(issuer, profile, cb) {
return cb(null, profile);
}));
const userFunct = (user, cb) => cb(null, user);
passport.serializeUser(userFunct);
passport.deserializeUser(userFunct);
app.use(passport.initialize());
app.use(passport.session());
app.get('/auth',
passport.authenticate('openidconnect'));
app.get('/auth/callback',
passport.authenticate('openidconnect', { failureRedirect: '/auth' }),
function(req, res) {
res.redirect('/');
});
const requireAuth = (req, res, next) => {
if (req.user) {
next();
} else {
res.redirect('/auth');
}
};
app.use('/', requireAuth, index);
app.use('/resumes', requireAuth, resumes);
app.use('/upload', requireAuth, upload);
app.use('/comment', requireAuth, comment);
// 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 handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = JSON.stringify(err.message);
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.listen(config.port);
export default app;