authentication.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const Model = require('objection').Model
  2. const autoload = require('auto-load')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. /* global WIKI */
  6. /**
  7. * Authentication model
  8. */
  9. module.exports = class Authentication extends Model {
  10. static get tableName() { return 'authentication' }
  11. static get jsonSchema () {
  12. return {
  13. type: 'object',
  14. required: ['key', 'title', 'isEnabled', 'useForm'],
  15. properties: {
  16. id: {type: 'integer'},
  17. key: {type: 'string'},
  18. title: {type: 'string'},
  19. isEnabled: {type: 'boolean'},
  20. useForm: {type: 'boolean'},
  21. config: {type: 'object'},
  22. selfRegistration: {type: 'boolean'},
  23. domainWhitelist: {type: 'object'},
  24. autoEnrollGroups: {type: 'object'}
  25. }
  26. }
  27. }
  28. static async getStrategies() {
  29. const strategies = await WIKI.db.authentication.query()
  30. return strategies.map(str => ({
  31. ...str,
  32. domainWhitelist: _.get(str.domainWhitelist, 'v', []),
  33. autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
  34. }))
  35. }
  36. static async refreshStrategiesFromDisk() {
  37. try {
  38. const dbStrategies = await WIKI.db.authentication.query()
  39. const diskStrategies = autoload(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  40. let newStrategies = []
  41. _.forOwn(diskStrategies, (strategy, strategyKey) => {
  42. if (!_.some(dbStrategies, ['key', strategy.key])) {
  43. newStrategies.push({
  44. key: strategy.key,
  45. title: strategy.title,
  46. isEnabled: false,
  47. useForm: strategy.useForm,
  48. config: _.reduce(strategy.props, (result, value, key) => {
  49. _.set(result, value, '')
  50. return result
  51. }, {}),
  52. selfRegistration: false,
  53. domainWhitelist: { v: [] },
  54. autoEnrollGroups: { v: [] }
  55. })
  56. }
  57. })
  58. if (newStrategies.length > 0) {
  59. await WIKI.db.authentication.query().insert(newStrategies)
  60. WIKI.logger.info(`Loaded ${newStrategies.length} new authentication strategies: [ OK ]`)
  61. } else {
  62. WIKI.logger.info(`No new authentication strategies found: [ SKIPPED ]`)
  63. }
  64. } catch (err) {
  65. WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
  66. WIKI.logger.error(err)
  67. }
  68. }
  69. }