authentication.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* global WIKI */
  2. // ------------------------------------
  3. // LDAP Account
  4. // ------------------------------------
  5. const LdapStrategy = require('passport-ldapauth').Strategy
  6. const fs = require('fs')
  7. const _ = require('lodash')
  8. module.exports = {
  9. init (passport, conf) {
  10. passport.use('ldap',
  11. new LdapStrategy({
  12. server: {
  13. url: conf.url,
  14. bindDn: conf.bindDn,
  15. bindCredentials: conf.bindCredentials,
  16. searchBase: conf.searchBase,
  17. searchFilter: conf.searchFilter,
  18. tlsOptions: (conf.tlsEnabled) ? {
  19. rejectUnauthorized: conf.verifyTLSCertificate,
  20. ca: [
  21. fs.readFileSync(conf.tlsCertPath)
  22. ]
  23. } : {},
  24. includeRaw: true
  25. },
  26. usernameField: 'email',
  27. passwordField: 'password',
  28. passReqToCallback: true
  29. }, async (req, profile, cb) => {
  30. try {
  31. const userId = _.get(profile, conf.mappingUID, null)
  32. if (!userId) {
  33. throw new Error('Invalid Unique ID field mapping!')
  34. }
  35. const user = await WIKI.models.users.processProfile({
  36. providerKey: req.params.strategy,
  37. profile: {
  38. id: userId,
  39. email: String(_.get(profile, conf.mappingEmail, '')).split(',')[0],
  40. displayName: _.get(profile, conf.mappingDisplayName, '???'),
  41. picture: _.get(profile, `_raw.${conf.mappingPicture}`, '')
  42. }
  43. })
  44. cb(null, user)
  45. } catch (err) {
  46. if (WIKI.config.flags.ldapdebug) {
  47. WIKI.logger.warn('LDAP LOGIN ERROR (c2): ', err)
  48. }
  49. cb(err, null)
  50. }
  51. }
  52. ))
  53. }
  54. }