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.

67 lines
1.9 KiB

  1. const passport = require('passport')
  2. const fs = require('fs-extra')
  3. const _ = require('lodash')
  4. const path = require('path')
  5. /* global WIKI */
  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.users.query().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. return this
  27. },
  28. async activateStrategies() {
  29. try {
  30. // Unload any active strategies
  31. WIKI.auth.strategies = {}
  32. const currentStrategies = _.keys(passport._strategies)
  33. _.pull(currentStrategies, 'session')
  34. _.forEach(currentStrategies, stg => { passport.unuse(stg) })
  35. // Load enable strategies
  36. const enabledStrategies = await WIKI.db.authentication.getEnabledStrategies()
  37. for (let idx in enabledStrategies) {
  38. const stg = enabledStrategies[idx]
  39. const strategy = require(`../modules/authentication/${stg.key}`)
  40. stg.config.callbackURL = `${WIKI.config.site.host}/login/${stg.key}/callback`
  41. strategy.init(passport, stg.config)
  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. WIKI.auth.strategies[stg.key] = strategy
  52. WIKI.logger.info(`Authentication Strategy ${stg.title}: [ OK ]`)
  53. }
  54. } catch (err) {
  55. WIKI.logger.error(`Authentication Strategy: [ FAILED ]`)
  56. WIKI.logger.error(err)
  57. }
  58. }
  59. }