auth.js 3.2 KB

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