-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.ts
74 lines (67 loc) · 1.95 KB
/
server.ts
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
import bodyParser from 'body-parser';
import uuid from 'node-uuid';
import express from 'express';
import cookieParser from 'cookie-parser'
var app = express();
app.use(cookieParser());
var tickets:any = {};
var cookies:any = {};
app.get('/login', function(req, res) {
if (req.cookies['fake-cas'] && cookies[req.cookies['fake-cas']]) {
if (req.query.service) {
var ticket = "ST-" + uuid.v1();
tickets[ticket] = cookies[req.cookies['fake-cas']];
res.writeHead(302, {
'Location': `${req.query.service}?ticket=${ticket}`
});
res.end();
} else {
res.sendFile(process.cwd() + '/already.html');
}
} else {
res.sendFile(process.cwd() + '/form.html');
}
});
app.post('/login', bodyParser.urlencoded(), function(req, res) {
if (process.env.CAS_PASSWORD && req.body.password !== process.env.CAS_PASSWORD) {
return res.sendFile(process.cwd() + '/form.html');
}
if (process.env.NO_COOKIE === undefined) {
var cookie = uuid.v1();
cookies[cookie] = req.body.login;
res.cookie('fake-cas', cookie);
}
if (req.query.service) {
var ticket = "ST-" + uuid.v1();
tickets[ticket] = req.body.login;
res.writeHead(302, {
'Location': `${req.query.service}?ticket=${ticket}`
});
res.end();
} else {
res.sendFile(process.cwd() + '/already.html');
}
});
app.use('/logout', function(req, res) {
if (req.cookies['fake-cas'] && cookies[req.cookies['fake-cas']]) {
delete cookies[req.cookies['fake-cas']];
}
res.cookie('fake-cas', '');
res.writeHead(302, {
'Location': '/login'
});
res.end();
});
app.get('/validate', function(req, res) {
const ticket = req.query.ticket as string;
if (ticket && tickets[ticket]) {
res.send(`yes\n${tickets[ticket]}`);
delete tickets[ticket];
} else {
res.send('no');
}
res.end();
});
app.listen( process.env.PORT || 3000, function() {
console.log(`Fake-cas available on http://localhost:${process.env.PORT || 3000}`);
});