passport-func.js
2.19 KB
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 LocalStrategy = require('passport-local').Strategy;
const SamlStrategy = require('passport-saml').Strategy;
const conf = require('utils/config');
const passportFunctionLocal = function (username, password, done) {
console.log('checking username and password...');
if (username === 'admin' && password === 'passw0rd') {
const user = {
id: 1,
username: username
};
console.log('checking username and password... Passed');
return done(null, user);
} else {
console.log('checking username and password... Failed');
return done(null, false, {message: 'Incorrect credentials'});
}
};
const passportFunctionSaml = function(profile, done) {
return done(null,
{
userName: profile.userName,
email: profile.email,
firstName: profile.firstName,
lastName: profile.lastName,
fullName: profile.fullName
});
};
module.exports = function() {
passport.serializeUser(function(user, done) {
console.log('serialize');
done(null, user);
});
passport.deserializeUser(function(user, done) {
console.log('deserialize');
done(null, user);
});
let PassportStrategy, passportConfig, passportFunction;
switch(conf.get('passport.strategy')) {
case "local":
PassportStrategy = LocalStrategy;
passportConfig = conf.get('passport.configStrategy.local');
passportFunction = passportFunctionLocal;
break;
case "saml":
PassportStrategy = SamlStrategy;
passportConfig = conf.get('passport.configStrategy.saml');
passportFunction = passportFunctionSaml;
break;
default:
PassportStrategy = null;
break;
}
if (PassportStrategy) {
console.log('setup passport strategy');
console.log(conf.get('passport.strategy'));
console.log(passportConfig);
console.log(passportFunction);
console.log(PassportStrategy);
passport.use(new PassportStrategy(passportConfig, passportFunction));
}
return passport;
};