authentication.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(conf.key,
  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: getTlsOptions(conf),
  19. includeRaw: true
  20. },
  21. usernameField: 'email',
  22. passwordField: 'password',
  23. passReqToCallback: true
  24. }, async (req, profile, cb) => {
  25. try {
  26. const userId = _.get(profile, conf.mappingUID, null)
  27. if (!userId) {
  28. throw new Error('Invalid Unique ID field mapping!')
  29. }
  30. const user = await WIKI.db.users.processProfile({
  31. providerKey: req.params.strategy,
  32. profile: {
  33. id: userId,
  34. email: String(_.get(profile, conf.mappingEmail, '')).split(',')[0],
  35. displayName: _.get(profile, conf.mappingDisplayName, '???'),
  36. picture: _.get(profile, `_raw.${conf.mappingPicture}`, '')
  37. }
  38. })
  39. cb(null, user)
  40. } catch (err) {
  41. if (WIKI.config.flags.ldapdebug) {
  42. WIKI.logger.warn('LDAP LOGIN ERROR (c2): ', err)
  43. }
  44. cb(err, null)
  45. }
  46. }
  47. ))
  48. }
  49. }
  50. function getTlsOptions(conf) {
  51. if (!conf.tlsEnabled) {
  52. return {}
  53. }
  54. if (!conf.tlsCertPath) {
  55. return {
  56. rejectUnauthorized: conf.verifyTLSCertificate,
  57. }
  58. }
  59. const caList = []
  60. if (conf.verifyTLSCertificate) {
  61. caList.push(fs.readFileSync(conf.tlsCertPath))
  62. }
  63. return {
  64. rejectUnauthorized: conf.verifyTLSCertificate,
  65. ca: caList
  66. }
  67. }