authentication.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const Model = require('objection').Model
  2. const fs = require('fs-extra')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const commonHelper = require('../helpers/common')
  7. /* global WIKI */
  8. /**
  9. * Authentication model
  10. */
  11. module.exports = class Authentication extends Model {
  12. static get tableName() { return 'authentication' }
  13. static get jsonSchema () {
  14. return {
  15. type: 'object',
  16. required: ['module'],
  17. properties: {
  18. id: { type: 'string' },
  19. module: { type: 'string' },
  20. selfRegistration: {type: 'boolean'}
  21. }
  22. }
  23. }
  24. static get jsonAttributes() {
  25. return ['config', 'domainWhitelist', 'autoEnrollGroups']
  26. }
  27. static async getStrategy(key) {
  28. return WIKI.models.authentication.query().findOne({ key })
  29. }
  30. static async getStrategies() {
  31. const strategies = await WIKI.models.authentication.query().orderBy('order')
  32. return strategies.map(str => ({
  33. ...str,
  34. domainWhitelist: _.get(str.domainWhitelist, 'v', []),
  35. autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
  36. }))
  37. }
  38. static async refreshStrategiesFromDisk() {
  39. try {
  40. // -> Fetch definitions from disk
  41. const authenticationDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication'))
  42. WIKI.data.authentication = []
  43. for (const dir of authenticationDirs) {
  44. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8')
  45. const defParsed = yaml.load(def)
  46. defParsed.key = dir
  47. defParsed.props = commonHelper.parseModuleProps(defParsed.props)
  48. WIKI.data.analytics.push(defParsed)
  49. WIKI.logger.debug(`Loaded authentication module definition ${dir}: [ OK ]`)
  50. }
  51. WIKI.logger.info(`Loaded ${WIKI.data.analytics.length} authentication module definitions: [ OK ]`)
  52. } catch (err) {
  53. WIKI.logger.error(`Failed to scan or load authentication providers: [ FAILED ]`)
  54. WIKI.logger.error(err)
  55. }
  56. }
  57. }