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.

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