ldap.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* global WIKI */
  2. // ------------------------------------
  3. // LDAP Account
  4. // ------------------------------------
  5. const LdapStrategy = require('passport-ldapauth').Strategy
  6. const fs = require('fs')
  7. module.exports = {
  8. key: 'ldap',
  9. title: 'LDAP / Active Directory',
  10. useForm: true,
  11. props: {
  12. url: {
  13. type: String,
  14. default: 'ldap://serverhost:389'
  15. },
  16. bindDn: {
  17. type: String,
  18. default: `cn='root'`
  19. },
  20. bindCredentials: String,
  21. searchBase: {
  22. type: String,
  23. default: 'o=users,o=example.com'
  24. },
  25. searchFilter: {
  26. type: String,
  27. default: '(uid={{username}})'
  28. },
  29. tlsEnabled: {
  30. type: Boolean,
  31. default: false
  32. },
  33. tlsCertPath: String
  34. },
  35. init (passport, conf) {
  36. passport.use('ldapauth',
  37. new LdapStrategy({
  38. server: {
  39. url: conf.url,
  40. bindDn: conf.bindDn,
  41. bindCredentials: conf.bindCredentials,
  42. searchBase: conf.searchBase,
  43. searchFilter: conf.searchFilter,
  44. searchAttributes: ['displayName', 'name', 'cn', 'mail'],
  45. tlsOptions: (conf.tlsEnabled) ? {
  46. ca: [
  47. fs.readFileSync(conf.tlsCertPath)
  48. ]
  49. } : {}
  50. },
  51. usernameField: 'email',
  52. passReqToCallback: false
  53. }, (profile, cb) => {
  54. profile.provider = 'ldap'
  55. profile.id = profile.dn
  56. WIKI.db.users.processProfile(profile).then((user) => {
  57. return cb(null, user) || true
  58. }).catch((err) => {
  59. return cb(err, null) || true
  60. })
  61. }
  62. ))
  63. }
  64. }