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.

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