authentication.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. ...conf.mapGroups && {
  20. groupSearchBase: conf.groupSearchBase,
  21. groupSearchFilter: conf.groupSearchFilter,
  22. groupSearchScope: conf.groupSearchScope,
  23. groupDnProperty: conf.groupDnProperty,
  24. groupSearchAttributes: [conf.groupNameField]
  25. },
  26. includeRaw: true
  27. },
  28. usernameField: 'email',
  29. passwordField: 'password',
  30. passReqToCallback: true
  31. }, async (req, profile, cb) => {
  32. try {
  33. const userId = _.get(profile, conf.mappingUID, null)
  34. if (!userId) {
  35. throw new Error('Invalid Unique ID field mapping!')
  36. }
  37. const user = await WIKI.models.users.processProfile({
  38. providerKey: req.params.strategy,
  39. profile: {
  40. id: userId,
  41. email: String(_.get(profile, conf.mappingEmail, '')).split(',')[0],
  42. displayName: _.get(profile, conf.mappingDisplayName, '???'),
  43. picture: _.get(profile, `_raw.${conf.mappingPicture}`, '')
  44. }
  45. })
  46. // map users LDAP groups to wiki groups with the same name, and remove any groups that don't match LDAP
  47. if (conf.mapGroups) {
  48. const ldapGroups = _.get(profile, '_groups')
  49. if (ldapGroups && _.isArray(ldapGroups)) {
  50. const groups = ldapGroups.map(g => g[conf.groupNameField])
  51. const currentGroups = (await user.$relatedQuery('groups').select('groups.id')).map(g => g.id)
  52. const expectedGroups = Object.values(WIKI.auth.groups).filter(g => groups.includes(g.name)).map(g => g.id)
  53. for (const groupId of _.difference(expectedGroups, currentGroups)) {
  54. await user.$relatedQuery('groups').relate(groupId)
  55. }
  56. for (const groupId of _.difference(currentGroups, expectedGroups)) {
  57. await user.$relatedQuery('groups').unrelate().where('groupId', groupId)
  58. }
  59. }
  60. }
  61. cb(null, user)
  62. } catch (err) {
  63. if (WIKI.config.flags.ldapdebug) {
  64. WIKI.logger.warn('LDAP LOGIN ERROR (c2): ', err)
  65. }
  66. cb(err, null)
  67. }
  68. }
  69. ))
  70. }
  71. }
  72. function getTlsOptions(conf) {
  73. if (!conf.tlsEnabled) {
  74. return {}
  75. }
  76. if (!conf.tlsCertPath) {
  77. return {
  78. rejectUnauthorized: conf.verifyTLSCertificate
  79. }
  80. }
  81. const caList = []
  82. if (conf.verifyTLSCertificate) {
  83. caList.push(fs.readFileSync(conf.tlsCertPath))
  84. }
  85. return {
  86. rejectUnauthorized: conf.verifyTLSCertificate,
  87. ca: caList
  88. }
  89. }