auth.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* global WIKI */
  2. const _ = require('lodash')
  3. const passport = require('passport')
  4. const fs = require('fs-extra')
  5. const path = require('path')
  6. const autoload = require('auto-load')
  7. module.exports = {
  8. strategies: {},
  9. init() {
  10. this.passport = passport
  11. // Serialization user methods
  12. passport.serializeUser(function (user, done) {
  13. done(null, user.id)
  14. })
  15. passport.deserializeUser(function (id, done) {
  16. WIKI.db.User.findById(id).then((user) => {
  17. if (user) {
  18. done(null, user)
  19. } else {
  20. done(new Error(WIKI.lang.t('auth:errors:usernotfound')), null)
  21. }
  22. return true
  23. }).catch((err) => {
  24. done(err, null)
  25. })
  26. })
  27. // Load authentication strategies
  28. const modules = _.values(autoload(path.join(WIKI.SERVERPATH, 'modules/authentication')))
  29. _.forEach(modules, (strategy) => {
  30. const strategyConfig = _.get(WIKI.config.auth.strategies, strategy.key, {})
  31. strategyConfig.callbackURL = `${WIKI.config.site.host}${WIKI.config.site.path}login/${strategy.key}/callback`
  32. if (strategyConfig.isEnabled) {
  33. try {
  34. strategy.init(passport, strategyConfig)
  35. } catch (err) {
  36. WIKI.logger.error(`Authentication Provider ${strategy.title}: [ FAILED ]`)
  37. WIKI.logger.error(err)
  38. }
  39. }
  40. fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${strategy.key}.svg`), 'utf8').then(iconData => {
  41. strategy.icon = iconData
  42. }).catch(err => {
  43. if (err.code === 'ENOENT') {
  44. strategy.icon = '[missing icon]'
  45. } else {
  46. WIKI.logger.warn(err)
  47. }
  48. })
  49. this.strategies[strategy.key] = strategy
  50. WIKI.logger.info(`Authentication Provider ${strategy.title}: [ OK ]`)
  51. })
  52. // Create Guest account for first-time
  53. WIKI.db.User.findOne({
  54. where: {
  55. provider: 'local',
  56. email: 'guest@example.com'
  57. }
  58. }).then((c) => {
  59. if (c < 1) {
  60. return WIKI.db.User.create({
  61. provider: 'local',
  62. email: 'guest@example.com',
  63. name: 'Guest',
  64. password: '',
  65. role: 'guest'
  66. }).then(() => {
  67. WIKI.logger.info('[AUTH] Guest account created successfully!')
  68. return true
  69. }).catch((err) => {
  70. WIKI.logger.error('[AUTH] An error occured while creating guest account:')
  71. WIKI.logger.error(err)
  72. return err
  73. })
  74. }
  75. })
  76. // .then(() => {
  77. // if (process.env.WIKI_JS_HEROKU) {
  78. // return WIKI.db.User.findOne({ provider: 'local', email: process.env.WIKI_ADMIN_EMAIL }).then((c) => {
  79. // if (c < 1) {
  80. // // Create root admin account (HEROKU ONLY)
  81. // return WIKI.db.User.create({
  82. // provider: 'local',
  83. // email: process.env.WIKI_ADMIN_EMAIL,
  84. // name: 'Administrator',
  85. // password: '$2a$04$MAHRw785Xe/Jd5kcKzr3D.VRZDeomFZu2lius4gGpZZ9cJw7B7Mna', // admin123 (default)
  86. // role: 'admin'
  87. // }).then(() => {
  88. // WIKI.logger.info('[AUTH] Root admin account created successfully!')
  89. // return true
  90. // }).catch((err) => {
  91. // WIKI.logger.error('[AUTH] An error occured while creating root admin account:')
  92. // WIKI.logger.error(err)
  93. // return err
  94. // })
  95. // } else { return true }
  96. // })
  97. // } else { return true }
  98. // })
  99. return this
  100. }
  101. }