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.

108 lines
3.2 KiB

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