You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

  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. ca: [
  20. fs.readFileSync(conf.tlsCertPath)
  21. ]
  22. } : {}
  23. },
  24. usernameField: 'email',
  25. passwordField: 'password',
  26. passReqToCallback: false
  27. }, async (profile, cb) => {
  28. try {
  29. const userId = _.get(profile, conf.mappingUID, null)
  30. if (!userId) {
  31. throw new Error('Invalid Unique ID field mapping!')
  32. }
  33. const user = await WIKI.models.users.processProfile({
  34. profile: {
  35. id: userId,
  36. email: _.get(profile, conf.mappingEmail, ''),
  37. displayName: _.get(profile, conf.mappingDisplayName, '???'),
  38. picture: _.get(profile, conf.mappingPicture, '')
  39. },
  40. providerKey: 'ldap'
  41. })
  42. cb(null, user)
  43. } catch (err) {
  44. cb(err, null)
  45. }
  46. }
  47. ))
  48. }
  49. }