-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
122 lines (110 loc) · 4.13 KB
/
server.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
const express = require('express');
const next = require('next');
let session = require('cookie-session')
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
var CentralAuthenticationService = require('cas');
app.prepare()
.then(() => {
const server = express();
var casURL = 'https://fed.princeton.edu/cas/'
var cas = new CentralAuthenticationService({
base_url: casURL,
service: "http://ec2-18-224-19-243.us-east-2.compute.amazonaws.com" + "/verify"
})
server.use(session({
secret: 'abcdefghijklmnop',
maxAge: 24 * 60 * 60 * 1000 * 365,
cookie: { secure: false }
}))
server.set('json spaces', 2);
server.get('/login', function (req, res) {
// Save the user's redirection destination to a cookie
if (typeof (req.query.redirect) === 'string') {
req.session.redirect = req.query.redirect
}
// Redirect the user to the CAS server
res.redirect(casURL + 'login?service=' + "http://ec2-18-224-19-243.us-east-2.compute.amazonaws.com/verify")
})
server.get('/logout', function (req, res) {
req.session = null
res.cookie('netid', null)
res.redirect(casURL + 'logout?url=http://ec2-18-224-19-243.us-east-2.compute.amazonaws.com')
})
server.get('/verify', function(req, res) {
// Check if the user has a redirection destination
let redirectDestination = req.session.redirect || '/'
// If the user already has a valid CAS session then send them to their destination
if (req.session.cas) {
res.redirect(redirectDestination)
return
}
var ticket = req.query.ticket
// If the user does not have a ticket then send them to the homepage
if (typeof (ticket) === 'undefined') {
res.redirect('/')
return
}
// Check if the user's ticket is valid
cas.validate(ticket, function (err, status, netid) {
if (err) {
console.log(err)
res.sendStatus(500)
return
}
req.session.cas = {
status: status,
netid: netid
};
res.cookie('netid', netid)
//req.session.cookie.netid = netid;
res.redirect(redirectDestination);
}) });
server.get('/eventOrganizerRegister', function(req, res){
return handle(req, res);
});
server.get('/test', function (req, res) {
res.send({yo: req.session.cas.netid});
});
server.get('/netid', function (req, res) {
// Save the user's redirection destination to a cookie
if (req.session.cas) {
res.send({netid: req.session.cas.netid});
//console.log("yeeeeeeeeeeeet");
}
else res.redirect("/login?redirect=/")
})
server.get('/', function (req, res) {
res.redirect("http://ec2-18-218-99-159.us-east-2.compute.amazonaws.com")
})
server.get('*/bootstrap.css',
function (req, res) {
return handle(req, res);
}
)
server.get('/static/background.jpg',
function (req, res) {
return handle(req, res);
}
)
server.get('*', function (req, res) {
if (req.session.cas){
return handle(req, res);
}
res.redirect("/login?redirect=/")
})
server.set('views', '/views');
server.set('view engine', 'js');
server.engine('js', require('express-react-views').createEngine());
server.listen(process.env.PORT || 3000, (err) => {
if (err) {
throw err;
}
//console.log('> Ready on http://localhost:3000');
});
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});