-
Notifications
You must be signed in to change notification settings - Fork 0
/
adauth.js
71 lines (58 loc) · 1.75 KB
/
adauth.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
module.exports = function (config) {
var express = require('express');
var session = require('express-session');
var passport = require('passport');
var wsfedsaml2 = require('passport-azure-ad').WsfedStrategy;
var waad = require('node-waad');
var app = express.Router();
// store session info in memory
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false
}))
// add passport middleware
app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
if (req.isAuthenticated() || req.url === "/login" || req.url === "/login/callback") {
next()
}
else {
res.redirect('/login')
}
})
var wsfedStrategy = new wsfedsaml2(config.wsfed,
function (profile, done) {
return done(null, profile)
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login')
}
passport.use(wsfedStrategy);
passport.serializeUser(function (user, cb) {
cb(null, user)
});
var users = [];
passport.deserializeUser(function (user, done) {
done(null, user)
});
// send the user to WAAD to authenticate
app.get('/login', passport.authenticate('wsfed-saml2', {
failureRedirect: '/',
failureFlash: true
}), function (req, res) {
res.redirect('/');
});
// callback from WAAD with a token
app.post('/login/callback', passport.authenticate('wsfed-saml2', {
failureRedirect: '/',
failureFlash: true
}), function (req, res) {
res.redirect('/');
});
return app;
}