authentication.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. },
  25. usernameField: 'email',
  26. passwordField: 'password',
  27. passReqToCallback: false
  28. }, async (profile, cb) => {
  29. try {
  30. const userId = _.get(profile, conf.mappingUID, null)
  31. if (!userId) {
  32. throw new Error('Invalid Unique ID field mapping!')
  33. }
  34. const user = await WIKI.models.users.processProfile({
  35. profile: {
  36. id: userId,
  37. email: _.get(profile, conf.mappingEmail, ''),
  38. displayName: _.get(profile, conf.mappingDisplayName, '???'),
  39. picture: _.get(profile, conf.mappingPicture, '')
  40. },
  41. providerKey: 'ldap'
  42. })
  43. cb(null, user)
  44. } catch (err) {
  45. if (WIKI.config.flags.ldapdebug) {
  46. WIKI.logger.warn('LDAP LOGIN ERROR (c2): ', err)
  47. }
  48. cb(err, null)
  49. }
  50. }
  51. ))
  52. }
  53. }