-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathauth.js
70 lines (63 loc) · 2.07 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
var passport = require('passport');
var SamlStrategy = require('passport-saml').Strategy;
var config = require('./config.json')[process.env.NODE_ENV || 'dev'];
//users array to hold
var users = [];
function findByEmail(email, fn) {
for (var i = 0, len = users.length; i < len; i++) {
var user = users[i];
if (user.email === 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) {
done(null, user.email);
});
passport.deserializeUser(function(id, done) {
findByEmail(id, function(err, user) {
done(err, user);
});
});
passport.use(new SamlStrategy({
issuer: config.auth.issuer,
path: '/login/callback',
entryPoint: config.auth.entryPoint,
cert: config.auth.cert
},
function(profile, done) {
console.log('Succesfully Profile' + profile);
if (!profile.email) {
return done(new Error("No email found"), null);
}
process.nextTick(function() {
console.log('process.nextTick' + profile);
findByEmail(profile.email, function(err, user) {
if (err) {
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;