-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
71 lines (63 loc) · 2.15 KB
/
auth.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
const passport = require('passport');
const SamlStrategy = require('passport-saml').Strategy;
const config = require('./config.json')['custom'];//process.env.NODE_ENV ||
//users array to hold
const users = [];
function findByEmail(email, fn) {
for (let i = 0, len = users.length; i < len; i++) {
const user = users[i];
if (user.nameID === email) return fn(null, user);
}
return fn(null, null);
}
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing.
passport.serializeUser(function(user, done) {
console.log('----serializeUser----');
done(null, user.nameID);
});
passport.deserializeUser(function(id, done) {
console.log('----deserializeUser----');
findByEmail(id, function(err, user) {
done(err, user);
});
});
passport.use(new SamlStrategy({
issuer: config.auth.issuer,
entryPoint: config.auth.entryPoint,
cert: config.auth.cert
},
function(profile, done) {
console.log('----profile----');
console.log(JSON.stringify(profile, null, 2));
if (!profile.nameID) {
return done(new Error("No email found"), null);
}
process.nextTick(function() {
findByEmail(profile.nameID, function(err, user) {
if (err) {
console.log('Error');
return done(err);
}
if (!user) {
users.push(profile);
return done(null, profile);
}
console.log('Ending Method for profiling');
return done(null, user);
})
});
}
));
passport.protected = function protected(req, res, next) {
console.log('Login Profile' + req.isAuthenticated());
if (req.isAuthenticated()) {
return next();
}
console.log('login please' + req.isAuthenticated());
res.redirect('/login');
};
exports = module.exports = passport;