auth.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, { isEnabled: false })
  31. strategyConfig.callbackURL = `${WIKI.config.site.host}${WIKI.config.site.path}login/${strategy.key}/callback`
  32. strategy.config = strategyConfig
  33. if (strategyConfig.isEnabled) {
  34. try {
  35. strategy.init(passport, strategyConfig)
  36. } catch (err) {
  37. WIKI.logger.error(`Authentication Provider ${strategy.title}: [ FAILED ]`)
  38. WIKI.logger.error(err)
  39. }
  40. }
  41. fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${strategy.key}.svg`), 'utf8').then(iconData => {
  42. strategy.icon = iconData
  43. }).catch(err => {
  44. if (err.code === 'ENOENT') {
  45. strategy.icon = '[missing icon]'
  46. } else {
  47. WIKI.logger.warn(err)
  48. }
  49. })
  50. this.strategies[strategy.key] = strategy
  51. WIKI.logger.info(`Authentication Provider ${strategy.title}: [ OK ]`)
  52. })
  53. // Create Guest account for first-time
  54. WIKI.db.User.findOne({
  55. where: {
  56. provider: 'local',
  57. email: 'guest@example.com'
  58. }
  59. }).then((c) => {
  60. if (c < 1) {
  61. return WIKI.db.User.create({
  62. provider: 'local',
  63. email: 'guest@example.com',
  64. name: 'Guest',
  65. password: '',
  66. role: 'guest'
  67. }).then(() => {
  68. WIKI.logger.info('[AUTH] Guest account created successfully!')
  69. return true
  70. }).catch((err) => {
  71. WIKI.logger.error('[AUTH] An error occured while creating guest account:')
  72. WIKI.logger.error(err)
  73. return err
  74. })
  75. }
  76. })
  77. // .then(() => {
  78. // if (process.env.WIKI_JS_HEROKU) {
  79. // return WIKI.db.User.findOne({ provider: 'local', email: process.env.WIKI_ADMIN_EMAIL }).then((c) => {
  80. // if (c < 1) {
  81. // // Create root admin account (HEROKU ONLY)
  82. // return WIKI.db.User.create({
  83. // provider: 'local',
  84. // email: process.env.WIKI_ADMIN_EMAIL,
  85. // name: 'Administrator',
  86. // password: '$2a$04$MAHRw785Xe/Jd5kcKzr3D.VRZDeomFZu2lius4gGpZZ9cJw7B7Mna', // admin123 (default)
  87. // role: 'admin'
  88. // }).then(() => {
  89. // WIKI.logger.info('[AUTH] Root admin account created successfully!')
  90. // return true
  91. // }).catch((err) => {
  92. // WIKI.logger.error('[AUTH] An error occured while creating root admin account:')
  93. // WIKI.logger.error(err)
  94. // return err
  95. // })
  96. // } else { return true }
  97. // })
  98. // } else { return true }
  99. // })
  100. return this
  101. }
  102. }