2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-07-29 21:33:08 +00:00
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// LDAP Account
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
const LdapStrategy = require('passport-ldapauth').Strategy
|
2017-07-30 04:04:57 +00:00
|
|
|
const fs = require('fs')
|
2019-04-27 03:59:35 +00:00
|
|
|
const _ = require('lodash')
|
2017-07-29 21:33:08 +00:00
|
|
|
|
2017-09-11 03:00:03 +00:00
|
|
|
module.exports = {
|
|
|
|
init (passport, conf) {
|
2019-04-27 03:59:35 +00:00
|
|
|
passport.use('ldap',
|
2017-09-11 03:00:03 +00:00
|
|
|
new LdapStrategy({
|
|
|
|
server: {
|
|
|
|
url: conf.url,
|
|
|
|
bindDn: conf.bindDn,
|
|
|
|
bindCredentials: conf.bindCredentials,
|
|
|
|
searchBase: conf.searchBase,
|
|
|
|
searchFilter: conf.searchFilter,
|
|
|
|
tlsOptions: (conf.tlsEnabled) ? {
|
|
|
|
ca: [
|
|
|
|
fs.readFileSync(conf.tlsCertPath)
|
|
|
|
]
|
|
|
|
} : {}
|
|
|
|
},
|
|
|
|
usernameField: 'email',
|
2019-04-27 03:59:35 +00:00
|
|
|
passwordField: 'password',
|
2017-09-11 03:00:03 +00:00
|
|
|
passReqToCallback: false
|
2019-04-27 03:59:35 +00:00
|
|
|
}, async (profile, cb) => {
|
|
|
|
try {
|
|
|
|
const userId = _.get(profile, conf.mappingUID, null)
|
|
|
|
if (!userId) {
|
|
|
|
throw new Error('Invalid Unique ID field mapping!')
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await WIKI.models.users.processProfile({
|
|
|
|
profile: {
|
|
|
|
id: userId,
|
|
|
|
email: _.get(profile, conf.mappingEmail, ''),
|
|
|
|
displayName: _.get(profile, conf.mappingDisplayName, '???'),
|
|
|
|
picture: _.get(profile, conf.mappingPicture, '')
|
|
|
|
},
|
|
|
|
providerKey: 'ldap'
|
|
|
|
})
|
|
|
|
cb(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err, null)
|
|
|
|
}
|
2017-09-11 03:00:03 +00:00
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
2017-07-29 21:33:08 +00:00
|
|
|
}
|