You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.5 KiB

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