-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (57 loc) · 1.86 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
const express = require('express');
const session = require('cookie-session');
const bodyParser = require('body-parser');
const jwt = require('jwt-simple');
const app = express();
const config = require('./config.json');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('trust proxy', 1);
// TODO: this is v naive
app.use(session({
name: config['sessionName'],
secret: config['cookieKey'],
maxAge: Number(config['expiry']) * 1000 * 60 * 60
}));
function check_jwt(authjwt) {
if( authjwt['iss'] !== config['iss'] ) {
console.log(`iss ${authjwt['iss']} mismatch with ${config['iss']}`);
return false;
}
if( authjwt['aud'] !== config['aud'] ) {
console.log(`aud ${authjwt['aud']} mismatch with ${config['aud']}`);
return false;
}
const nbf = new Date(authjwt['nbf']);
const exp = new Date(authjwt['exp']);
const now = new Date();
if( !! ( now > nbf && now < exp ) ) {
console.log(`Time assertion fail ${nbf} > ${now} > ${exp}`);
return false;
}
return true;
}
app.get('/', (req, res) => {
res.redirect(302, config['authURL']);
});
app.post('/jwt', (req, res) => {
const authjwt = jwt.decode(req.body['assertion'], config['secret']);
console.log(JSON.stringify(authjwt, null, 8))
if( check_jwt(authjwt) ) {
console.log("AAF authentication was successful");
const atts = authjwt[config['attributes']];
req.session.uid = atts['mail'];
req.session.displayName = atts['displaybame'];
req.session.affiliation = atts['edupersonscopedaffiliation'];
res.redirect(config['success']);
} else {
console.log("AAF authentication failed");
res.sendStatus(403);
}
})
app.get('/logout', ( req, res ) => {
req.session.uid = '';
req.session.displayName = '';
req.session.affiliation = '';
});
app.listen(config['port'], config['host'], () => console.log(`express-aaf listening on ${config['host']}:${config['port']}`));