-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpassport.js
More file actions
executable file
·51 lines (44 loc) · 1.44 KB
/
passport.js
File metadata and controls
executable file
·51 lines (44 loc) · 1.44 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
const passport = require("passport");
const { Strategy: SamlStrategy } = require("passport-saml");
const dotenv = require("dotenv");
const authConfig = require("./authConfig");
//checks if func is a function
const isFunction = (func)=>func instanceof Function;
//extracts claims from profile by profileExtractor object keys
const extract = (profile,profileExtractor)=>{
let res = {}
Object.keys(profileExtractor).forEach(key=>
res[key]=profile[profileExtractor[key]]
)
return res;
}
dotenv.config();
let users = [];
passport.serializeUser((user, cb) => {
cb(null, user.id);
});
passport.deserializeUser((id, cb) => {
const user =
users.filter(user => user.id === id).length > 0
? users.filter(user => user.id === id)[0]
: {};
cb(null, user);
});
const configurePassport = db => {
const { saml: samlConfig, profileExtractor } = authConfig();
passport.use(
new SamlStrategy(samlConfig, (profile, done) => {
//if profileExtractor is provided as a function use it to extract profile
//else assume its an object whos keys are the profiles keys and the values are the keys in
//the provided profile from saml
profile = isFunction(profileExtractor)?
profileExtractor(profile)
:extract(profile,profileExtractor)
if (users.filter(user => user.id === profile.id).length === 0) {
users.push(profile);
}
done(null, profile);
})
);
};
module.exports = configurePassport;